Add code to load and save the destinations to iCloud. Not yet subscribed to watch for iCloud changes though.

This commit is contained in:
Jeremy Rand 2021-11-03 23:20:29 -04:00
parent 0528a7db41
commit dffbd0ec1c
2 changed files with 42 additions and 1 deletions

View File

@ -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
}
}

View File

@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>242</string>
<string>246</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSRequiresIPhoneOS</key>