// // 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("TableView: Fix blur background of search bar after transition") #warning("TableView: Add banner to jump down to unmatched caps / bottom") #warning("Database: Calculate thumbnails and colors in the background") var shouldLaunchCamera = false var app: AppDelegate! private let unlockCode = 3849 @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 dbUrl: URL { documentsFolder.appendingPathComponent("db.sqlite3") } /// Indicate if the user has write permissions. private(set) var isUnlocked: Bool { get { UserDefaults.standard.bool(forKey: "unlocked") } set { UserDefaults.standard.set(newValue, forKey: "unlocked") } } 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() database = Database(url: dbUrl, server: serverUrl) 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) } func lock() { isUnlocked = false } func checkUnlock(with pin: Int) -> Bool { isUnlocked = pin == unlockCode return isUnlocked } private func handleShortCutItem(_ shortcutItem: UIApplicationShortcutItem) -> Bool { log("Shortcut pressed") shouldLaunchCamera = true return true } func applicationDidBecomeActive(_ application: UIApplication) { app.database?.uploadRemainingCaps() 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 { }