diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index d98db75..164d6c9 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -134,7 +134,7 @@ class DataStore: ObservableObject { } if Store.main.fileCollectionsAllLoaded() { - Patcher.applyAllWhenApplicable() + AutomaticPatcher.applyAllWhenApplicable() } } diff --git a/PadelClub/PadelClubApp.swift b/PadelClub/PadelClubApp.swift index 8ac2503..5033f08 100644 --- a/PadelClub/PadelClubApp.swift +++ b/PadelClub/PadelClubApp.swift @@ -17,6 +17,7 @@ struct PadelClubApp: App { @StateObject var dataStore = DataStore.shared @State private var registrationError: RegistrationError? = nil @State private var importObserverViewModel = ImportObserver() + @State private var showDisconnectionAlert: Bool = false @Environment(\.horizontalSizeClass) var horizontalSizeClass @State var requiredVersion: String? = nil @@ -91,6 +92,10 @@ struct PadelClubApp: App { .accentColor(.master) .onAppear { self._checkVersion() + + if ManualPatcher.patchIfPossible(.disconnect) == true { + self.showDisconnectionAlert = true + } #if DEBUG print("Running in Debug mode") #elseif TESTFLIGHT @@ -104,6 +109,9 @@ struct PadelClubApp: App { self._onAppear() print(PersistenceController.getModelVersion()) } + .alert(isPresented: self.$showDisconnectionAlert, content: { + Alert(title: Text("Vous avez été déconnecté. Veuillez vous reconnecter pour récupérer vos données.")) + }) .task { // try? Tips.resetDatastore() @@ -255,6 +263,12 @@ struct DownloadNewVersionView: View { } +struct DisconnectionAlertView: View { + var body: some View { + Text("Vous avez été déconnecté. Veuillez vous reconnecter pour récupérer vos données.").multilineTextAlignment(.center).padding() + } +} + #Preview { DownloadNewVersionView(version: "1.2") } diff --git a/PadelClub/Utils/Patcher.swift b/PadelClub/Utils/Patcher.swift index 4b76f79..94e4447 100644 --- a/PadelClub/Utils/Patcher.swift +++ b/PadelClub/Utils/Patcher.swift @@ -8,6 +8,44 @@ import Foundation import LeStorage +enum ManualPatch: String { + case disconnect + + var id: String { + return "padelclub.app.manual.patch.\(self.rawValue)" + } +} + +class ManualPatcher { + + static func patchIfPossible(_ patch: ManualPatch) -> Bool { + if UserDefaults.standard.value(forKey: patch.id) == nil { + do { + Logger.log(">>> Patches \(patch.rawValue)...") + let result = try self._applyPatch(patch) + UserDefaults.standard.setValue(true, forKey: patch.id) + return result + } catch { + Logger.error(error) + } + } + return false + } + + fileprivate static func _applyPatch(_ patch: ManualPatch) throws -> Bool { + switch patch { + case .disconnect: + let rawToken = try? StoreCenter.main.rawTokenShouldNotBeUsed() + if StoreCenter.main.userName != nil || StoreCenter.main.userId != nil || rawToken != nil { + DataStore.shared.disconnect() + return true + } else { + return false + } + } + } +} + enum PatchError: Error { case patchError(message: String) } @@ -21,7 +59,7 @@ enum Patch: String, CaseIterable { } } -class Patcher { +class AutomaticPatcher { static func applyAllWhenApplicable() { for patch in Patch.allCases {