Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

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

Rev 15 Rev 17
Línea 7... Línea 7...
7
 
7
 
8
import Foundation
8
import Foundation
9
import Firebase
9
import Firebase
10
import Messages
10
import Messages
-
 
11
import BackgroundTasks
Línea 11... Línea 12...
11
import BackgroundTasks
12
import SwiftUI
Línea 12... Línea 13...
12
 
13
 
13
//UIResponder, UIApplicationDelegate {
-
 
-
 
14
//UIResponder, UIApplicationDelegate {
14
 
15
 
15
class AppDelegate : NSObject, UIApplicationDelegate {
16
class AppDelegate : NSObject, UIApplicationDelegate {
16
    private let database = Database.sharedInstance
-
 
17
    private let gcmMessageIDKey = "gcm.message_id"
-
 
18
    private var appData = AppData.sharedInstance
17
 
-
 
18
    private let gcmMessageIDKey = "gcm.message_id"
Línea -... Línea 19...
-
 
19
    private var appData = AppData.sharedInstance
19
    private var syncAdapter = SyncAdapter()
20
    public static var orientationLock = UIInterfaceOrientationMask.portrait
20
    private var isSyncInProgress = false
21
    
21
    private static var orientationLock = UIInterfaceOrientationMask.portrait
22
 
Línea 22... Línea 23...
22
 
23
    
23
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
24
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
Línea -... Línea 25...
-
 
25
        return AppDelegate.orientationLock
-
 
26
    }
-
 
27
    
-
 
28
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
Línea 24... Línea 29...
24
        return AppDelegate.orientationLock
29
        [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
25
    }
-
 
26
    
-
 
Línea -... Línea 30...
-
 
30
 
-
 
31
        /*        let database = Database.sharedInstance
-
 
32
        if database.open() != nil  {
-
 
33
            database.createTables()
-
 
34
        }
-
 
35
        
-
 
36
        */
27
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
37
        
28
        [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
38
        /*
Línea 29... Línea 39...
29
 
39
        for  fontFamily in  UIFont.familyNames {
30
        
40
            for fontName in UIFont.fontNames(forFamilyName: fontFamily) {
-
 
41
                print("\(fontName)")
31
        database.open()
42
            }
Línea 32... Línea -...
32
        database.createTables()
-
 
33
        database.close()
-
 
34
        
-
 
35
        print("observador del UUID del dispositivo")
-
 
Línea 36... Línea 43...
36
        NotificationCenter.default.addObserver(self, selector: #selector(receivingNewDevice(n:)), name: .receivingNewDevice, object: nil)
43
        }*/
Línea 37... Línea 44...
37
 
44
        
Línea 75... Línea 82...
75
        
82
        
76
        return true
83
        return true
Línea 77... Línea 84...
77
    }
84
    }
-
 
85
    
78
    
86
    // Storage of the UID once the notification is received
Línea 79... Línea 87...
79
    // Storage of the UID once the notification is received
