Synchronize selected TOTP entries to Apple Watch (#55)
This commit is contained in:
@@ -1,7 +1,54 @@
|
||||
import SwiftUI
|
||||
import WatchConnectivity
|
||||
|
||||
private final class WatchSnapshotTransport: NSObject, ObservableObject, WCSessionDelegate {
|
||||
private static let snapshotKey = "de.rfc1437.ironstorage.watch.snapshot"
|
||||
private static let deliveredReceiptKey = "de.rfc1437.ironstorage.watch.delivered"
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
guard WCSession.isSupported() else { return }
|
||||
let session = WCSession.default
|
||||
session.delegate = self
|
||||
session.activate()
|
||||
}
|
||||
|
||||
private func receive(_ applicationContext: [String: Any], session: WCSession) {
|
||||
guard
|
||||
applicationContext[Self.snapshotKey] is Data,
|
||||
let receipt = applicationContext[Self.deliveredReceiptKey] as? Data
|
||||
else { return }
|
||||
let acknowledgement = [Self.deliveredReceiptKey: receipt]
|
||||
if session.isReachable {
|
||||
session.sendMessage(acknowledgement, replyHandler: nil) { _ in
|
||||
session.transferUserInfo(acknowledgement)
|
||||
}
|
||||
} else {
|
||||
session.transferUserInfo(acknowledgement)
|
||||
}
|
||||
}
|
||||
|
||||
func session(
|
||||
_ session: WCSession,
|
||||
activationDidCompleteWith activationState: WCSessionActivationState,
|
||||
error: Error?
|
||||
) {
|
||||
guard activationState == .activated, error == nil else { return }
|
||||
receive(session.receivedApplicationContext, session: session)
|
||||
}
|
||||
|
||||
func session(
|
||||
_ session: WCSession,
|
||||
didReceiveApplicationContext applicationContext: [String: Any]
|
||||
) {
|
||||
receive(applicationContext, session: session)
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
struct IronStorageWatchApp: App {
|
||||
@StateObject private var transport = WatchSnapshotTransport()
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentUnavailableView("No TOTP Codes", systemImage: "timer")
|
||||
|
||||
Reference in New Issue
Block a user