Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 1 | Rev 11 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 8
Línea 11... Línea 11...
11
 
11
 
12
@main
12
@main
13
struct TwoGetSkillsApp: App {
13
struct TwoGetSkillsApp: App {
14
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
14
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
-
 
15
    @Environment(\.scenePhase) var scenePhase
-
 
16
    
-
 
17
    @ObservedObject var syncForeground = SyncForegroundObservableObject()
Línea 15... Línea 18...
15
    @Environment(\.scenePhase) var scenePhase
18
    private let syncAdapter = SyncAdapter()
Línea 16... Línea 19...
16
   
19
   
17
    init() {
20
    init() {
Línea 34... Línea 37...
34
      
37
      
35
      let database = Database.sharedInstance
38
      let database = Database.sharedInstance
36
      if database.open() != nil {
39
      if database.open() != nil {
37
            database.createTables()
40
            database.createTables()
38
      }
-
 
39
        
41
      }
40
        
-
 
41
        
42
 
42
      registerBGTasksScheduler()
-
 
-
 
43
      registerBGTasksScheduler()
-
 
44
      scheduleProcess()
43
        
45
      scheduleRefresh()
Línea 44... Línea 46...
44
    }
46
    }
Línea 45... Línea 47...
45
    
47
    
46
    var body: some Scene {
-
 
47
 
48
    var body: some Scene {
48
        WindowGroup {
-
 
49
            
-
 
50
            MainView()
-
 
51
 
-
 
52
            
49
 
53
            
50
        WindowGroup {
54
            
51
            MainView()
55
        } .onChange(of: scenePhase) { newScenePhase in
52
        } .onChange(of: scenePhase) { newScenePhase in
-
 
53
            switch newScenePhase {
-
 
54
                case .active:
-
 
55
                    print("App is active")
-
 
56
                    syncForeground.timer.invalidate()
56
            switch newScenePhase {
57
                    syncForeground.timer = Timer.scheduledTimer(timeInterval: 30.0, target: syncAdapter, selector: #selector(syncAdapter.updateTimer), userInfo: nil, repeats: true)
57
            case .active:
58
                    break
-
 
59
                    
-
 
60
                case .inactive:
-
 
61
                  print("App is inactive")
58
              print("App is active")
62
                    syncForeground.timer.invalidate()
59
            case .inactive:
63
                    break
-
 
64
                    
-
 
65
                case .background:
-
 
66
                  print("App is in background")
60
              print("App is inactive")
67
                    syncForeground.timer.invalidate()
61
            case .background:
68
                    break
-
 
69
 
-
 
70
                @unknown default:
62
              print("App is in background")
71
                    print("Oh - interesting: I received an unexpected new value.")
63
            @unknown default:
72
                    syncForeground.timer.invalidate()
64
              print("Oh - interesting: I received an unexpected new value.")
73
                    break
Línea 65... Línea 74...
65
            }
74
                }
66
          }
75
          }
67
    }
76
    }
68
    
77
    
-
 
78
    func registerBGTasksScheduler() {
69
    func registerBGTasksScheduler() {
79
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
70
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
80
                 self.handleRefresh(task: task as! BGAppRefreshTask)
71
                 self.handleAppRefresh(task: task as! BGAppRefreshTask)
-
 
72
            }
-
 
73
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_PROCESS, using: nil) { task in
81
        }
-
 
82
        
-
 
83
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_PROCESS, using: nil) { task in
-
 
84
                 self.handleProcessTask(task: task as! BGProcessingTask)
-
 
85
        }
-
 
86
 
-
 
87
    }
Línea 74... Línea -...
74
                 self.handleProcessingTask(task: task as! BGProcessingTask)
-
 
75
            }
-
 
76
 
-
 
Línea 77... Línea 88...
77
        }
88
        
78
        
89
    func scheduleRefresh() {
79
        func scheduleAppRefresh() {
90
        let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
80
            let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
91
        request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
81
            request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
-
 
82
 
92
        
-
 
93
 
Línea 83... Línea 94...
83
            
94
            
84
            do {
95
        do {
85
                try BGTaskScheduler.shared.submit(request)
96
             try BGTaskScheduler.shared.submit(request)
86
            } catch {
97
        } catch {
87
                print("Could not schedule app refresh: \(error)")
98
            print("Could not schedule app refresh: \(error)")
88
            }
99
        }
89
        }
100
    }
90
        
101
        
91
        func scheduleProcessing() {
102
    func scheduleProcess() {
92
            let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
103
        let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
93
            request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
-
 
94
            request.requiresNetworkConnectivity = true
104
        request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
-
 
105
        request.requiresNetworkConnectivity = true
Línea 95... Línea 106...
95
            request.requiresExternalPower = false
106
        request.requiresExternalPower = false
96
 
-
 
97
           do {
-
 
98
              try BGTaskScheduler.shared.submit(request)
107
 
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.
-
 
Línea 119... Línea -...
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
            
108
        do {
131
 
-
 
132
           // Schedule a new refresh task.
-
 
133
           scheduleAppRefresh()
-
 
134
 
-
 
135
           // Create an operation that performs the main part of the background task.
109
            try BGTaskScheduler.shared.submit(request)
136
           let operation = SampleOperation(text: "handleAppRefresh")
-
 
137
           
110
        } catch {
138
           // Provide the background task with an expiration handler that cancels the operation.
-
 
139
           task.expirationHandler = {
-
 
140
              operation.cancel()
-
 
Línea 141... Línea 111...
141
           }
111
            print("Could not schedule processing: \(error)")
-
 
112
        }
-
 
113
    }
-
 
114
        
-
 
115
    func handleProcessTask(task: BGProcessingTask) {
-
 
116
        scheduleProcess()
-
 
117
        
-
 
118
        let syncAdapter = SyncAdapter()
-
 
119
        syncAdapter.sync { success in
-
 
120
        }
142
 
121
 
-
 
122
        task.expirationHandler = {
-
 
123
            task.setTaskCompleted(success: false)
143
           // Inform the system that the background task is complete
124
         }
Línea -... Línea 125...
-
 
125
        task.setTaskCompleted(success: true)
-
 
126
    }
-
 
127
        
-
 
128
    func handleRefresh(task: BGAppRefreshTask) {
-
 
129
        scheduleRefresh()