87
    /*
80
       @objc func receivingNewDevice(n: NSNotification){
88
     @objc func receivingNewDevice(n: NSNotification){
81
 
89
 
82
            print("receivingNewDevice")
90
            print("receivingNewDevice")
83
            if n.userInfo != nil{
91
            if n.userInfo != nil{
Línea 84... Línea -...
84
                if let new_uuid = n.userInfo?["uuid"]! as? String {
-
 
85
                    let old_uuid = appData.deviceUuid
-
 
86
                    if new_uuid != old_uuid {
92
                if let new_uuid = n.userInfo?["uuid"]! as? String {
87
                        
93
                    let old_uuid = appData.deviceUuid
88
                        db = database.open()
94
                    if new_uuid != old_uuid {
Línea 89... Línea 95...
89
                        if db != nil {
95
                        
90
                            var sync = SyncModel()
96
                        var sync = SyncModel()
91
                            sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
97
                        sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
92
                            sync.data = new_uuid
98
                        sync.data = new_uuid
Línea 93... Línea 99...
93
                            
99
                            
94
                            let syncDao = SyncDao(db : db)
100
                        let syncDao = SyncDao()
95
                            if syncDao.insert(record: sync) > 0 {
-
 
96
                                appData.deviceUuid = new_uuid
101
                        if syncDao.insert(record: sync) > 0 {
97
                                appData.save()
102
                            appData.deviceUuid = new_uuid
98
                                
-
 
99
                                syncAdapter.sync {
-
 
Línea 100... Línea 103...
100
                                        success in
103
                            appData.save()
101
                                }
104
                                
102
                            }
105
                            syncAdapter.sync(isForeground: true) {
103
                        }
106
                                success in
Línea 113... Línea 116...
113
            if n.userInfo != nil{
116
            if n.userInfo != nil{
114
                if let token = n.userInfo?["token"]! as? String {
117
                if let token = n.userInfo?["token"]! as? String {
115
                    if !token.isEmpty {
118
                    if !token.isEmpty {
116
                        if appData.fcmToken != token  {
119
                        if appData.fcmToken != token  {
Línea 117... Línea -...
117
                            
-
 
118
                            var db = database.open()
-
 
119
                            if db != nil {
120
                            
120
                                var sync = SyncModel()
121
                            var sync = SyncModel()
121
                                sync.data = token
122
                            sync.data = token
122
                                sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
-
 
123
                                
-
 
124
                                let syncDao = SyncDao(db: db)
-
 
125
                                
-
 
Línea -... Línea 123...
-
 
123
                            sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
-
 
124
                                
126
                                if syncDao.insert(record: sync) > 0 {
125
                            let syncDao = SyncDao()
127
                                
126
                            if syncDao.insert(record: sync) > 0 {
Línea 128... Línea 127...
128
                                    appData.fcmToken = token
127
                                appData.fcmToken = token
129
                                    appData.save()
128
                                appData.save()
130
                                    
-
 
131
                                    syncAdapter.sync {
129
                                    
132
                                            success in
130
                                syncAdapter.sync(isForeground: true) {
133
                                    }
-
 
134
                                }
131
                                    success in
135
                            }
-
 
136
                            database.close()
-
 
137
                            
-
 
138
                            
132
                                }
139
                            
133
                            }
140
                            
134
 
141
                        }
135
                        }
142
                   }
136
                   }
Línea 143... Línea 137...
143
               }
137
               }
144
           }
138
           }
Línea 145... Línea 139...
145
        }
139
        }*/
Línea 155... Línea 149...
155
        processNotification(userInfo: userInfo, isForeground: false)
149
        processNotification(userInfo: userInfo, isForeground: false)
156
        completionHandler(UIBackgroundFetchResult.newData)
150
        completionHandler(UIBackgroundFetchResult.newData)
157
    }
151
    }
Línea 158... Línea 152...
158
    
152
    
-
 
153
    func registerBGTasksScheduler() {
159
    func registerBGTasksScheduler() {
154
        print("background :  registerBGTasksScheduler");
160
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
155
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
161
                 self.handleRefreshTask(task: task as! BGAppRefreshTask)
156
                 self.handleRefreshTask(task: task as! BGAppRefreshTask)
Línea 162... Línea 157...
162
        }
157
        }
Línea 166... Línea 161...
166
        }
161
        }
Línea 167... Línea 162...
167
 
162
 
Línea 168... Línea 163...
168
    }
163
    }
-
 
164
        
169
        
165
    func scheduleRefresh() {
170
    func scheduleRefresh() {
166
        print("background: scheduleRefresh")
Línea 171... Línea 167...
171
        let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
167
        let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
172
        request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
168
        request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
173
        
169
        
174
 
170
 
175
            
171
            
176
        do {
172
        do {
Línea 177... Línea 173...
177
             try BGTaskScheduler.shared.submit(request)
173
             try BGTaskScheduler.shared.submit(request)
-
 
174
        } catch {
178
        } catch {
175
            print("background: Could not schedule app refresh: \(error)")
179
            print("Could not schedule app refresh: \(error)")
176
        }
180
        }
177
    }
181
    }
