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 |
@main
|
|
|
12 |
struct TwoGetSkillApp: App {
|
|
|
13 |
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
|
|
|
14 |
@Environment(\.scenePhase) var scenePhase
|
|
|
15 |
//@State public var idxFragment : Int = 0
|
|
|
16 |
|
|
|
17 |
private let colorAppBar = UIColor(hex: Config.COLOR_APP_BAR)
|
|
|
18 |
private let colorAppTextView = UIColor(hex: Config.COLOR_APP_TEXT_VIEW_TITLE)
|
|
|
19 |
|
|
|
20 |
init() {
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
let coloredAppearance = UINavigationBarAppearance()
|
|
|
25 |
coloredAppearance.configureWithOpaqueBackground()
|
|
|
26 |
coloredAppearance.backgroundColor = colorAppBar
|
|
|
27 |
coloredAppearance.titleTextAttributes = [.foregroundColor: colorAppTextView ?? UIColor.systemBlue]
|
|
|
28 |
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: colorAppTextView ?? UIColor.systemBlue]
|
|
|
29 |
coloredAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor:colorAppTextView ?? UIColor.systemBlue]
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
UINavigationBar.appearance().standardAppearance = coloredAppearance
|
|
|
34 |
UINavigationBar.appearance().compactAppearance = coloredAppearance
|
|
|
35 |
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
|
|
|
36 |
|
|
|
37 |
UINavigationBar.appearance().tintColor = colorAppTextView ?? .systemBlue
|
|
|
38 |
|
|
|
39 |
UIPageControl.appearance().currentPageIndicatorTintColor = .systemBlue
|
|
|
40 |
UIPageControl.appearance().pageIndicatorTintColor = .systemGray2
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
var body: some Scene {
|
|
|
47 |
|
|
|
48 |
WindowGroup {
|
|
|
49 |
NavigationView {
|
|
|
50 |
let preference = Preference.sharedInstance
|
|
|
51 |
if preference.userUuid.isEmpty {
|
|
|
52 |
IntroView()
|
|
|
53 |
} else {
|
|
|
54 |
MainView()
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
} .onChange(of: scenePhase) { newScenePhase in
|
|
|
59 |
switch newScenePhase {
|
|
|
60 |
case .active:
|
|
|
61 |
print("App is active")
|
|
|
62 |
case .inactive:
|
|
|
63 |
print("App is inactive")
|
|
|
64 |
case .background:
|
|
|
65 |
print("App is in background")
|
|
|
66 |
@unknown default:
|
|
|
67 |
print("Oh - interesting: I received an unexpected new value.")
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
func registerBGTaskScheduler() {
|
|
|
73 |
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
|
|
|
74 |
self.handleAppRefresh(task: task as! BGAppRefreshTask)
|
|
|
75 |
}
|
|
|
76 |
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_PROCESS, using: nil) { task in
|
|
|
77 |
self.handleProcessingTask(task: task as! BGProcessingTask)
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
func scheduleAppRefresh() {
|
|
|
83 |
let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
|
|
|
84 |
|
|
|
85 |
// Fetch no earlier than 15 minutes from now.
|
|
|
86 |
// request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
|
|
|
87 |
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
|
|
|
88 |
|
|
|
89 |
// Try
|
|
|
90 |
// e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateExpirationForTaskWithIdentifier:@"com.mikasoftware.BGTaskSample.Refresh"]
|
|
|
91 |
|
|
|
92 |
// e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.mikasoftware.BGTaskSample.Refresh"]
|
|
|
93 |
|
|
|
94 |
do {
|
|
|
95 |
try BGTaskScheduler.shared.submit(request)
|
|
|
96 |
} catch {
|
|
|
97 |
print("Could not schedule app refresh: \(error)")
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
func scheduleProcessing() {
|
|
|
102 |
let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
|
|
|
103 |
|
|
|
104 |
// Fetch no earlier than 15 minutes from now.
|
|
|
105 |
// request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
|
|
|
106 |
request.earliestBeginDate = Date(timeIntervalSinceNow: 1)
|
|
|
107 |
request.requiresNetworkConnectivity = true
|
|
|
108 |
request.requiresExternalPower = false
|
|
|
109 |
|
|
|
110 |
// e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.mikasoftware.BGTaskSample.Processing"]
|
|
|
111 |
|
|
|
112 |
do {
|
|
|
113 |
try BGTaskScheduler.shared.submit(request)
|
|
|
114 |
} catch {
|
|
|
115 |
print("Could not schedule processing: \(error)")
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
func handleProcessingTask(task: BGProcessingTask) {
|
|
|
120 |
// Create an operation that performs the main part of the background task.
|
|
|
121 |
let operation = SampleOperation(text: "Hello world! v2")
|
|
|
122 |
|
|
|
123 |
// Provide the background task with an expiration handler that cancels the operation.
|
|
|
124 |
task.expirationHandler = {
|
|
|
125 |
operation.cancel()
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// Inform the system that the background task is complete
|
|
|
129 |
// when the operation completes.
|
|
|
130 |
operation.completionBlock = {
|
|
|
131 |
task.setTaskCompleted(success: !operation.isCancelled)
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// Start the operation.
|
|
|
135 |
let queue = OperationQueue()
|
|
|
136 |
|
|
|
137 |
queue.addOperation(operation)
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
func handleAppRefresh(task: BGAppRefreshTask) {
|
|
|
141 |
// Schedule a new refresh task.
|
|
|
142 |
scheduleAppRefresh()
|
|
|
143 |
|
|
|
144 |
// Create an operation that performs the main part of the background task.
|
|
|
145 |
let operation = SampleOperation(text: "Hello world!")
|
|
|
146 |
|
|
|
147 |
// Provide the background task with an expiration handler that cancels the operation.
|
|
|
148 |
task.expirationHandler = {
|
|
|
149 |
operation.cancel()
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
// Inform the system that the background task is complete
|
|
|
153 |
// when the operation completes.
|
|
|
154 |
operation.completionBlock = {
|
|
|
155 |
task.setTaskCompleted(success: !operation.isCancelled)
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
// Start the operation.
|
|
|
159 |
let queue = OperationQueue()
|
|
|
160 |
|
|
|
161 |
queue.addOperation(operation)
|
|
|
162 |
}
|
|
|
163 |
}
|