Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 8 | 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
   
    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
      
      let database = Database.sharedInstance
      if database.open() != nil {
            database.createTables()
      }
        
        
        
      registerBGTasksScheduler()
        
    }
    
    var body: some Scene {

        WindowGroup {
            
            MainView()
 
            
            
            
        } .onChange(of: scenePhase) { newScenePhase in
            switch newScenePhase {
            case .active:
              print("App is active")
            case .inactive:
              print("App is inactive")
            case .background:
              print("App is in background")
            @unknown default:
              print("Oh - interesting: I received an unexpected new value.")
            }
          }
    }
    
    func registerBGTasksScheduler() {
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
                 self.handleAppRefresh(task: task as! BGAppRefreshTask)
            }
        BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_PROCESS, using: nil) { task in
                 self.handleProcessingTask(task: task as! BGProcessingTask)
            }

        }
        
        func scheduleAppRefresh() {
            let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
            request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)

            
            do {
                try BGTaskScheduler.shared.submit(request)
            } catch {
                print("Could not schedule app refresh: \(error)")
            }
        }
        
        func scheduleProcessing() {
            let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
            request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
            request.requiresNetworkConnectivity = true
            request.requiresExternalPower = false

           do {
              try BGTaskScheduler.shared.submit(request)
           } catch {
              print("Could not schedule processing: \(error)")
           }
        }
        
        func handleProcessingTask(task: BGProcessingTask) {
            
            // Schedule a new refresh task.
            scheduleProcessing()

            // Create an operation that performs the main part of the background task.
            let operation = SampleOperation(text: "handleProcessingTask")
            
            // Provide the background task with an expiration handler that cancels the operation.
            task.expirationHandler = {
               operation.cancel()
            }

            // Inform the system that the background task is complete
            // when the operation completes.
            operation.completionBlock = {
               task.setTaskCompleted(success: !operation.isCancelled)
            }

            // Start the operation.
             let queue = OperationQueue()

             queue.addOperation(operation)
        }
        
        func handleAppRefresh(task: BGAppRefreshTask) {
            

           // Schedule a new refresh task.
           scheduleAppRefresh()

           // Create an operation that performs the main part of the background task.
           let operation = SampleOperation(text: "handleAppRefresh")
           
           // Provide the background task with an expiration handler that cancels the operation.
           task.expirationHandler = {
              operation.cancel()
           }

           // Inform the system that the background task is complete
           // when the operation completes.
           operation.completionBlock = {
              task.setTaskCompleted(success: !operation.isCancelled)
           }

           // Start the operation.
            let queue = OperationQueue()

            queue.addOperation(operation)
         }
}