diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index d6b93fa..a7a722a 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -118,15 +118,25 @@ class DataStore: ObservableObject { } else if let eventsCollection: StoredCollection = notification.object as? StoredCollection { self._fixMissingEventCreatorIfNecessary(eventsCollection) } + + if Store.main.collectionsAllLoaded() { + Patcher.applyAllWhenApplicable() + } + } fileprivate func _fixMissingClubCreatorIfNecessary(_ clubsCollection: StoredCollection) { - for club in clubsCollection { - if let userId = StoreCenter.main.userId, club.creator == nil { - club.creator = userId - self.userStorage.item()?.addClub(club) - clubsCollection.writeChangeAndInsertOnServer(instance: club) + do { + for club in clubsCollection { + if let userId = StoreCenter.main.userId, club.creator == nil { + club.creator = userId + self.userStorage.item()?.addClub(club) + try self.userStorage.update() + clubsCollection.writeChangeAndInsertOnServer(instance: club) + } } + } catch { + Logger.error(error) } } diff --git a/PadelClub/Utils/Patcher.swift b/PadelClub/Utils/Patcher.swift index 7597199..4596316 100644 --- a/PadelClub/Utils/Patcher.swift +++ b/PadelClub/Utils/Patcher.swift @@ -19,20 +19,20 @@ enum Patch: String, CaseIterable { class Patcher { -// static func applyIfNeeded() { -// for patch in Patch.allCases { -// if UserDefaults.standard.value(forKey: patch.id) == nil { -// do { -// try self.applyPatch(patch) -// UserDefaults.standard.setValue(true, forKey: patch.id) -// } catch { -// Logger.error(error) -// } -// } -// } -// } + static func applyAllWhenApplicable() { + for patch in Patch.allCases { + self.patchIfPossible(patch) + } + } + + static func patchIfPossible(_ patch: Patch) { + if UserDefaults.standard.value(forKey: patch.id) == nil { + self._applyPatch(patch) + UserDefaults.standard.setValue(true, forKey: patch.id) + } + } - static func applyPatch(_ patch: Patch) { + fileprivate static func _applyPatch(_ patch: Patch) { switch patch { case .alexisLeDu: self._patchAlexisLeDu() case .importDataFromDev: self._importDataFromDev() diff --git a/PadelClub/Views/Navigation/Toolbox/ToolboxView.swift b/PadelClub/Views/Navigation/Toolbox/ToolboxView.swift index 8a6b377..997cc7c 100644 --- a/PadelClub/Views/Navigation/Toolbox/ToolboxView.swift +++ b/PadelClub/Views/Navigation/Toolbox/ToolboxView.swift @@ -28,22 +28,6 @@ struct ToolboxView: View { SupportButtonView(contentIsUnavailable: false) - if StoreCenter.main.userId == "94f45ed2-8938-4c32-a4b6-e4525073dd33" { - Button { - Patcher.applyPatch(.alexisLeDu) - } label: { - Text("Patch") - } - } - - #if DEBUG - Button { - Patcher.applyPatch(.importDataFromDev) - } label: { - Text("Import from dev") - } - #endif - } #if DEBUG