You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LeCountdown/LeCountdown/LeCountdownApp.swift

90 lines
2.8 KiB

//
// LeCountdownApp.swift
// LeCountdown
//
// Created by Laurent Morvillier on 20/01/2023.
//
import SwiftUI
import CoreData
import BackgroundTasks
enum BGTaskIdentifier : String {
case refresh = "com.staxriver.lecountdown.refresh"
}
@main
struct LeCountdownApp: App {
let persistenceController = PersistenceController.shared
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
init() {
UITabBar.appearance().backgroundColor = UIColor(white: 0.96, alpha: 1.0)
}
var body: some Scene {
WindowGroup {
TabView {
ContentView<Countdown>()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.tabItem { Label("Countdown", systemImage: "timer") }
ContentView<Stopwatch>()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.tabItem { Label("Stopwatch", systemImage: "stopwatch") }
ContentView<Alarm>()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.tabItem { Label("Alarm", systemImage: "alarm") }
RecordsView().environment(\.managedObjectContext, persistenceController.container.viewContext)
.tabItem { Label("Stats", systemImage: "chart.bar.fill") }
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
self._willEnterForegroundNotification()
}.onAppear {
self._onAppear()
}
}
}
fileprivate func _willEnterForegroundNotification() {
Conductor.maestro.cleanup()
}
fileprivate func _onAppear() {
self._registerBackgroundRefreshes()
// Task {
// for s in Sound.allCases {
// do {
// let d = try await s.duration()
// print("\(s) duration = \(d)")
// } catch {
// print("error = \(error)")
// }
// }
// }
}
fileprivate func _registerBackgroundRefreshes() {
BGTaskScheduler.shared.register(forTaskWithIdentifier: BGTaskIdentifier.refresh.rawValue, using: nil) { task in
self._handleAppRefresh(task: task as! BGAppRefreshTask)
}
}
fileprivate func _handleAppRefresh(task: BGAppRefreshTask) {
print("_handleAppRefresh = \(task.description)")
task.expirationHandler = {
print("expired")
}
DispatchQueue.main.async {
Conductor.maestro.updateLiveActivities()
task.setTaskCompleted(success: true)
}
}
}