| 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 |
|
| 8 |
efrain |
38 |
|
| 11 |
efrain |
39 |
|
| 1 |
efrain |
40 |
registerBGTasksScheduler()
|
| 8 |
efrain |
41 |
scheduleProcess()
|
|
|
42 |
scheduleRefresh()
|
| 1 |
efrain |
43 |
}
|
|
|
44 |
|
|
|
45 |
var body: some Scene {
|
|
|
46 |
|
|
|
47 |
WindowGroup {
|
|
|
48 |
MainView()
|
|
|
49 |
} .onChange(of: scenePhase) { newScenePhase in
|
|
|
50 |
switch newScenePhase {
|
| 8 |
efrain |
51 |
case .active:
|
|
|
52 |
print("App is active")
|
|
|
53 |
syncForeground.timer.invalidate()
|
|
|
54 |
syncForeground.timer = Timer.scheduledTimer(timeInterval: 30.0, target: syncAdapter, selector: #selector(syncAdapter.updateTimer), userInfo: nil, repeats: true)
|
|
|
55 |
break
|
|
|
56 |
|
|
|
57 |
case .inactive:
|
|
|
58 |
print("App is inactive")
|
|
|
59 |
syncForeground.timer.invalidate()
|
|
|
60 |
break
|
|
|
61 |
|
|
|
62 |
case .background:
|
|
|
63 |
print("App is in background")
|
|
|
64 |
syncForeground.timer.invalidate()
|
|
|
65 |
break
|
|
|
66 |
|
|
|
67 |
@unknown default:
|
|
|
68 |
print("Oh - interesting: I received an unexpected new value.")
|
|
|
69 |
syncForeground.timer.invalidate()
|
|
|
70 |
break
|
|
|
71 |
}
|
| 1 |
efrain |
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
func registerBGTasksScheduler() {
|
|
|
76 |
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
|
| 8 |
efrain |
77 |
self.handleRefresh(task: task as! BGAppRefreshTask)
|
|
|
78 |
}
|
|
|
79 |
|
| 1 |
efrain |
80 |
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_PROCESS, using: nil) { task in
|
| 8 |
efrain |
81 |
self.handleProcessTask(task: task as! BGProcessingTask)
|
|
|
82 |
}
|
| 1 |
efrain |
83 |
|
| 8 |
efrain |
84 |
}
|
| 1 |
efrain |
85 |
|
| 8 |
efrain |
86 |
func scheduleRefresh() {
|
|
|
87 |
let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
|
|
|
88 |
request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
|
|
|
89 |
|
| 1 |
efrain |
90 |
|
|
|
91 |
|
| 8 |
efrain |
92 |
do {
|
|
|
93 |
try BGTaskScheduler.shared.submit(request)
|
|
|
94 |
} catch {
|
|
|
95 |
print("Could not schedule app refresh: \(error)")
|
| 1 |
efrain |
96 |
}
|
| 8 |
efrain |
97 |
}
|
| 1 |
efrain |
98 |
|
| 8 |
efrain |
99 |
func scheduleProcess() {
|
|
|
100 |
let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
|
|
|
101 |
request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
|
|
|
102 |
request.requiresNetworkConnectivity = true
|
|
|
103 |
request.requiresExternalPower = false
|
| 1 |
efrain |
104 |
|
| 8 |
efrain |
105 |
do {
|
|
|
106 |
try BGTaskScheduler.shared.submit(request)
|
|
|
107 |
} catch {
|
|
|
108 |
print("Could not schedule processing: \(error)")
|
| 1 |
efrain |
109 |
}
|
| 8 |
efrain |
110 |
}
|
| 1 |
efrain |
111 |
|
| 8 |
efrain |
112 |
func handleProcessTask(task: BGProcessingTask) {
|
|
|
113 |
scheduleProcess()
|
|
|
114 |
|
|
|
115 |
let syncAdapter = SyncAdapter()
|
|
|
116 |
syncAdapter.sync { success in
|
|
|
117 |
}
|
| 1 |
efrain |
118 |
|
| 8 |
efrain |
119 |
task.expirationHandler = {
|
|
|
120 |
task.setTaskCompleted(success: false)
|
|
|
121 |
}
|
|
|
122 |
task.setTaskCompleted(success: true)
|
|
|
123 |
}
|
| 1 |
efrain |
124 |
|
| 8 |
efrain |
125 |
func handleRefresh(task: BGAppRefreshTask) {
|
|
|
126 |
scheduleRefresh()
|
|
|
127 |
task.expirationHandler = {
|
|
|
128 |
task.setTaskCompleted(success: false)
|
|
|
129 |
}
|
|
|
130 |
task.setTaskCompleted(success: true)
|
|
|
131 |
}
|
|
|
132 |
}
|
| 1 |
efrain |
133 |
|
|
|
134 |
|
| 8 |
efrain |
135 |
class SyncForegroundObservableObject: ObservableObject
|
|
|
136 |
{
|
|
|
137 |
@Published var timer : Timer = Timer()
|
| 1 |
efrain |
138 |
}
|