Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Rev 12 | Rev 18 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

//
//  twogetskillsApp.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 1/12/22.
//

import SwiftUI
import BackgroundTasks


@main
struct TwoGetSkillsApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    @Environment(\.scenePhase) var scenePhase
    
    
    @ObservedObject var syncForeground = SyncForegroundObservableObject()
    private let syncAdapter = SyncAdapter()
    private var appData = AppData.sharedInstance
    
    init() {
      let coloredAppearance = UINavigationBarAppearance()
      coloredAppearance.configureWithOpaqueBackground()
      coloredAppearance.backgroundColor = UIColor(Color("color_app_bar_backgroud"))
      coloredAppearance.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
      coloredAppearance.largeTitleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
      coloredAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]

      UINavigationBar.appearance().standardAppearance = coloredAppearance
      UINavigationBar.appearance().compactAppearance = coloredAppearance
      UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance

      UINavigationBar.appearance().tintColor = UIColor(Color("color_app_bar_foreground"))
        
      UIPageControl.appearance().currentPageIndicatorTintColor = .systemBlue
      UIPageControl.appearance().pageIndicatorTintColor = .systemGray2
      
        


        if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
            
            let fileURL = dir.appendingPathComponent(Config.DEVICE_UUID_FILENAME)
            if !FileManager.default.fileExists(atPath: fileURL.path) {
                if let new_uuid = UIDevice.current.identifierForVendor?.uuidString {
                    do {
                        try new_uuid.write(to: fileURL, atomically: false, encoding: .utf8)
                        
                        var sync = SyncModel()
                        sync.data = new_uuid
                        sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
                        
                        let syncDao = SyncDao()
                        if syncDao.insert(record: sync) > 0 {
                            appData.deviceUuid = new_uuid
                            appData.save()
                            
                            let syncAdapter = SyncAdapter()
                            syncAdapter.sync(isForeground: true) {
                                success in
                            }
                        }
                    }
                    catch {/* error handling here */}
                }
            }
        }
    }
    
    var body: some Scene {

        WindowGroup {
            MainView()
        }

        .onChange(of: scenePhase) { newScenePhase in
            switch newScenePhase {
                case .active:
                    print("App is active")
                    syncForeground.timer.invalidate()
                    syncForeground.timer = Timer.scheduledTimer(timeInterval: 30.0, target: syncAdapter, selector: #selector(syncAdapter.updateTimerForeground), userInfo: nil, repeats: true)
                    break
                    
                case .inactive:
                  print("App is inactive")
                    syncForeground.timer.invalidate()
                    break
                    
                case .background:
                  print("App is in background")
                    syncForeground.timer.invalidate()
                    break

                @unknown default:
                    print("Oh - interesting: I received an unexpected new value.")
                    syncForeground.timer.invalidate()
                    break
                }
          }
    }
    
    
}


class SyncForegroundObservableObject: ObservableObject
{
    @Published var timer : Timer = Timer()
}