diff --git a/PadelClub/Data/Club.swift b/PadelClub/Data/Club.swift index 696715d..6836fb8 100644 --- a/PadelClub/Data/Club.swift +++ b/PadelClub/Data/Club.swift @@ -131,7 +131,7 @@ extension Club { } func isFavorite() -> Bool { - DataStore.shared.user.clubs?.contains(where: { $0 == id }) == true + return DataStore.shared.user.clubs.contains(where: { $0 == id }) } static func findOrCreate(name: String, code: String?, city: String? = nil, zipCode: String? = nil) -> Club { diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index fe586b6..b792c41 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -138,24 +138,22 @@ class DataStore: ObservableObject { //// self._userStorage.item = user // } - var globalRights: UserRight { - if let _ = Guard.main.currentPlan { - return .creation - } - if self.user.clubs != nil { - if user.umpireCode != nil { - return .creation - } else { - return .edition - } - } - - // TODO what are the rules when testing the app? - // scenario example: one cancelled tournament - - - return .none - } +// var globalRights: UserRight { +// if let _ = Guard.main.currentPlan { +// return .creation +// } +// if self.user.umpireCode != nil { +// return .creation +// } else { +// return .edition +// } +// +// // TODO what are the rules when testing the app? +// // scenario example: one cancelled tournament +// +// +// return .none +// } func disconnect() { Store.main.disconnect(resetAll: true) diff --git a/PadelClub/Data/TeamScore.swift b/PadelClub/Data/TeamScore.swift index 8fe3684..4aca3a5 100644 --- a/PadelClub/Data/TeamScore.swift +++ b/PadelClub/Data/TeamScore.swift @@ -16,15 +16,17 @@ class TeamScore: ModelObject, Storable { var id: String = Store.randomId() var match: String var teamRegistration: String? - var playerRegistrations: [String]? + var playerRegistrations: [String] = [] var score: String? var walkOut: Int? var luckyLoser: Int? internal init(match: String, team: TeamRegistration?) { self.match = match - self.teamRegistration = team?.id - self.playerRegistrations = team?.players().map { $0.id } + if let team { + self.teamRegistration = team.id + self.playerRegistrations = team.players().map { $0.id } + } self.score = nil self.walkOut = nil self.luckyLoser = nil diff --git a/PadelClub/Data/User.swift b/PadelClub/Data/User.swift index a7c9f96..f6c36de 100644 --- a/PadelClub/Data/User.swift +++ b/PadelClub/Data/User.swift @@ -24,7 +24,7 @@ class User: UserBase, Storable { public var id: String = Store.randomId() public var username: String public var email: String - var clubs: [String]? + var clubs: [String] = [] var umpireCode: String? var licenceId: String? var firstName: String @@ -58,7 +58,7 @@ class User: UserBase, Storable { } func hasClubs() -> Bool { - clubs?.isEmpty == false + self.clubs.isEmpty == false } func hasFavoriteClubsAndCreatedClubs() -> Bool { @@ -66,15 +66,10 @@ class User: UserBase, Storable { } func setUserClub(_ userClub: Club) { - if clubs == nil { - clubs = [userClub.id] - } else { - clubs!.insert(userClub.id, at: 0) - } + self.clubs.insert(userClub.id, at: 0) } func clubsObjects(includeCreated: Bool = false) -> [Club] { - guard let clubs else { return [] } return Store.main.filter(isIncluded: { (includeCreated && $0.creator == id) || clubs.contains($0.id) }) } diff --git a/PadelClub/Views/Club/ClubDetailView.swift b/PadelClub/Views/Club/ClubDetailView.swift index 4c62c10..c9ce608 100644 --- a/PadelClub/Views/Club/ClubDetailView.swift +++ b/PadelClub/Views/Club/ClubDetailView.swift @@ -153,7 +153,7 @@ struct ClubDetailView: View { RowButtonView("Supprimer ce club", role: .destructive) { do { try dataStore.clubs.deleteById(club.id) - dataStore.user.clubs?.removeAll(where: { $0 == club.id }) + dataStore.user.clubs.removeAll(where: { $0 == club.id }) try dataStore.userStorage.update() } catch { Logger.error(error) @@ -176,9 +176,9 @@ struct ClubDetailView: View { BarButtonView("Favori", icon: isFavorite ? "star.fill" : "star") { do { if isFavorite { - dataStore.user.clubs?.removeAll(where: { $0 == club.id }) + dataStore.user.clubs.removeAll(where: { $0 == club.id }) } else { - dataStore.user.clubs?.append(club.id) + dataStore.user.clubs.append(club.id) } try dataStore.userStorage.update() } catch { diff --git a/PadelClub/Views/Club/ClubSearchView.swift b/PadelClub/Views/Club/ClubSearchView.swift index f414c02..81786ad 100644 --- a/PadelClub/Views/Club/ClubSearchView.swift +++ b/PadelClub/Views/Club/ClubSearchView.swift @@ -98,8 +98,8 @@ struct ClubSearchView: View { if displayContext == .addition { do { try dataStore.clubs.addOrUpdate(instance: clubToEdit) - if dataStore.user.clubs?.contains(where: { $0 == clubToEdit.id }) == false { - dataStore.user.clubs?.append(clubToEdit.id) + if dataStore.user.clubs.contains(where: { $0 == clubToEdit.id }) == false { + dataStore.user.clubs.append(clubToEdit.id) try dataStore.userStorage.update() } } catch { diff --git a/PadelClub/Views/Club/CreateClubView.swift b/PadelClub/Views/Club/CreateClubView.swift index ee2d7b4..09c3f33 100644 --- a/PadelClub/Views/Club/CreateClubView.swift +++ b/PadelClub/Views/Club/CreateClubView.swift @@ -44,8 +44,8 @@ struct CreateClubView: View { //save into user do { - if dataStore.user.clubs?.contains(where: { $0 == existingOrCreatedClub.id }) == false { - dataStore.user.clubs?.append(existingOrCreatedClub.id) + if dataStore.user.clubs.contains(where: { $0 == existingOrCreatedClub.id }) == false { + dataStore.user.clubs.append(existingOrCreatedClub.id) try dataStore.userStorage.update() } } catch { diff --git a/PadelClub/Views/Navigation/Umpire/UmpireView.swift b/PadelClub/Views/Navigation/Umpire/UmpireView.swift index 8ae51ae..a734fee 100644 --- a/PadelClub/Views/Navigation/Umpire/UmpireView.swift +++ b/PadelClub/Views/Navigation/Umpire/UmpireView.swift @@ -85,7 +85,7 @@ struct UmpireView: View { ClubsView() } label: { LabeledContent { - Text((dataStore.user.clubs ?? []).count.formatted()) + Text(dataStore.user.clubs.count.formatted()) } label: { Label("Mes clubs", systemImage: "house.and.flag.circle.fill") }