Caps-iOS/CapCollector/AppDelegate.swift

133 lines
4.2 KiB
Swift
Raw Normal View History

2018-05-05 08:08:37 +02:00
//
// AppDelegate.swift
2018-08-16 11:18:27 +02:00
// CapFinder
2018-05-05 08:08:37 +02:00
//
2018-08-16 11:18:27 +02:00
// Created by User on 31.01.18.
// Copyright © 2018 User. All rights reserved.
2018-05-05 08:08:37 +02:00
//
import UIKit
2018-08-16 11:18:27 +02:00
import CoreData
2018-05-05 08:08:37 +02:00
2020-05-16 11:21:55 +02:00
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")
2019-03-15 13:19:19 +01:00
var shouldLaunchCamera = false
2020-05-16 11:21:55 +02:00
var app: AppDelegate!
private let unlockCode = 3849
2018-05-05 08:08:37 +02:00
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
2019-03-15 13:19:19 +01:00
2020-05-16 11:21:55 +02:00
// MARK: Static Properties
2018-08-16 11:18:27 +02:00
/// Main tint color of the app
static let tintColor = UIColor(red: 122/255, green: 155/255, blue: 41/255, alpha: 1)
2018-05-05 08:08:37 +02:00
var window: UIWindow?
2020-05-16 11:21:55 +02:00
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!
2020-05-16 11:21:55 +02:00
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")
}
}
2020-05-16 11:21:55 +02:00
let serverUrl = URL(string: "https://cc.ssl443.org")!
2018-05-05 08:08:37 +02:00
2018-08-16 11:18:27 +02:00
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
2020-05-16 11:21:55 +02:00
app = self
storage = Storage(in: documentsFolder)
reachability = try! Reachability()
//resetToFactoryState()
database = Database(url: dbUrl, server: serverUrl)
2018-08-16 11:18:27 +02:00
return true
}
2019-03-15 13:19:19 +01:00
2020-05-16 11:21:55 +02:00
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)
2019-03-15 13:19:19 +01:00
}
func lock() {
isUnlocked = false
}
func checkUnlock(with pin: Int) -> Bool {
isUnlocked = pin == unlockCode
return isUnlocked
}
2019-03-15 13:19:19 +01:00
private func handleShortCutItem(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
2020-05-16 11:21:55 +02:00
log("Shortcut pressed")
2019-03-15 13:19:19 +01:00
shouldLaunchCamera = true
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
app.database?.uploadRemainingCaps()
2020-05-16 11:21:55 +02:00
app.database?.uploadRemainingImages()
2019-03-15 13:19:19 +01:00
guard shouldLaunchCamera else { return }
shouldLaunchCamera = false
if let c = (frontmostViewController as? UINavigationController)?.topViewController as? TableView {
2020-05-16 11:21:55 +02:00
c.showCameraView()
2019-03-15 13:19:19 +01:00
}
}
/*
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)
}
2018-08-16 11:18:27 +02:00
var frontmostViewController: UIViewController? {
var controller = window?.rootViewController
while let presentedViewController = controller?.presentedViewController {
controller = presentedViewController
}
return controller
}
2018-05-05 08:08:37 +02:00
2019-03-15 13:19:19 +01:00
2018-05-05 08:08:37 +02:00
}
2020-05-16 11:21:55 +02:00
extension AppDelegate: Logger { }