Caps-iOS/CapCollector/SettingsController.swift
2018-10-17 11:46:16 +02:00

145 lines
3.9 KiB
Swift

//
// SettingsController.swift
// CapCollector
//
// Created by Christoph on 15.10.18.
// Copyright © 2018 CH. All rights reserved.
//
import UIKit
class SettingsController: UITableViewController {
@IBOutlet weak var totalCapsLabel: UILabel!
@IBOutlet weak var recognizedCapsLabel: UILabel!
@IBOutlet weak var databaseUpdatesLabel: UILabel!
@IBOutlet weak var dropboxAccountLabel: UILabel!
private var nameFileChanges = false
private var isUpdatingCounts = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
updateDropboxStatus()
updateNameFileStats()
updateDatabaseStats()
}
private func updateDatabaseStats() {
let totalCaps = CapNames.shared.count
totalCapsLabel.text = "\(totalCaps) caps, \(CapNames.shared.imageCount) images"
let recognizedCaps = Persistence.recognizedCapCount
let newCapCount = totalCaps - recognizedCaps
recognizedCapsLabel.text = "\(recognizedCaps) recognized, \(newCapCount) new"
}
private func updateNameFileStats() {
let capCount = Persistence.notUploadedCapCount
let imageCount = Persistence.notUploadedImageCount
nameFileChanges = capCount > 0 || imageCount > 0
databaseUpdatesLabel.text = "\(capCount) new caps and \(imageCount) new images"
}
private func updateDropboxStatus() {
print("Dropbox enabled: \(DropboxController.shared.isEnabled)")
dropboxAccountLabel.text = DropboxController.shared.isEnabled ? "Sign out" : "Sign in"
}
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
switch indexPath.section {
case 0: // Database
return false
case 1: // Upload
return nameFileChanges
case 2: // Refresh count
return !isUpdatingCounts
case 3: // Dropbox account
return true
default: return false
}
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
switch indexPath.section {
case 0: // Database
return nil
case 1: // Upload
return nameFileChanges ? indexPath : nil
case 2: // Refresh count
return isUpdatingCounts ? nil : indexPath
case 3: // Dropbox account
return indexPath
default: return nil
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch indexPath.section {
case 0:
break
case 1: // Upload
if nameFileChanges {
uploadNameFile()
}
case 2: // Refresh count
updateCounts()
case 3: // Dropbox account
break
default: break
}
}
private func updateCounts() {
isUpdatingCounts = true
CapImages.shared.updateCounts() {
self.isUpdatingCounts = false
self.updateDatabaseStats()
}
}
private func uploadNameFile() {
CapNames.shared.saveAndUpload()
updateNameFileStats()
}
private func toggleDropbox() {
guard !DropboxController.shared.isEnabled else {
DropboxController.shared.signOut()
updateDropboxStatus()
return
}
DropboxController.shared.setup(in: self)
updateDropboxStatus()
}
}
extension SettingsController: DropboxControllerDelegate {
func dropboxControllerDidFinishLoadingCaps() {
}
func dropboxControllerDidLoadNames() {
}
func dropboxController(didLoad caps: [Int]) {
}
func dropboxController(didLoad cap: Int) {
}
}