|
|
|
@ -11,7 +11,6 @@ import SwiftUI |
|
|
|
|
|
|
|
|
|
|
|
class DataStore: ObservableObject { |
|
|
|
class DataStore: ObservableObject { |
|
|
|
|
|
|
|
|
|
|
|
// fileprivate var _store: Store |
|
|
|
|
|
|
|
static let shared = DataStore() |
|
|
|
static let shared = DataStore() |
|
|
|
|
|
|
|
|
|
|
|
@Published var user: User = User.placeHolder() { |
|
|
|
@Published var user: User = User.placeHolder() { |
|
|
|
@ -96,7 +95,6 @@ class DataStore: ObservableObject { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func saveUser() { |
|
|
|
func saveUser() { |
|
|
|
|
|
|
|
|
|
|
|
do { |
|
|
|
do { |
|
|
|
if user.username.count > 0 { |
|
|
|
if user.username.count > 0 { |
|
|
|
try self.userStorage.update() |
|
|
|
try self.userStorage.update() |
|
|
|
@ -106,8 +104,6 @@ class DataStore: ObservableObject { |
|
|
|
} catch { |
|
|
|
} catch { |
|
|
|
Logger.error(error) |
|
|
|
Logger.error(error) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@objc func collectionDidLoad(notification: Notification) { |
|
|
|
@objc func collectionDidLoad(notification: Notification) { |
|
|
|
@ -116,11 +112,36 @@ class DataStore: ObservableObject { |
|
|
|
Logger.log("StoredObject<User> loaded with user = \(String(describing: userSingleton.item()))") |
|
|
|
Logger.log("StoredObject<User> loaded with user = \(String(describing: userSingleton.item()))") |
|
|
|
|
|
|
|
|
|
|
|
self.user = userSingleton.item() ?? User.placeHolder() |
|
|
|
self.user = userSingleton.item() ?? User.placeHolder() |
|
|
|
// if let user = object.item() { |
|
|
|
} else if let clubsCollection: StoredCollection<Club> = notification.object as? StoredCollection<Club> { |
|
|
|
// self.user = user |
|
|
|
self._fixMissingClubCreatorIfNecessary(clubsCollection) |
|
|
|
// } else { |
|
|
|
} else if let eventsCollection: StoredCollection<Event> = notification.object as? StoredCollection<Event> { |
|
|
|
// self. |
|
|
|
self._fixMissingEventCreatorIfNecessary(eventsCollection) |
|
|
|
// } |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fileprivate func _fixMissingClubCreatorIfNecessary(_ clubsCollection: StoredCollection<Club>) { |
|
|
|
|
|
|
|
for club in clubsCollection { |
|
|
|
|
|
|
|
if club.creator == nil && self.user.username.count > 0 { |
|
|
|
|
|
|
|
club.creator = self.user.id |
|
|
|
|
|
|
|
do { |
|
|
|
|
|
|
|
try clubsCollection.addOrUpdate(instance: club) |
|
|
|
|
|
|
|
} catch { |
|
|
|
|
|
|
|
Logger.error(error) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fileprivate func _fixMissingEventCreatorIfNecessary(_ eventsCollection: StoredCollection<Event>) { |
|
|
|
|
|
|
|
for event in eventsCollection { |
|
|
|
|
|
|
|
if event.creator == nil && self.user.username.count > 0 { |
|
|
|
|
|
|
|
event.creator = self.user.id |
|
|
|
|
|
|
|
do { |
|
|
|
|
|
|
|
try eventsCollection.addOrUpdate(instance: event) |
|
|
|
|
|
|
|
} catch { |
|
|
|
|
|
|
|
Logger.error(error) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -154,11 +175,7 @@ class DataStore: ObservableObject { |
|
|
|
self.userStorage.reset() |
|
|
|
self.userStorage.reset() |
|
|
|
|
|
|
|
|
|
|
|
Guard.main.disconnect() |
|
|
|
Guard.main.disconnect() |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fileprivate func _loadCollections() { |
|
|
|
|
|
|
|
Store.main.loadCollectionFromServer() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|