Autoría | Ultima modificación | Ver Log |
//
// twogetskillsApp.swift
// twogetskills
//
// Created by Efrain Yanez Recanatini on 1/12/22.
//
import SwiftUI
import BackgroundTasks
@main
struct TwoGetSkillApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
@Environment(\.scenePhase) var scenePhase
//@State public var idxFragment : Int = 0
private 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 = colorAppBar
coloredAppearance.titleTextAttributes = [.foregroundColor: colorAppTextView ?? UIColor.systemBlue]
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: colorAppTextView ?? UIColor.systemBlue]
coloredAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor:colorAppTextView ?? UIColor.systemBlue]
UINavigationBar.appearance().standardAppearance = coloredAppearance
UINavigationBar.appearance().compactAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
UINavigationBar.appearance().tintColor = colorAppTextView ?? .systemBlue
UIPageControl.appearance().currentPageIndicatorTintColor = .systemBlue
UIPageControl.appearance().pageIndicatorTintColor = .systemGray2
}
var body: some Scene {
WindowGroup {
NavigationView {
let preference = Preference.sharedInstance
if preference.userUuid.isEmpty {
IntroView()
} else {
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 registerBGTaskScheduler() {
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)
// 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 = true
request.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)
}
}