178
        
Línea 182... Línea 179...
182
        
179
    func scheduleProcess() {
183
    func scheduleProcess() {
180
        print("background: scheduleProcess")
184
        let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
181
        let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
185
        request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
182
        request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
186
        request.requiresNetworkConnectivity = true
183
        request.requiresNetworkConnectivity = true
187
        request.requiresExternalPower = false
184
        request.requiresExternalPower = false
Línea 188... Línea 185...
188
 
185
 
189
        do {
186
        do {
-
 
187
            try BGTaskScheduler.shared.submit(request)
Línea 190... Línea 188...
190
            try BGTaskScheduler.shared.submit(request)
188
        } catch {
191
        } catch {
189
            print("background: Could not schedule processing: \(error)")
192
            print("Could not schedule processing: \(error)")
190
        }
193
        }
191
    }
Línea 205... Línea 203...
205
    }
203
    }
Línea 206... Línea 204...
206
        
204
        
207
    func handleRefreshTask(task: BGAppRefreshTask) {
205
    func handleRefreshTask(task: BGAppRefreshTask) {
Línea 208... Línea -...
208
        scheduleRefresh()
-
 
-
 
206
        scheduleRefresh()
Línea 209... Línea 207...
209
        
207
        
210
        
208
        print("background: handleRefreshTask")
211
        
209
        
212
        let syncAdapter = SyncAdapter()
210
        let syncAdapter = SyncAdapter()
Línea 230... Línea 228...
230
 
228
 
Línea 231... Línea 229...
231
extension AppDelegate: MessagingDelegate {
229
extension AppDelegate: MessagingDelegate {
Línea 232... Línea 230...
232
    
230
    
233
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
231
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
234
 
232
 
Línea 235... Línea 233...
235
       
233
  
236
        let token = fcmToken ?? ""
234
        let token = fcmToken ?? ""
Línea 237... Línea 235...
237
        print( "MessagingDelegate didReceiveRegistrationToken: \(token)")
235
        print( "MessagingDelegate didReceiveRegistrationToken: \(token)")
238
        
236
        
Línea 280... Línea 278...
280
        let command = userInfo.index(forKey: "command") == nil ? "" : userInfo["command"] as? String ?? ""
278
        let command = userInfo.index(forKey: "command") == nil ? "" : userInfo["command"] as? String ?? ""
281
        let newCapsules = userInfo.index(forKey: "new_capsules") == nil ? 0 : Int(userInfo["new_capsules"] as? String ?? "") ?? 0
279
        let newCapsules = userInfo.index(forKey: "new_capsules") == nil ? 0 : Int(userInfo["new_capsules"] as? String ?? "") ?? 0
Línea 282... Línea 280...
282
 
280
 
-
 
281
    
-
 
282
        if command == "signout" {
-
 
283
            let userinfo = [
-
 
284
                "is_foreground" : (isForeground ? "1" : "0")
283
    
285
            ]
284
        if command == "signout" {
286
            
285
            NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_COMMAND_EXIT , object: self, userInfo: nil)
287
            NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_COMMAND_EXIT , object: self, userInfo: userinfo)
286
        }
288
        }
287
        else if command == "content-refresh" {
289
        else if command == "content-refresh" {
288
            let userinfo = [
290
            let userinfo = [
Línea 309... Línea 311...
309
 
311
 
Línea 310... Línea 312...
310
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
312
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Línea -... Línea 313...
-
 
313
 
-
 
314
        print( "didRegisterForRemoteNotificationsWithDeviceToken")
311
 
315
        
312
        print( "didRegisterForRemoteNotificationsWithDeviceToken")
316
    
313
        
317
        
Línea 314... Línea 318...
314
        let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
318
        let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
315
        let token = tokenParts.joined()
319
        let token = tokenParts.joined()
Línea 316... Línea 320...
316
            print("token: ", token)
320
            print("token: ", token)