AutoCat/AutoCat/AppDelegate.swift

138 lines
5.2 KiB
Swift

import UIKit
import RealmSwift
import RxSwift
import RxCocoa
import os.log
import PKHUD
import AutoCatCore
extension OSLog {
static let startup = OSLog(subsystem: "pro.aliencat.autocat.startup", category: "startup")
}
enum QuickAction {
case none
case check
case checkNumber(String, VehicleEvent?)
case addVoiceRecord
case openReport(String)
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var quickAction: QuickAction = .none
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let config = Realm.Configuration(
schemaVersion: 40,
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion <= 31 {
migration.enumerateObjects(ofType: "Vehicle") { old, new in
if let oldDebugInfo = old?["debugInfo"] as? DynamicObject, let newDebugInfo = new?["debugInfo"] as? DynamicObject {
let addStatus = { (providerName: String) -> Void in
if let provider = oldDebugInfo[providerName] as? DynamicObject, let newProvider = newDebugInfo[providerName] as? DynamicObject {
if provider["error"] != nil {
newProvider["status"] = DebugInfoStatus.error.rawValue
} else {
newProvider["status"] = DebugInfoStatus.success.rawValue
}
}
}
addStatus("autocod")
addStatus("vin01vin")
addStatus("vin01base")
addStatus("vin01history")
addStatus("nomerogram")
}
}
}
if oldSchemaVersion <= 34 {
var ids: Set<String> = Set()
migration.enumerateObjects(ofType: "VehicleEvent") { old, new in
guard let oldId = old?["id"] as? String? else { return }
var newId = oldId ?? UUID().uuidString
if ids.contains(newId) {
newId = UUID().uuidString
}
ids.insert(newId)
new?["id"] = newId
}
}
if oldSchemaVersion <= 36 {
migration.enumerateObjects(ofType: "Vehicle") { old, new in
new?["synchronized"] = true
}
}
})
Realm.Configuration.defaultConfiguration = config
HUD.dimsBackground = true
HUD.allowsInteraction = false
//Logging.URLRequests = { _ in false };
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
#if !targetEnvironment(macCatalyst)
if let shortcutItem = options.shortcutItem {
if shortcutItem.type == "CheckNumberAction" {
self.quickAction = .check
} else if shortcutItem.type == "AddVoiceRecordAction" {
self.quickAction = .addVoiceRecord
}
}
#endif
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
// MARK: - Global menu
#if targetEnvironment(macCatalyst)
override func buildMenu(with builder: UIMenuBuilder) {
super.buildMenu(with: builder)
let command = UIKeyCommand(title: NSLocalizedString("Add new number", comment: ""),
action: #selector(openAddNumberPanel),
input: "n",
modifierFlags: .command)
let menu = UIMenu(options: .displayInline, children: [command])
builder.insertChild(menu, atEndOfMenu: .file)
}
@objc func openAddNumberPanel() {
guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else {
return
}
sceneDelegate.showAddNumberPanel()
}
#endif
}