From 5441261c6c809e8aa55efc20557dfa03c5ba57d7 Mon Sep 17 00:00:00 2001 From: christophhagen Date: Sun, 9 Aug 2020 21:04:30 +0200 Subject: [PATCH] Add offline mode --- CapCollector/Base.lproj/Main.storyboard | 7 +++++-- CapCollector/Data/Database.swift | 18 +++++++++++++++++ CapCollector/Data/Upload.swift | 2 ++ CapCollector/TableView.swift | 27 ++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/CapCollector/Base.lproj/Main.storyboard b/CapCollector/Base.lproj/Main.storyboard index ddf74e5..b292b11 100644 --- a/CapCollector/Base.lproj/Main.storyboard +++ b/CapCollector/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -168,7 +168,7 @@ - + @@ -179,6 +179,9 @@ + + + diff --git a/CapCollector/Data/Database.swift b/CapCollector/Data/Database.swift index 89e7718..3df9180 100644 --- a/CapCollector/Data/Database.swift +++ b/CapCollector/Data/Database.swift @@ -145,6 +145,16 @@ final class Database { } } + var isInOfflineMode: Bool { + set { + UserDefaults.standard.set(newValue, forKey: Upload.offlineKey) + log("Offline mode set to \(newValue)") + } + get { + UserDefaults.standard.bool(forKey: Upload.offlineKey) + } + } + // MARK: Data updates /** @@ -168,6 +178,10 @@ final class Database { log("Cap image not saved") return false } + guard !isInOfflineMode else { + log("Offline mode: Not uploading cap") + return true + } upload.upload(name: name, for: cap.id) { success in guard success else { return @@ -224,6 +238,10 @@ final class Database { log("Failed to add cap \(cap) version \(version) to upload queue") return false } + guard !isInOfflineMode else { + log("Offline mode: Not uploading cap image") + return true + } upload.uploadImage(for: cap, version: version) { actualVersion in guard let actualVersion = actualVersion else { self.log("Failed to upload image \(version) for cap \(cap)") diff --git a/CapCollector/Data/Upload.swift b/CapCollector/Data/Upload.swift index 8e04d9c..390b0d0 100644 --- a/CapCollector/Data/Upload.swift +++ b/CapCollector/Data/Upload.swift @@ -12,6 +12,8 @@ import SQLite struct Upload { + static let offlineKey = "offline" + let serverUrl: URL let table = Table("uploads") diff --git a/CapCollector/TableView.swift b/CapCollector/TableView.swift index 77daeca..637e9f0 100644 --- a/CapCollector/TableView.swift +++ b/CapCollector/TableView.swift @@ -12,6 +12,8 @@ import JGProgressHUD class TableView: UITableViewController { + @IBOutlet weak var infoButton: UIBarButtonItem! + private var classifier: Classifier? private var accessory: SearchAndDisplayAccessory? @@ -79,7 +81,11 @@ class TableView: UITableViewController { // MARK: - Actions - @IBAction func updateInfo(_ sender: UIBarButtonItem) { + @IBAction func updateInfo(_ sender: UIBarButtonItem, forEvent event: UIEvent) { + guard let touch = event.allTouches?.first, touch.tapCount > 0, !app.database.isInOfflineMode else { + showOfflineDialog() + return + } downloadCapNames() } @@ -122,6 +128,8 @@ class TableView: UITableViewController { accessory = SearchAndDisplayAccessory(width: self.view.frame.width) accessory?.delegate = self + initInfoButton() + app.database.delegate = self let count = app.database.capCount if count == 0 { @@ -152,6 +160,16 @@ class TableView: UITableViewController { initNavigationItemTitleView() } + private func initInfoButton() { + let offline = app.database.isInOfflineMode + setInfoButtonIcon(offline: offline) + } + + private func setInfoButtonIcon(offline: Bool) { + let symbol = offline ? "icloud.slash" : "arrow.clockwise.icloud" + infoButton.image = UIImage(systemName: symbol) + } + private func initNavigationItemTitleView() { self.titleLabel = UILabel() titleLabel.text = titleText @@ -315,6 +333,13 @@ class TableView: UITableViewController { nav.allowLandscape = true } + private func showOfflineDialog() { + let offline = app.database.isInOfflineMode + if offline { + + } + } + private func downloadCapNames() { app.database.downloadCapNames { success in guard success else {