Restore smart settings and sync watch favourites
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import Foundation
|
||||
import WatchConnectivity
|
||||
|
||||
extension Notification.Name {
|
||||
static let fuelBoardWatchDataDidUpdate = Notification.Name("FuelBoardWatchDataDidUpdate")
|
||||
}
|
||||
|
||||
final class WatchSyncManager: NSObject, WCSessionDelegate {
|
||||
static let shared = WatchSyncManager()
|
||||
|
||||
private let suiteName = "group.com.apt.fuelboard"
|
||||
|
||||
private override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
func activate() {
|
||||
guard WCSession.isSupported() else { return }
|
||||
let session = WCSession.default
|
||||
if session.delegate !== self {
|
||||
session.delegate = self
|
||||
}
|
||||
session.activate()
|
||||
}
|
||||
|
||||
func requestRefresh(at date: Date = Date()) {
|
||||
guard WCSession.isSupported() else { 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)
|
||||
} else {
|
||||
session.transferUserInfo(payload)
|
||||
}
|
||||
}
|
||||
|
||||
private func sharedDefaults() -> UserDefaults? {
|
||||
UserDefaults(suiteName: suiteName)
|
||||
}
|
||||
|
||||
private func storeSnapshot(_ payload: [String: Any]) {
|
||||
guard let defaults = sharedDefaults() else { return }
|
||||
|
||||
if let favourites = payload["fuelboard.favourites"] as? Data {
|
||||
defaults.set(favourites, forKey: "fuelboard.favourites")
|
||||
}
|
||||
if let fuel = payload["fuelboard.selectedFuel"] as? String {
|
||||
defaults.set(fuel, forKey: "fuelboard.selectedFuel")
|
||||
}
|
||||
if let distanceUnit = payload["fuelboard.distanceUnit"] as? String {
|
||||
defaults.set(distanceUnit, forKey: "fuelboard.distanceUnit")
|
||||
}
|
||||
if let priceStyle = payload["fuelboard.priceDisplayStyle"] as? String {
|
||||
defaults.set(priceStyle, forKey: "fuelboard.priceDisplayStyle")
|
||||
}
|
||||
if let location = payload["fuelboard.lastLocation"] as? String {
|
||||
defaults.set(location, forKey: "fuelboard.lastLocation")
|
||||
}
|
||||
if let lastRefresh = payload["fuelboard.lastRefresh"] as? String {
|
||||
defaults.set(lastRefresh, forKey: "fuelboard.lastRefresh")
|
||||
}
|
||||
if let handled = payload["fuelboard.watchRefreshHandled"] as? String {
|
||||
defaults.set(handled, forKey: "fuelboard.watchRefreshHandled")
|
||||
}
|
||||
|
||||
NotificationCenter.default.post(name: .fuelBoardWatchDataDidUpdate, object: nil)
|
||||
}
|
||||
|
||||
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: (any Error)?) {
|
||||
if let error {
|
||||
print("WATCH-SYNC activation failed: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
|
||||
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
|
||||
storeSnapshot(applicationContext)
|
||||
}
|
||||
|
||||
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
|
||||
storeSnapshot(userInfo)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user