Caps-iOS/CapCollector/AppDelegate.swift
2020-05-16 11:21:55 +02:00

124 lines
4.1 KiB
Swift

//
// AppDelegate.swift
// CapFinder
//
// Created by User on 31.01.18.
// Copyright © 2018 User. All rights reserved.
//
import UIKit
import CoreData
import Reachability
#warning("ImageSelector: Allow deletion and moving of an image of a cap")
#warning("ImageSelector: Show icons for failed downloads")
#warning("GridController: Allow sorting of caps by color")
#warning("GridController: Reorder caps by dragging")
#warning("GridController: Load and save current mosaic")
#warning("GridController: Add option to specify image width")
#warning("TableView: Fix blur background of search bar after transition")
#warning("TableView: Add banner to jump down to unmatched caps / bottom")
var shouldLaunchCamera = false
var app: AppDelegate!
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// MARK: Static Properties
/// Main tint color of the app
static let tintColor = UIColor(red: 122/255, green: 155/255, blue: 41/255, alpha: 1)
var window: UIWindow?
var mainStoryboard: UIStoryboard {
UIStoryboard(name: "Main", bundle: nil)
}
let documentsFolder = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
var database: Database!
var storage: Storage!
var reachability: Reachability!
var needsDownload = false
var dbUrl: URL {
documentsFolder.appendingPathComponent("db.sqlite3")
}
let serverUrl = URL(string: "https://cc.ssl443.org")!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
app = self
storage = Storage(in: documentsFolder)
reachability = try! Reachability()
//resetToFactoryState()
needsDownload = !FileManager.default.fileExists(atPath: dbUrl.path)
database = Database(url: dbUrl, server: serverUrl)
if needsDownload {
log("New database created")
} else {
let size = (try! FileManager.default.attributesOfItem(atPath: dbUrl.path) as NSDictionary).fileSize()
log("Loaded \(database.capCount) caps, database size: \(size) bytes")
}
return true
}
private func resetToFactoryState() {
for path in try! FileManager.default.contentsOfDirectory(at: documentsFolder, includingPropertiesForKeys: nil) {
try! FileManager.default.removeItem(at: path)
}
UserDefaults.standard.removeObject(forKey: Classifier.userDefaultsKey)
}
private func handleShortCutItem(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
log("Shortcut pressed")
shouldLaunchCamera = true
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
app.database?.uploadRemainingImages()
guard shouldLaunchCamera else { return }
shouldLaunchCamera = false
if let c = (frontmostViewController as? UINavigationController)?.topViewController as? TableView {
c.showCameraView()
}
}
/*
Called when the user activates your application by selecting a shortcut on the home screen, except when
application(_:,willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions) returns `false`.
You should handle the shortcut in those callbacks and return `false` if possible. In that case, this
callback is used if your application is already launched in the background.
*/
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
let handledShortCutItem = handleShortCutItem(shortcutItem)
completionHandler(handledShortCutItem)
}
var frontmostViewController: UIViewController? {
var controller = window?.rootViewController
while let presentedViewController = controller?.presentedViewController {
controller = presentedViewController
}
return controller
}
}
extension AppDelegate: Logger { }