Harden watch sync and App Group storage
This commit is contained in:
@@ -157,6 +157,7 @@ private enum WatchFuelCache {
|
||||
static let appGroupSuite = "group.com.apt.fuelboard"
|
||||
static let favouritesKey = "fuelboard.favourites"
|
||||
static let stationsKey = "fuelboard.stations"
|
||||
static let stationsFileName = "fuelboard.stations.json"
|
||||
static let fuelKey = "fuelboard.selectedFuel"
|
||||
static let distanceUnitKey = "fuelboard.distanceUnit"
|
||||
static let priceStyleKey = "fuelboard.priceDisplayStyle"
|
||||
@@ -228,7 +229,17 @@ private enum WatchFuelCache {
|
||||
}
|
||||
|
||||
static func loadStations() -> [WatchStation] {
|
||||
guard let data = defaults()?.data(forKey: stationsKey),
|
||||
let fileData: Data?
|
||||
if let container = FileManager.default.containerURL(
|
||||
forSecurityApplicationGroupIdentifier: appGroupSuite
|
||||
) {
|
||||
let url = container.appendingPathComponent(stationsFileName)
|
||||
fileData = try? Data(contentsOf: url)
|
||||
} else {
|
||||
fileData = nil
|
||||
}
|
||||
let data = fileData ?? defaults()?.data(forKey: stationsKey)
|
||||
guard let data,
|
||||
let stations = try? JSONDecoder().decode([WatchStation].self, from: data) else {
|
||||
return []
|
||||
}
|
||||
|
||||
@@ -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