Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Rev 19 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 19 Rev 21
Línea 8... Línea 8...
8
import Foundation
8
import Foundation
Línea 9... Línea 9...
9
 
9
 
10
import Foundation
10
import Foundation
11
import Alamofire
11
import Alamofire
-
 
12
import SwiftyJSON
-
 
13
import SwiftUI
Línea 12... Línea 14...
12
import SwiftyJSON
14
import TTGSnackbar
13
 
15
 
14
class SyncAdapter
16
class SyncAdapter
Línea 15... Línea 17...
15
{
17
{
16
    private var appData = AppData.sharedInstance
18
    private var appData = Environment(\.appData).wrappedValue
Línea 339... Línea 341...
339
 
341
 
Línea -... Línea 342...
-
 
342
    func registerFcm(record: SyncModel) {
340
    func registerFcm(record: SyncModel) {
343
 
341
 
344
      
342
      
345
        
343
        
346
        
344
        let deviceUuid = appData.deviceUuid
347
        let deviceUuid = appData.deviceUuid
345
        let parameters = [
348
        let parameters = [
346
            Constants.POST_FCM_FIELD_APPLICATION_ID: "\(Config.APPLICATION_ID)",
349
            Constants.POST_FCM_FIELD_APPLICATION_ID: "\(Config.APPLICATION_ID)",
347
            Constants.POST_FCM_FIELD_VARIANT_ID: "\(Config.VARIANT_ID)",
350
            Constants.POST_FCM_FIELD_VARIANT_ID: "\(Config.VARIANT_ID)",
-
 
351
            Constants.POST_FCM_FIELD_DEVICE_UUID: "\(deviceUuid)",
Línea 348... Línea 352...
348
            Constants.POST_FCM_FIELD_DEVICE_UUID: "\(deviceUuid)",
352
            Constants.POST_FCM_FIELD_TOKEN: "\(record.data)",
349
            Constants.POST_FCM_FIELD_TOKEN: "\(record.data)",
353
            Constants.POST_FCM_FIELD_SYNC_ID: "\(record.id)"
350
            Constants.POST_FCM_FIELD_SYNC_ID: "\(record.id)"
354
        ]
Línea 351... Línea 355...
351
        ]
355
        print("Register FCM parameters: \(parameters)");
Línea 352... Línea 356...
352
        
356
        
Línea 353... Línea 357...
353
        let headers: HTTPHeaders = [
357
        let headers: HTTPHeaders = [
354
            .accept(Constants.HTTP_HEADER_ACCEPT)
358
            .accept(Constants.HTTP_HEADER_ACCEPT)
355
        ]
359
        ]
-
 
360
            
-
 
361
        AF.request(Config.URL_FCM, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{response in
-
 
362
            
-
 
363
            print("Register FCM Request")
356
            
364
            
357
        AF.request(Config.URL_FCM, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{response in
365
            switch response.result {
358
            
366
                case .success:
-
 
367
                    let result = try? JSON(data: response.data!)
-
 
368
                    if result?["success"] ?? "" != false {
-
 
369
 
359
            print("Send FCM")
370
                        print("Register FCM success = true ")
360
            
371
 
361
            switch response.result {
372
                        let sync_id = Int(result?["data"]["sync_id"].stringValue ?? "0") ?? 0
362
                case .success:
373
                        if sync_id > 0 {
363
                    let result = try? JSON(data: response.data!)
374
                            self.syncDao.remove(id: sync_id)
364
                    let sync_id = Int(result?["data"]["sync_id"].stringValue ?? "0") ?? 0
375
                        }
365
                    if sync_id > 0 {
376
                    } else {
366
                        self.syncDao.remove(id: sync_id)
377
                        print("Register FCM success = false : \(result?["data"]))")
Línea 367... Línea 378...
367
                    }
378
                    }
368
                    break
379
                    break
Línea -... Línea 380...
-
 
380
                case .failure:
369
                case .failure:
381
                    print("Register  FCM failura : \(String(describing: Error.self))")
370
                   // print("JSON = \(String(describing: Error.self))")
382
                    break
Línea 392... Línea 404...
392
            Constants.POST_DEVICE_FIELD_VERSION: "\(version)",
404
            Constants.POST_DEVICE_FIELD_VERSION: "\(version)",
393
            Constants.POST_DEVICE_FIELD_MODEL: "\(model)",
405
            Constants.POST_DEVICE_FIELD_MODEL: "\(model)",
394
            Constants.POST_DEVICE_FIELD_PLATFORM: "Iphone",
406
            Constants.POST_DEVICE_FIELD_PLATFORM: "Iphone",
395
            Constants.POST_DEVICE_FIELD_SYNC_ID: "\(record.id)"
407
            Constants.POST_DEVICE_FIELD_SYNC_ID: "\(record.id)"
396
        ]
408
        ]
-
 
409
        print("Register Device parmameters: \(parameters)");
Línea 397... Línea 410...
397
        
410
        
398
        let headers: HTTPHeaders = [
411
        let headers: HTTPHeaders = [
399
            .accept(Constants.HTTP_HEADER_ACCEPT)
412
            .accept(Constants.HTTP_HEADER_ACCEPT)
Línea 400... Línea 413...
400
        ]
413
        ]
Línea 401... Línea 414...
401
        
414
        
Línea 402... Línea 415...
402
        
415
        
Línea 403... Línea 416...
403
        print("URL DEVICE : \(Config.URL_DEVICE) ")
416
        print("Register Device URL : \(Config.URL_DEVICE) ")
404
        
417
        
405
        AF.request(Config.URL_DEVICE, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON {response in
418
        AF.request(Config.URL_DEVICE, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON {response in
406
            
419
            
407
            print("Send Device: ")
420
            print("Register Device Request")
408
            
421
            
Línea -... Línea 422...
-
 
422
            switch response.result {
-
 
423
                case .success:
409
            switch response.result {
424
                    let result = try? JSON(data: response.data!)
Línea 410... Línea 425...
410
                case .success:
425
                    if result?["success"] ?? "" != false {
411
                    let result = try? JSON(data: response.data!)
426
                        let aes = result?["data"]["aes"].stringValue ?? ""
412
                    if result?["success"] ?? "" != false {
427
                        let password = result?["data"]["password"].stringValue ?? ""
Línea 422... Línea 437...
422
                        
437
                        
423
                        let sync_id = Int(result?["data"]["sync_id"].stringValue ?? "0") ?? 0
438
                        let sync_id = Int(result?["data"]["sync_id"].stringValue ?? "0") ?? 0
424
                        if sync_id > 0 {
439
                        if sync_id > 0 {
425
                            self.syncDao.remove(id: sync_id)
440
                            self.syncDao.remove(id: sync_id)
-
 
441
                        }
-
 
442
                    } else {
426
                        }
443
                        print("Register Device success = false : \(result?["data"]))")
427
                    }
444
                    }
428
                break
445
                break
429
                case .failure:
446
                case .failure:
430
                    //print("JSON = \(String(describing: Error.self))")
447
                    print("Register Device Failure : \(String(describing: Error.self))")
431
                break
448
                break
432
            }
449
            }
433
        }
450
        }
434
    }
451
    }