Harden watch sync and App Group storage
This commit is contained in:
@@ -10,6 +10,7 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
|
||||
|
||||
private let suiteName = "group.com.apt.fuelboard"
|
||||
private let snapshotRequestKey = "fuelboard.snapshotRequest"
|
||||
private var activationRequested = false
|
||||
|
||||
private override init() {
|
||||
super.init()
|
||||
@@ -21,14 +22,21 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
|
||||
if session.delegate !== self {
|
||||
session.delegate = self
|
||||
}
|
||||
guard session.activationState != .activated else { return }
|
||||
guard !activationRequested else { return }
|
||||
activationRequested = true
|
||||
session.activate()
|
||||
}
|
||||
|
||||
func requestRefresh(at date: Date = Date()) {
|
||||
guard WCSession.isSupported() else { return }
|
||||
let session = WCSession.default
|
||||
guard session.activationState == .activated else {
|
||||
activate()
|
||||
return
|
||||
}
|
||||
let raw = String(date.timeIntervalSince1970)
|
||||
let payload = ["fuelboard.watchRefreshRequest": raw]
|
||||
let session = WCSession.default
|
||||
|
||||
if session.isReachable {
|
||||
session.sendMessage(payload, replyHandler: nil, errorHandler: nil)
|
||||
@@ -39,8 +47,12 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
|
||||
|
||||
func requestSnapshot() {
|
||||
guard WCSession.isSupported() else { return }
|
||||
let payload = [snapshotRequestKey: String(Date().timeIntervalSince1970)]
|
||||
let session = WCSession.default
|
||||
guard session.activationState == .activated else {
|
||||
activate()
|
||||
return
|
||||
}
|
||||
let payload = [snapshotRequestKey: String(Date().timeIntervalSince1970)]
|
||||
|
||||
if session.isReachable {
|
||||
session.sendMessage(payload, replyHandler: nil, errorHandler: nil)
|
||||
@@ -82,6 +94,7 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
|
||||
}
|
||||
|
||||
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: (any Error)?) {
|
||||
activationRequested = false
|
||||
if let error {
|
||||
print("WATCH-SYNC activation failed: \(error.localizedDescription)")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user