| 1 | efrain | 1 | //
 | 
        
           |  |  | 2 | //  twogetskillsApp.swift
 | 
        
           |  |  | 3 | //  twogetskills
 | 
        
           |  |  | 4 | //
 | 
        
           |  |  | 5 | //  Created by Efrain Yanez Recanatini on 1/12/22.
 | 
        
           |  |  | 6 | //
 | 
        
           |  |  | 7 |   | 
        
           |  |  | 8 | import SwiftUI
 | 
        
           |  |  | 9 | import BackgroundTasks
 | 
        
           |  |  | 10 |   | 
        
           |  |  | 11 |   | 
        
           |  |  | 12 | @main
 | 
        
           |  |  | 13 | struct TwoGetSkillsApp: App {
 | 
        
           |  |  | 14 |     @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
 | 
        
           |  |  | 15 |     @Environment(\.scenePhase) var scenePhase
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 |     @ObservedObject var syncForeground = SyncForegroundObservableObject()
 | 
        
           |  |  | 19 |     private let syncAdapter = SyncAdapter()
 | 
        
           |  |  | 20 |     private var appData = AppData.sharedInstance
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 |     init() {
 | 
        
           |  |  | 23 |       let coloredAppearance = UINavigationBarAppearance()
 | 
        
           |  |  | 24 |       coloredAppearance.configureWithOpaqueBackground()
 | 
        
           |  |  | 25 |       coloredAppearance.backgroundColor = UIColor(Color("color_app_bar_backgroud"))
 | 
        
           |  |  | 26 |       coloredAppearance.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
 | 
        
           |  |  | 27 |       coloredAppearance.largeTitleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
 | 
        
           |  |  | 28 |       coloredAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 |       UINavigationBar.appearance().standardAppearance = coloredAppearance
 | 
        
           |  |  | 31 |       UINavigationBar.appearance().compactAppearance = coloredAppearance
 | 
        
           |  |  | 32 |       UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 |       UINavigationBar.appearance().tintColor = UIColor(Color("color_app_bar_foreground"))
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |       UIPageControl.appearance().currentPageIndicatorTintColor = .systemBlue
 | 
        
           |  |  | 37 |       UIPageControl.appearance().pageIndicatorTintColor = .systemGray2
 | 
        
           |  |  | 38 |   | 
        
           | 17 | efrain | 39 |   | 
        
           | 1 | efrain | 40 |   | 
        
           |  |  | 41 |   | 
        
           | 17 | efrain | 42 |         if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |             let fileURL = dir.appendingPathComponent(Config.DEVICE_UUID_FILENAME)
 | 
        
           |  |  | 45 |             if !FileManager.default.fileExists(atPath: fileURL.path) {
 | 
        
           | 18 | efrain | 46 |                 if appData.deviceUuid.isEmpty {
 | 
        
           |  |  | 47 |                     if let new_uuid = UIDevice.current.identifierForVendor?.uuidString {
 | 
        
           |  |  | 48 |                         do {
 | 
        
           |  |  | 49 |                             try new_uuid.write(to: fileURL, atomically: false, encoding: .utf8)
 | 
        
           | 17 | efrain | 50 |   | 
        
           | 18 | efrain | 51 |                             var sync = SyncModel()
 | 
        
           |  |  | 52 |                             sync.data = new_uuid
 | 
        
           |  |  | 53 |                             sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |                             let syncDao = SyncDao()
 | 
        
           |  |  | 56 |                             if syncDao.insert(record: sync) > 0 {
 | 
        
           |  |  | 57 |                                 appData.deviceUuid = new_uuid
 | 
        
           |  |  | 58 |                                 appData.save()
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |                                 let syncAdapter = SyncAdapter()
 | 
        
           |  |  | 61 |                                 syncAdapter.sync(isForeground: true) {
 | 
        
           |  |  | 62 |                                     success in
 | 
        
           |  |  | 63 |                                 }
 | 
        
           | 17 | efrain | 64 |                             }
 | 
        
           |  |  | 65 |                         }
 | 
        
           | 18 | efrain | 66 |                         catch {/* error handling here */}
 | 
        
           | 17 | efrain | 67 |                     }
 | 
        
           | 18 | efrain | 68 |                 } else {
 | 
        
           |  |  | 69 |                     do {
 | 
        
           |  |  | 70 |                         try appData.deviceUuid.write(to: fileURL, atomically: false, encoding: .utf8)
 | 
        
           |  |  | 71 |                     }
 | 
        
           | 17 | efrain | 72 |                     catch {/* error handling here */}
 | 
        
           |  |  | 73 |                 }
 | 
        
           |  |  | 74 |             }
 | 
        
           |  |  | 75 |         }
 | 
        
           | 1 | efrain | 76 |     }
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |     var body: some Scene {
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |         WindowGroup {
 | 
        
           |  |  | 81 |             MainView()
 | 
        
           |  |  | 82 |         }
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |         .onChange(of: scenePhase) { newScenePhase in
 | 
        
           |  |  | 85 |             switch newScenePhase {
 | 
        
           |  |  | 86 |                 case .active:
 | 
        
           |  |  | 87 |                     print("App is active")
 | 
        
           |  |  | 88 |                     syncForeground.timer.invalidate()
 | 
        
           |  |  | 89 |                     syncForeground.timer = Timer.scheduledTimer(timeInterval: 30.0, target: syncAdapter, selector: #selector(syncAdapter.updateTimerForeground), userInfo: nil, repeats: true)
 | 
        
           |  |  | 90 |                     break
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 |                 case .inactive:
 | 
        
           |  |  | 93 |                   print("App is inactive")
 | 
        
           |  |  | 94 |                     syncForeground.timer.invalidate()
 | 
        
           |  |  | 95 |                     break
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |                 case .background:
 | 
        
           |  |  | 98 |                   print("App is in background")
 | 
        
           |  |  | 99 |                     syncForeground.timer.invalidate()
 | 
        
           |  |  | 100 |                     break
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |                 @unknown default:
 | 
        
           |  |  | 103 |                     print("Oh - interesting: I received an unexpected new value.")
 | 
        
           |  |  | 104 |                     syncForeground.timer.invalidate()
 | 
        
           |  |  | 105 |                     break
 | 
        
           |  |  | 106 |                 }
 | 
        
           |  |  | 107 |           }
 | 
        
           |  |  | 108 |     }
 | 
        
           |  |  | 109 |   | 
        
           | 12 | efrain | 110 |   | 
        
           | 1 | efrain | 111 | }
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 | class SyncForegroundObservableObject: ObservableObject
 | 
        
           |  |  | 115 | {
 | 
        
           |  |  | 116 |     @Published var timer : Timer = Timer()
 | 
        
           |  |  | 117 | }
 |