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 SwiftUIimport BackgroundTasks@mainstruct TwoGetSkillsApp: App {@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate@Environment(\.scenePhase) var scenePhaseinit() {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 = coloredAppearanceUINavigationBar.appearance().compactAppearance = coloredAppearanceUINavigationBar.appearance().scrollEdgeAppearance = coloredAppearanceUINavigationBar.appearance().tintColor = UIColor(Color("color_app_bar_foreground"))UIPageControl.appearance().currentPageIndicatorTintColor = .systemBlueUIPageControl.appearance().pageIndicatorTintColor = .systemGray2let database = Database.sharedInstanceif database.open() != nil {database.createTables()}registerBGTasksScheduler()}var body: some Scene {WindowGroup {MainView()} .onChange(of: scenePhase) { newScenePhase inswitch 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 inself.handleAppRefresh(task: task as! BGAppRefreshTask)}BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_PROCESS, using: nil) { task inself.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 = truerequest.requiresExternalPower = falsedo {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)}}