-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathIronApp.swift
87 lines (78 loc) · 3.85 KB
/
IronApp.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// IronApp.swift
// IronWatch WatchKit Extension
//
// Created by Karim Abou Zeid on 27.05.22.
//
import SwiftUI
@main
struct IronApp: App {
@WKApplicationDelegateAdaptor private var appDelegate: AppDelegate
@SceneBuilder var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
}
}
import HealthKit
import os.log
class AppDelegate: NSObject, WKApplicationDelegate {
private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "watch")
func applicationDidFinishLaunching() {
// Perform any final initialization of your application.
Self.logger.log("Checking whether active workout session can be recovered")
handleActiveWorkoutRecovery() // somehow this isn't automatically called by the system?! last checked watchOS 8
PhoneConnectionManager.shared.activateSession()
_ = NotificationManager.shared
}
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
// Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
for task in backgroundTasks {
// Use a switch statement to check the task type
switch task {
case let backgroundTask as WKApplicationRefreshBackgroundTask:
// Be sure to complete the background task once you’re done.
backgroundTask.setTaskCompletedWithSnapshot(false)
case let snapshotTask as WKSnapshotRefreshBackgroundTask:
// Snapshot tasks have a unique completion call, make sure to set your expiration date
snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
// Be sure to complete the connectivity task once you’re done.
connectivityTask.setTaskCompletedWithSnapshot(false)
case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
// Be sure to complete the URL session task once you’re done.
urlSessionTask.setTaskCompletedWithSnapshot(false)
case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:
// Be sure to complete the relevant-shortcut task once you're done.
relevantShortcutTask.setTaskCompletedWithSnapshot(false)
case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:
// Be sure to complete the intent-did-run task once you're done.
intentDidRunTask.setTaskCompletedWithSnapshot(false)
default:
// make sure to complete unhandled task types
task.setTaskCompletedWithSnapshot(false)
}
}
}
func handleActiveWorkoutRecovery() {
Self.logger.log("Recovering active workout session")
WorkoutSessionManager.healthStore.recoverActiveWorkoutSession { workoutSession, error in
guard let workoutSession = workoutSession else {
if let error = error {
Self.logger.error("Could not recover active workout session: \(error.localizedDescription)")
} else {
Self.logger.info("No active workout session to recover")
}
return
}
Self.logger.info("Successfully recovered active workout session")
WorkoutSessionManagerStore.shared.recoverWorkoutSession(workoutSession: workoutSession)
}
}
/// from `HKHealthStore.startWatchApp`
func handle(_ workoutConfiguration: HKWorkoutConfiguration) {
PhoneConnectionManager.shared.handlePrepareWorkoutSession(workoutConfiguration)
}
}