| 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
|
| 8 |
efrain |
16 |
|
|
|
17 |
@ObservedObject var syncForeground = SyncForegroundObservableObject()
|
|
|
18 |
private let syncAdapter = SyncAdapter()
|
| 1 |
efrain |
19 |
|
|
|
20 |
init() {
|
|
|
21 |
|
|
|
22 |
let coloredAppearance = UINavigationBarAppearance()
|
|
|
23 |
coloredAppearance.configureWithOpaqueBackground()
|
|
|
24 |
coloredAppearance.backgroundColor = UIColor(Color("color_app_bar_backgroud"))
|
|
|
25 |
coloredAppearance.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
|
|
|
26 |
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
|
|
|
27 |
coloredAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
|
|
|
28 |
|
|
|
29 |
UINavigationBar.appearance().standardAppearance = coloredAppearance
|
|
|
30 |
UINavigationBar.appearance().compactAppearance = coloredAppearance
|
|
|
31 |
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
|
|
|
32 |
|
|
|
33 |
UINavigationBar.appearance().tintColor = UIColor(Color("color_app_bar_foreground"))
|
|
|
34 |
|
|
|
35 |
UIPageControl.appearance().currentPageIndicatorTintColor = .systemBlue
|
|
|
36 |
UIPageControl.appearance().pageIndicatorTintColor = .systemGray2
|
|
|
37 |
|
|
|
38 |
let database = Database.sharedInstance
|
|
|
39 |
if database.open() != nil {
|
|
|
40 |
database.createTables()
|
|
|
41 |
}
|
| 8 |
efrain |
42 |
|
| 1 |
efrain |
43 |
registerBGTasksScheduler()
|
| 8 |
efrain |
44 |
scheduleProcess()
|
|
|
45 |
scheduleRefresh()
|
| 1 |
efrain |
46 |
}
|
|
|
47 |
|
|
|
48 |
var body: some Scene {
|
|
|
49 |
|
|
|
50 |
WindowGroup {
|
|
|
51 |
MainView()
|
|
|
52 |
} .onChange(of: scenePhase) { newScenePhase in
|
|
|
53 |
switch newScenePhase {
|
| 8 |
efrain |
54 |
case .active:
|
|
|
55 |
print("App is active")
|
|
|
56 |
syncForeground.timer.invalidate()
|
|
|
57 |
syncForeground.timer = Timer.scheduledTimer(timeInterval: 30.0, target: syncAdapter, selector: #selector(syncAdapter.updateTimer), userInfo: nil, repeats: true)
|
|
|
58 |
break
|
|
|
59 |
|
|
|
60 |
case .inactive:
|
|
|
61 |
print("App is inactive")
|
|
|
62 |
syncForeground.timer.invalidate()
|
|
|
63 |
break
|
|
|
64 |
|
|
|
65 |
case .background:
|
|
|
66 |
print("App is in background")
|
|
|
67 |
syncForeground.timer.invalidate()
|
|
|
68 |
break
|
|
|
69 |
|
|
|
70 |
@unknown default:
|
|
|
71 |
print("Oh - interesting: I received an unexpected new value.")
|
|
|
72 |
syncForeground.timer.invalidate()
|
|
|
73 |
break
|
|
|
74 |
}
|
| 1 |
efrain |
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
func registerBGTasksScheduler() {
|
|
|
79 |
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
|
| 8 |
efrain |
80 |
self.handleRefresh(task: task as! BGAppRefreshTask)
|
|
|
81 |
}
|
|
|
82 |
|
| 1 |
efrain |
83 |
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_PROCESS, using: nil) { task in
|
| 8 |
efrain |
84 |
self.handleProcessTask(task: task as! BGProcessingTask)
|
|
|
85 |
}
|
| 1 |
efrain |
86 |
|
| 8 |
efrain |
87 |
}
|
| 1 |
efrain |
88 |
|
| 8 |
efrain |
89 |
func scheduleRefresh() {
|
|
|
90 |
let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
|
|
|
91 |
request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
|
|
|
92 |
|
| 1 |
efrain |
93 |
|
|
|
94 |
|
| 8 |
efrain |
95 |
do {
|
|
|
96 |
try BGTaskScheduler.shared.submit(request)
|
|
|
97 |
} catch {
|
|
|
98 |
print("Could not schedule app refresh: \(error)")
|
| 1 |
efrain |
99 |
}
|
| 8 |
efrain |
100 |
}
|
| 1 |
efrain |
101 |
|
| 8 |
efrain |
102 |
func scheduleProcess() {
|
|
|
103 |
let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
|
|
|
104 |
request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
|
|
|
105 |
request.requiresNetworkConnectivity = true
|
|
|
106 |
request.requiresExternalPower = false
|
| 1 |
efrain |
107 |
|
| 8 |
efrain |
108 |
do {
|
|
|
109 |
try BGTaskScheduler.shared.submit(request)
|
|
|
110 |
} catch {
|
|
|
111 |
print("Could not schedule processing: \(error)")
|
| 1 |
efrain |
112 |
}
|
| 8 |
efrain |
113 |
}
|
| 1 |
efrain |
114 |
|
| 8 |
efrain |
115 |
func handleProcessTask(task: BGProcessingTask) {
|
|
|
116 |
scheduleProcess()
|
|
|
117 |
|
|
|
118 |
let syncAdapter = SyncAdapter()
|
|
|
119 |
syncAdapter.sync { success in
|
|
|
120 |
}
|
| 1 |
efrain |
121 |
|
| 8 |
efrain |
122 |
task.expirationHandler = {
|
|
|
123 |
task.setTaskCompleted(success: false)
|
|
|
124 |
}
|
|
|
125 |
task.setTaskCompleted(success: true)
|
|
|
126 |
}
|
| 1 |
efrain |
127 |
|
| 8 |
efrain |
128 |
func handleRefresh(task: BGAppRefreshTask) {
|
|
|
129 |
scheduleRefresh()
|
|
|
130 |
task.expirationHandler = {
|
|
|
131 |
task.setTaskCompleted(success: false)
|
|
|
132 |
}
|
|
|
133 |
task.setTaskCompleted(success: true)
|
|
|
134 |
}
|
|
|
135 |
}
|
| 1 |
efrain |
136 |
|
|
|
137 |
|
| 8 |
efrain |
138 |
class SyncForegroundObservableObject: ObservableObject
|
|
|
139 |
{
|
|
|
140 |
@Published var timer : Timer = Timer()
|
| 1 |
efrain |
141 |
}
|