USB-Meter / USB Meter / AppDelegate.swift
Newer Older
99 lines | 4.387kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  AppDelegate.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 01/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import UIKit
10
import CoreData
11

            
12
//let btSerial = BluetoothSerial(delegate: BSD())
13
let appData = AppData()
14
enum Constants {
15
    static let chartUnderscan: CGFloat = 0.5
16
    static let chartOverscan: CGFloat = 1 - chartUnderscan
17
}
18
// MARK: Clock
19

            
20
// MARK: Debug
21
public func track(_ message: String = "", file: String = #file, function: String = #function, line: Int = #line ) {
22
    let date = Date()
23
    let calendar = Calendar.current
24
    let hour = calendar.component(.hour, from: date)
25
    let minutes = calendar.component(.minute, from: date)
26
    let seconds = calendar.component(.second, from: date)
27
    print("\(hour):\(minutes):\(seconds) - \(file):\(line) - \(function) \(message)")
28
}
29

            
30
@UIApplicationMain
31
class AppDelegate: UIResponder, UIApplicationDelegate {
32

            
33

            
34
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
35
        // Override point for customization after application launch.
36
        return true
37
    }
38

            
39
    // MARK: UISceneSession Lifecycle
40

            
41
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
42
        // Called when a new scene session is being created.
43
        // Use this method to select a configuration to create the new scene with.
44
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
45
    }
46

            
47
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
48
        // Called when the user discards a scene session.
49
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
50
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
51
    }
52

            
53
    // MARK: - Core Data stack
54

            
55
    lazy var persistentContainer: NSPersistentCloudKitContainer = {
56
        /*
57
         The persistent container for the application. This implementation
58
         creates and returns a container, having loaded the store for the
59
         application to it. This property is optional since there are legitimate
60
         error conditions that could cause the creation of the store to fail.
61
        */
62
        let container = NSPersistentCloudKitContainer(name: "CKModel")
63
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
64
            if let error = error as NSError? {
65
                // Replace this implementation with code to handle the error appropriately.
66
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
67

            
68
                /*
69
                 Typical reasons for an error here include:
70
                 * The parent directory does not exist, cannot be created, or disallows writing.
71
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
72
                 * The device is out of space.
73
                 * The store could not be migrated to the current model version.
74
                 Check the error message to determine what the actual problem was.
75
                 */
76
                fatalError("Unresolved error \(error), \(error.userInfo)")
77
            }
78
        })
79
        return container
80
    }()
81

            
82
    // MARK: - Core Data Saving support
83

            
84
    func saveContext () {
85
        let context = persistentContainer.viewContext
86
        if context.hasChanges {
87
            do {
88
                try context.save()
89
            } catch {
90
                // Replace this implementation with code to handle the error appropriately.
91
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
92
                let nserror = error as NSError
93
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
94
            }
95
        }
96
    }
97

            
98
}
99