From 6b573e45058a68f8ad18fc4af6c68e18f9d5dea1 Mon Sep 17 00:00:00 2001 From: Raz Date: Sat, 12 Apr 2025 10:40:38 +0200 Subject: [PATCH] add user licence update --- PadelClub/Data/DataStore.swift | 2 +- .../Views/Navigation/Umpire/UmpireView.swift | 34 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index a3783e6..7fc05c3 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -38,7 +38,7 @@ class DataStore: ObservableObject { fileprivate(set) var dateIntervals: SyncedCollection fileprivate(set) var purchases: SyncedCollection - fileprivate var userStorage: StoredSingleton + var userStorage: StoredSingleton fileprivate var _temporaryLocalUser: OptionalStorage = OptionalStorage(fileName: "tmp_local_user.json") fileprivate(set) var appSettingsStorage: MicroStorage = MicroStorage(fileName: "appsettings.json") diff --git a/PadelClub/Views/Navigation/Umpire/UmpireView.swift b/PadelClub/Views/Navigation/Umpire/UmpireView.swift index 0cf1522..f4ce0c5 100644 --- a/PadelClub/Views/Navigation/Umpire/UmpireView.swift +++ b/PadelClub/Views/Navigation/Umpire/UmpireView.swift @@ -311,21 +311,24 @@ struct UmpireView: View { NavigationStack { SelectablePlayerListView(allowSelection: 1, searchField: user.firstName + " " + user.lastName, playerSelectionAction: { players in if let player = players.first { - user.licenceId = player.license?.computedLicense if user.clubsObjects().contains(where: { $0.code == player.clubCode }) == false { let userClub = Club.findOrCreate(name: player.clubName!, code: player.clubCode) if userClub.hasBeenCreated(by: StoreCenter.main.userId) { - do { - try dataStore.clubs.addOrUpdate(instance: userClub) - } catch { - Logger.error(error) - } + dataStore.clubs.addOrUpdate(instance: userClub) } user.setUserClub(userClub) } + self._updateUserLicense(license: player.license?.computedLicense) } }) } + .task { + do { + try await dataStore.clubs.loadDataFromServerIfAllowed() + } catch { + Logger.error(error) + } + } } .navigationDestination(for: UmpireScreen.self) { screen in switch screen { @@ -336,6 +339,25 @@ struct UmpireView: View { } } + private func _updateUserLicense(license: String?) { + print("_updateUserLicense(license: ", license) + guard let license else { return } + + let copyUser = CustomUser(username: "", email: "", firstName: "", lastName: "", phone: nil, country: nil, loserBracketMode: .automatic) + copyUser.copy(from: dataStore.user) + copyUser.licenceId = license + + Task { + do { + try await self.dataStore.userStorage.tryPutBeforeUpdating(copyUser) + print("_updateUserLicense OK") + } catch { + print(error) + Logger.error(error) + } + } + } + private func _confirmUmpireMail() { umpireCustomMailIsInvalid = false if umpireCustomMail.isEmpty {