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