diff --git a/PadelClubData/Data/DataStore.swift b/PadelClubData/Data/DataStore.swift index 45ed01e..44afbac 100644 --- a/PadelClubData/Data/DataStore.swift +++ b/PadelClubData/Data/DataStore.swift @@ -22,8 +22,8 @@ public class DataStore: ObservableObject { if self.user.id != self.userStorage.item()?.id { self.userStorage.setItemNoSync(self.user) StoreCenter.main.initialSynchronization(clear: false) - self._fixMissingClubCreatorIfNecessary(self.clubs) - self._fixMissingEventCreatorIfNecessary(self.events) + self._fixMissingClubCreatorIfNecessary() + self._fixMissingEventCreatorIfNecessary() } } else { self._temporaryLocalUser.item = self.user @@ -96,12 +96,12 @@ public class DataStore: ObservableObject { @objc func collectionDidLoad(notification: Notification) { - if let userSingleton: StoredSingleton = notification.object as? StoredSingleton { - self.user = userSingleton.item() ?? self._temporaryLocalUser.item ?? CustomUser.placeHolder() - } else if let clubsCollection: SyncedCollection = notification.object as? SyncedCollection { - self._fixMissingClubCreatorIfNecessary(clubsCollection) - } else if let eventsCollection: SyncedCollection = notification.object as? SyncedCollection { - self._fixMissingEventCreatorIfNecessary(eventsCollection) + if let userSingleton: StoredCollection = notification.object as? StoredCollection { + self.user = userSingleton.first ?? self._temporaryLocalUser.item ?? CustomUser.placeHolder() + } else if let clubsCollection: StoredCollection = notification.object as? StoredCollection { + self._fixMissingClubCreatorIfNecessary() + } else if let eventsCollection: StoredCollection = notification.object as? StoredCollection { + self._fixMissingEventCreatorIfNecessary() } if Store.main.fileCollectionsAllLoaded() { @@ -110,10 +110,11 @@ public class DataStore: ObservableObject { } - fileprivate func _fixMissingClubCreatorIfNecessary(_ clubsCollection: SyncedCollection) { + fileprivate func _fixMissingClubCreatorIfNecessary() { if self.user.clubs.count > 0 { return } + let clubsCollection = DataStore.shared.clubs for club in clubsCollection { if let userId = StoreCenter.main.userId, club.creator == nil { club.creator = userId @@ -124,7 +125,8 @@ public class DataStore: ObservableObject { } } - fileprivate func _fixMissingEventCreatorIfNecessary(_ eventsCollection: SyncedCollection) { + fileprivate func _fixMissingEventCreatorIfNecessary() { + let eventsCollection = DataStore.shared.events for event in eventsCollection { if let userId = StoreCenter.main.userId, event.creator == nil { event.creator = userId diff --git a/PadelClubData/Data/GroupStage.swift b/PadelClubData/Data/GroupStage.swift index 3f2cae4..b8eefd5 100644 --- a/PadelClubData/Data/GroupStage.swift +++ b/PadelClubData/Data/GroupStage.swift @@ -608,7 +608,7 @@ final public class GroupStage: BaseGroupStage, SideStorable { } public func computedStartDate() -> Date? { - return _matches().sorted(by: \.computedStartDateForSorting).first?.startDate + return self.matches().sorted(by: \.computedStartDateForSorting).first?.startDate } public override func deleteDependencies(store: Store, actionOption: ActionOption) { diff --git a/PadelClubDataTests/SyncDataAccessTests.swift b/PadelClubDataTests/SyncDataAccessTests.swift index 45b0319..63b82cb 100644 --- a/PadelClubDataTests/SyncDataAccessTests.swift +++ b/PadelClubDataTests/SyncDataAccessTests.swift @@ -414,8 +414,9 @@ struct SyncDataAccessTests { eventB.club = club2B.id try await eventColB.addOrUpdateAsync(instance: eventB) - let _ = try await self.storeCenterA.testSynchronizeOnceAsync() - + let data = try await self.storeCenterA.testSynchronizeOnceAsync() + let syncData = try SyncData(data: data, storeCenter: self.storeCenterB) + #expect(eventA.club == club2B.id) #expect(clubColB.count == 1) #expect(courtsColB.count == 0)