Makes many to many collections non-optional

multistore
Laurent 2 years ago
parent 85a1239cb4
commit bd0742d7cf
  1. 2
      PadelClub/Data/Club.swift
  2. 34
      PadelClub/Data/DataStore.swift
  3. 8
      PadelClub/Data/TeamScore.swift
  4. 11
      PadelClub/Data/User.swift
  5. 6
      PadelClub/Views/Club/ClubDetailView.swift
  6. 4
      PadelClub/Views/Club/ClubSearchView.swift
  7. 4
      PadelClub/Views/Club/CreateClubView.swift
  8. 2
      PadelClub/Views/Navigation/Umpire/UmpireView.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 {

@ -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)

@ -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

@ -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) })
}

@ -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 {

@ -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 {

@ -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 {

@ -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")
}

Loading…
Cancel
Save