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) {
|
|
|
46 |
if let new_uuid = UIDevice.current.identifierForVendor?.uuidString {
|
|
|
47 |
do {
|
|
|
48 |
try new_uuid.write(to: fileURL, atomically: false, encoding: .utf8)
|
|
|
49 |
|
|
|
50 |
var sync = SyncModel()
|
|
|
51 |
sync.data = new_uuid
|
|
|
52 |
sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
|
|
|
53 |
|
|
|
54 |
let syncDao = SyncDao()
|
|
|
55 |
if syncDao.insert(record: sync) > 0 {
|
|
|
56 |
appData.deviceUuid = new_uuid
|
|
|
57 |
appData.save()
|
|
|
58 |
|
|
|
59 |
let syncAdapter = SyncAdapter()
|
|
|
60 |
syncAdapter.sync(isForeground: true) {
|
|
|
61 |
success in
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
catch {/* error handling here */}
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
}
|
1 |
efrain |
69 |
}
|
|
|
70 |
|
|
|
71 |
var body: some Scene {
|
|
|
72 |
|
|
|
73 |
WindowGroup {
|
|
|
74 |
MainView()
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
.onChange(of: scenePhase) { newScenePhase in
|
|
|
78 |
switch newScenePhase {
|
|
|
79 |
case .active:
|
|
|
80 |
print("App is active")
|
|
|
81 |
syncForeground.timer.invalidate()
|
|
|
82 |
syncForeground.timer = Timer.scheduledTimer(timeInterval: 30.0, target: syncAdapter, selector: #selector(syncAdapter.updateTimerForeground), userInfo: nil, repeats: true)
|
|
|
83 |
break
|
|
|
84 |
|
|
|
85 |
case .inactive:
|
|
|
86 |
print("App is inactive")
|
|
|
87 |
syncForeground.timer.invalidate()
|
|
|
88 |
break
|
|
|
89 |
|
|
|
90 |
case .background:
|
|
|
91 |
print("App is in background")
|
|
|
92 |
syncForeground.timer.invalidate()
|
|
|
93 |
break
|
|
|
94 |
|
|
|
95 |
@unknown default:
|
|
|
96 |
print("Oh - interesting: I received an unexpected new value.")
|
|
|
97 |
syncForeground.timer.invalidate()
|
|
|
98 |
break
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
12 |
efrain |
103 |
|
1 |
efrain |
104 |
}
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
class SyncForegroundObservableObject: ObservableObject
|
|
|
108 |
{
|
|
|
109 |
@Published var timer : Timer = Timer()
|
|
|
110 |
}
|