DiveRuntime.swift (3260B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import Foundation 3 import WatchKit 4 5 /// Apple's dive session (`WKExtendedRuntimeSession`, `underwater-depth` mode): keeps the app in the 6 /// foreground for the whole dive and locks the touch screen (Water Lock). The diver leaves it with a long 7 /// press on the crown, which ends the session; it also expires after 10 min out of the water. When the app 8 /// does not start one, the system starts one below 1 m and hands it to the app delegate. 9 @MainActor 10 final class DiveRuntime: NSObject, @preconcurrency WKExtendedRuntimeSessionDelegate { 11 private var session: WKExtendedRuntimeSession? 12 /// State change (true = session running, hence screen locked), with a text for the diagnostics. 13 var onChange: ((Bool, String) -> Void)? 14 15 var isRunning: Bool { session?.state == .running } 16 17 func start() { 18 guard session == nil || session?.state == .invalid else { return } 19 let session = WKExtendedRuntimeSession() 20 session.delegate = self 21 self.session = session 22 session.start() 23 onChange?(false, String(localized: "requested")) 24 } 25 26 /// Session started by the system on submersion. 27 func adopt(_ session: WKExtendedRuntimeSession) { 28 session.delegate = self 29 self.session = session 30 onChange?(session.state == .running, String(localized: "started by the system")) 31 } 32 33 func stop() { 34 session?.invalidate() 35 session = nil 36 } 37 38 func extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession) { 39 onChange?(true, String(localized: "running")) 40 } 41 42 func extendedRuntimeSessionWillExpire(_ extendedRuntimeSession: WKExtendedRuntimeSession) { 43 onChange?(true, String(localized: "about to expire")) 44 } 45 46 func extendedRuntimeSession(_ extendedRuntimeSession: WKExtendedRuntimeSession, 47 didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, 48 error: (any Error)?) { 49 guard extendedRuntimeSession === session || session == nil else { return } 50 session = nil 51 let why: String 52 switch reason { 53 case .none: why = String(localized: "ended") 54 case .sessionInProgress: why = String(localized: "refused: another session is running") 55 case .expired: why = String(localized: "expired") 56 case .resignedFrontmost: why = String(localized: "ended: the app left the foreground") 57 case .suppressedBySystem: why = String(localized: "refused by the system") 58 case .error: why = String(localized: "error") 59 @unknown default: why = String(localized: "ended (reason \(reason.rawValue))") 60 } 61 // Domain and code: the localized text alone does not tell the errors apart. 62 onChange?(false, error.map { "\(why) (\(($0 as NSError).domain) \(($0 as NSError).code): \($0.localizedDescription))" } ?? why) 63 } 64 } 65 66 /// Receives the session the system starts by itself when the watch goes below 1 m. 67 final class AppDelegate: NSObject, WKApplicationDelegate { 68 func handle(_ extendedRuntimeSession: WKExtendedRuntimeSession) { 69 Task { @MainActor in DiveSession.current?.runtime.adopt(extendedRuntimeSession) } 70 } 71 }