mirror of
https://github.com/jeremysrand/ListenerApp.git
synced 2024-12-30 12:29:52 +00:00
Add code to load and save the destinations to iCloud. Not yet subscribed to watch for iCloud changes though.
This commit is contained in:
parent
0528a7db41
commit
dffbd0ec1c
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user