Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Rev 18 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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()
19 efrain 56
                            let syncId = syncDao.insert(record: sync)
57
 
58
                            if syncId > 0 {
18 efrain 59
                                appData.deviceUuid = new_uuid
60
                                appData.save()
61
 
19 efrain 62
                                sync.id = syncId
63
 
18 efrain 64
                                let syncAdapter = SyncAdapter()
19 efrain 65
                                syncAdapter.registerDevice(record: sync)
17 efrain 66
                            }
67
                        }
18 efrain 68
                        catch {/* error handling here */}
17 efrain 69
                    }
18 efrain 70
                } else {
71
                    do {
72
                        try appData.deviceUuid.write(to: fileURL, atomically: false, encoding: .utf8)
73
                    }
17 efrain 74
                    catch {/* error handling here */}
75
                }
76
            }
77
        }
1 efrain 78
    }
79
 
80
    var body: some Scene {
81
 
82
        WindowGroup {
83
            MainView()
84
        }
85
 
86
        .onChange(of: scenePhase) { newScenePhase in
87
            switch newScenePhase {
88
                case .active:
89
                    print("App is active")
90
                    syncForeground.timer.invalidate()
91
                    syncForeground.timer = Timer.scheduledTimer(timeInterval: 30.0, target: syncAdapter, selector: #selector(syncAdapter.updateTimerForeground), userInfo: nil, repeats: true)
92
                    break
93
 
94
                case .inactive:
95
                  print("App is inactive")
96
                    syncForeground.timer.invalidate()
97
                    break
98
 
99
                case .background:
100
                  print("App is in background")
101
                    syncForeground.timer.invalidate()
102
                    break
103
 
104
                @unknown default:
105
                    print("Oh - interesting: I received an unexpected new value.")
106
                    syncForeground.timer.invalidate()
107
                    break
108
                }
109
          }
110
    }
111
 
12 efrain 112
 
1 efrain 113
}
114
 
115
 
116
class SyncForegroundObservableObject: ObservableObject
117
{
118
    @Published var timer : Timer = Timer()
119
}