From dffbd0ec1ce1988a38da8d847bad2e359afe4ffa Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Wed, 3 Nov 2021 23:20:29 -0400 Subject: [PATCH] Add code to load and save the destinations to iCloud. Not yet subscribed to watch for iCloud changes though. --- ListenerGS/GSDestinations.swift | 41 +++++++++++++++++++++++++++++++++ ListenerGS/Info.plist | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/ListenerGS/GSDestinations.swift b/ListenerGS/GSDestinations.swift index 221f63e..43f546a 100644 --- a/ListenerGS/GSDestinations.swift +++ b/ListenerGS/GSDestinations.swift @@ -16,15 +16,56 @@ struct Destination: Identifiable, Hashable { class GSDestinations : ObservableObject { @Published var dests:[Destination] = [] + private var storedDestinations : [Any] = [] + + static let kDestinationKey = "destinations" + static let kDestinationIpAddress = "ip_address" + public func onDelete(offsets: IndexSet) { dests.remove(atOffsets: offsets) + storedDestinations.remove(atOffsets: offsets) + saveDestinations() } public func onMove(source: IndexSet, destination: Int) { dests.move(fromOffsets: source, toOffset: destination) + storedDestinations.move(fromOffsets: source, toOffset: destination) + saveDestinations() } public func onAdd(ipAddress: String) { dests.append(Destination(ipAddress: ipAddress)) + storedDestinations.append([GSDestinations.kDestinationIpAddress : ipAddress]) + saveDestinations() + } + + private func loadDestinations() { + if let newStoredDestinations = NSUbiquitousKeyValueStore.default.array(forKey: GSDestinations.kDestinationKey) { + storedDestinations = newStoredDestinations + dests = [] + + for value in storedDestinations { + if let ipAddress = (value as! [String:String])[GSDestinations.kDestinationIpAddress] { + dests.append(Destination(ipAddress: ipAddress)) + } else { + storedDestinations = [] + dests = [] + break + } + } + } else { + storedDestinations = [] + dests = [] + } + } + + private func saveDestinations() { + NSUbiquitousKeyValueStore.default.set(storedDestinations, forKey: GSDestinations.kDestinationKey) + } + + init() { + loadDestinations() + + // JSR_TODO - Add code here to watch for changes from iCloud } } diff --git a/ListenerGS/Info.plist b/ListenerGS/Info.plist index 3c1113f..d1f84df 100644 --- a/ListenerGS/Info.plist +++ b/ListenerGS/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString 1.0 CFBundleVersion - 242 + 246 LSApplicationCategoryType public.app-category.utilities LSRequiresIPhoneOS