Autoría | Ultima modificación | Ver Log |
//// twogetskillsApp.swift// twogetskills//// Created by Efrain Yanez Recanatini on 1/12/22.//import SwiftUIimport BackgroundTasks@mainstruct TwoGetSkillApp: App {@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate@Environment(\.scenePhase) var scenePhase//@State public var idxFragment : Int = 0private let colorAppBar = UIColor(hex: Config.COLOR_APP_BAR)private let colorAppTextView = UIColor(hex: Config.COLOR_APP_TEXT_VIEW_TITLE)init() {let coloredAppearance = UINavigationBarAppearance()coloredAppearance.configureWithOpaqueBackground()coloredAppearance.backgroundColor = colorAppBarcoloredAppearance.titleTextAttributes = [.foregroundColor: colorAppTextView ?? UIColor.systemBlue]coloredAppearance.largeTitleTextAttributes = [.foregroundColor: colorAppTextView ?? UIColor.systemBlue]coloredAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor:colorAppTextView ?? UIColor.systemBlue]UINavigationBar.appearance().standardAppearance = coloredAppearanceUINavigationBar.appearance().compactAppearance = coloredAppearanceUINavigationBar.appearance().scrollEdgeAppearance = coloredAppearanceUINavigationBar.appearance().tintColor = colorAppTextView ?? .systemBlueUIPageControl.appearance().currentPageIndicatorTintColor = .systemBlueUIPageControl.appearance().pageIndicatorTintColor = .systemGray2}var body: some Scene {WindowGroup {NavigationView {let preference = Preference.sharedInstanceif preference.userUuid.isEmpty {IntroView()} else {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 registerBGTaskScheduler() {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)// Fetch no earlier than 15 minutes from now.// request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)// Try// e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateExpirationForTaskWithIdentifier:@"com.mikasoftware.BGTaskSample.Refresh"]// e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.mikasoftware.BGTaskSample.Refresh"]do {try BGTaskScheduler.shared.submit(request)} catch {print("Could not schedule app refresh: \(error)")}}func scheduleProcessing() {let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)// Fetch no earlier than 15 minutes from now.// request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)request.earliestBeginDate = Date(timeIntervalSinceNow: 1)request.requiresNetworkConnectivity = truerequest.requiresExternalPower = false// e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.mikasoftware.BGTaskSample.Processing"]do {try BGTaskScheduler.shared.submit(request)} catch {print("Could not schedule processing: \(error)")}}func handleProcessingTask(task: BGProcessingTask) {// Create an operation that performs the main part of the background task.let operation = SampleOperation(text: "Hello world! v2")// 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: "Hello world!")// 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)}}