Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 8 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
    init() {
18
 
19
      let coloredAppearance = UINavigationBarAppearance()
20
      coloredAppearance.configureWithOpaqueBackground()
21
      coloredAppearance.backgroundColor = UIColor(Color("color_app_bar_backgroud"))
22
      coloredAppearance.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
23
      coloredAppearance.largeTitleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
24
      coloredAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
25
 
26
      UINavigationBar.appearance().standardAppearance = coloredAppearance
27
      UINavigationBar.appearance().compactAppearance = coloredAppearance
28
      UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
29
 
30
      UINavigationBar.appearance().tintColor = UIColor(Color("color_app_bar_foreground"))
31
 
32
      UIPageControl.appearance().currentPageIndicatorTintColor = .systemBlue
33
      UIPageControl.appearance().pageIndicatorTintColor = .systemGray2
34
 
35
      let database = Database.sharedInstance
36
      if database.open() != nil {
37
            database.createTables()
38
      }
39
 
40
 
41
 
42
      registerBGTasksScheduler()
43
 
44
    }
45
 
46
    var body: some Scene {
47
 
48
        WindowGroup {
49
 
50
            MainView()
51
 
52
 
53
 
54
 
55
        } .onChange(of: scenePhase) { newScenePhase in
56
            switch newScenePhase {
57
            case .active:
58
              print("App is active")
59
            case .inactive:
60
              print("App is inactive")
61
            case .background:
62
              print("App is in background")
63
            @unknown default:
64
              print("Oh - interesting: I received an unexpected new value.")
65
            }
66
          }
67
    }
68
 
69
    func registerBGTasksScheduler() {
70
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
71
                 self.handleAppRefresh(task: task as! BGAppRefreshTask)
72
            }
73
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_PROCESS, using: nil) { task in
74
                 self.handleProcessingTask(task: task as! BGProcessingTask)
75
            }
76
 
77
        }
78
 
79
        func scheduleAppRefresh() {
80
            let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
81
            request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
82
 
83
 
84
            do {
85
                try BGTaskScheduler.shared.submit(request)
86
            } catch {
87
                print("Could not schedule app refresh: \(error)")
88
            }
89
        }
90
 
91
        func scheduleProcessing() {
92
            let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
93
            request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
94
            request.requiresNetworkConnectivity = true
95
            request.requiresExternalPower = false
96
 
97
           do {
98
              try BGTaskScheduler.shared.submit(request)
99
           } catch {
100
              print("Could not schedule processing: \(error)")
101
           }
102
        }
103
 
104
        func handleProcessingTask(task: BGProcessingTask) {
105
 
106
            // Schedule a new refresh task.
107
            scheduleProcessing()
108
 
109
            // Create an operation that performs the main part of the background task.
110
            let operation = SampleOperation(text: "handleProcessingTask")
111
 
112
            // Provide the background task with an expiration handler that cancels the operation.
113
            task.expirationHandler = {
114
               operation.cancel()
115
            }
116
 
117
            // Inform the system that the background task is complete
118
            // when the operation completes.
119
            operation.completionBlock = {
120
               task.setTaskCompleted(success: !operation.isCancelled)
121
            }
122
 
123
            // Start the operation.
124
             let queue = OperationQueue()
125
 
126
             queue.addOperation(operation)
127
        }
128
 
129
        func handleAppRefresh(task: BGAppRefreshTask) {
130
 
131
 
132
           // Schedule a new refresh task.
133
           scheduleAppRefresh()
134
 
135
           // Create an operation that performs the main part of the background task.
136
           let operation = SampleOperation(text: "handleAppRefresh")
137
 
138
           // Provide the background task with an expiration handler that cancels the operation.
139
           task.expirationHandler = {
140
              operation.cancel()
141
           }
142
 
143
           // Inform the system that the background task is complete
144
           // when the operation completes.
145
           operation.completionBlock = {
146
              task.setTaskCompleted(success: !operation.isCancelled)
147
           }
148
 
149
           // Start the operation.
150
            let queue = OperationQueue()
151
 
152
            queue.addOperation(operation)
153
         }
154
}
155