Add offline mode

This commit is contained in:
christophhagen 2020-08-09 21:04:30 +02:00
parent 8892d04f62
commit 5441261c6c
4 changed files with 51 additions and 3 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="qlf-I7-aOI"> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097.2" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="qlf-I7-aOI">
<device id="retina4_7" orientation="portrait" appearance="light"/> <device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies> <dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
@ -168,7 +168,7 @@
</connections> </connections>
</tableView> </tableView>
<navigationItem key="navigationItem" title="Caps" id="qe9-JJ-Ei4"> <navigationItem key="navigationItem" title="Caps" id="qe9-JJ-Ei4">
<barButtonItem key="leftBarButtonItem" image="arrow.clockwise.icloud" catalog="system" id="SDv-mW-eqq"> <barButtonItem key="leftBarButtonItem" title="Item" image="arrow.clockwise.icloud" catalog="system" id="SDv-mW-eqq">
<connections> <connections>
<action selector="updateInfo:" destination="2ro-5c-16N" id="DgZ-Jh-0hR"/> <action selector="updateInfo:" destination="2ro-5c-16N" id="DgZ-Jh-0hR"/>
</connections> </connections>
@ -179,6 +179,9 @@
</connections> </connections>
</barButtonItem> </barButtonItem>
</navigationItem> </navigationItem>
<connections>
<outlet property="infoButton" destination="SDv-mW-eqq" id="hKf-A3-chi"/>
</connections>
</tableViewController> </tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="xpG-Fd-7Lc" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/> <placeholder placeholderIdentifier="IBFirstResponder" id="xpG-Fd-7Lc" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects> </objects>

View File

@ -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 // MARK: Data updates
/** /**
@ -168,6 +178,10 @@ final class Database {
log("Cap image not saved") log("Cap image not saved")
return false return false
} }
guard !isInOfflineMode else {
log("Offline mode: Not uploading cap")
return true
}
upload.upload(name: name, for: cap.id) { success in upload.upload(name: name, for: cap.id) { success in
guard success else { guard success else {
return return
@ -224,6 +238,10 @@ final class Database {
log("Failed to add cap \(cap) version \(version) to upload queue") log("Failed to add cap \(cap) version \(version) to upload queue")
return false return false
} }
guard !isInOfflineMode else {
log("Offline mode: Not uploading cap image")
return true
}
upload.uploadImage(for: cap, version: version) { actualVersion in upload.uploadImage(for: cap, version: version) { actualVersion in
guard let actualVersion = actualVersion else { guard let actualVersion = actualVersion else {
self.log("Failed to upload image \(version) for cap \(cap)") self.log("Failed to upload image \(version) for cap \(cap)")

View File

@ -12,6 +12,8 @@ import SQLite
struct Upload { struct Upload {
static let offlineKey = "offline"
let serverUrl: URL let serverUrl: URL
let table = Table("uploads") let table = Table("uploads")

View File

@ -12,6 +12,8 @@ import JGProgressHUD
class TableView: UITableViewController { class TableView: UITableViewController {
@IBOutlet weak var infoButton: UIBarButtonItem!
private var classifier: Classifier? private var classifier: Classifier?
private var accessory: SearchAndDisplayAccessory? private var accessory: SearchAndDisplayAccessory?
@ -79,7 +81,11 @@ class TableView: UITableViewController {
// MARK: - Actions // 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() downloadCapNames()
} }
@ -122,6 +128,8 @@ class TableView: UITableViewController {
accessory = SearchAndDisplayAccessory(width: self.view.frame.width) accessory = SearchAndDisplayAccessory(width: self.view.frame.width)
accessory?.delegate = self accessory?.delegate = self
initInfoButton()
app.database.delegate = self app.database.delegate = self
let count = app.database.capCount let count = app.database.capCount
if count == 0 { if count == 0 {
@ -152,6 +160,16 @@ class TableView: UITableViewController {
initNavigationItemTitleView() 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() { private func initNavigationItemTitleView() {
self.titleLabel = UILabel() self.titleLabel = UILabel()
titleLabel.text = titleText titleLabel.text = titleText
@ -315,6 +333,13 @@ class TableView: UITableViewController {
nav.allowLandscape = true nav.allowLandscape = true
} }
private func showOfflineDialog() {
let offline = app.database.isInOfflineMode
if offline {
}
}
private func downloadCapNames() { private func downloadCapNames() {
app.database.downloadCapNames { success in app.database.downloadCapNames { success in
guard success else { guard success else {