fix club stuff

multistore
Razmig Sarkissian 1 year ago
parent a1e9b6e8d0
commit 50ed16e99e
  1. 2
      PadelClub/Data/User.swift
  2. 6
      PadelClub/Views/Club/ClubDetailView.swift
  3. 80
      PadelClub/Views/Club/ClubsView.swift
  4. 2
      PadelClub/Views/Navigation/MainView.swift

@ -90,7 +90,7 @@ class User: ModelObject, UserBase, Storable {
}
func createdClubsObjectsNotFavorite() -> [Club] {
return Store.main.filter(isIncluded: { ($0.creator == id) || clubs.contains($0.id) == false })
return Store.main.filter(isIncluded: { ($0.creator == id) && clubs.contains($0.id) == false })
}
func saveMatchFormatsDefaultDuration(_ matchFormat: MatchFormat, estimatedDuration: Int) {

@ -17,6 +17,7 @@ struct ClubDetailView: View {
@State private var zipCode: String
@State private var selectedCourt: Court?
@Bindable var club: Club
@State private var clubDeleted: Bool = false
var displayContext: DisplayContext
var selection: ((Club) -> ())? = nil
@ -217,9 +218,10 @@ struct ClubDetailView: View {
Section {
RowButtonView("Supprimer ce club", role: .destructive) {
do {
try dataStore.clubs.deleteById(club.id)
clubDeleted = true
dataStore.user.clubs.removeAll(where: { $0 == club.id })
self.dataStore.saveUser()
try dataStore.clubs.deleteById(club.id)
dismiss()
} catch {
Logger.error(error)
@ -240,7 +242,7 @@ struct ClubDetailView: View {
CourtView(court: court)
}
.onDisappear {
if displayContext == .edition {
if displayContext == .edition && clubDeleted == false {
do {
try dataStore.clubs.addOrUpdate(instance: club)
} catch {

@ -28,31 +28,36 @@ struct ClubsView: View {
var body: some View {
List {
let clubs : [Club] = dataStore.user.clubsObjects(includeCreated: false)
Section {
ForEach(clubs) { club in
if let selection {
Button {
selection(club)
dismiss()
} label: {
ClubRowView(club: club, displayContext: .selection)
.frame(maxWidth: .infinity)
}
.contentShape(Rectangle())
.buttonStyle(.plain)
} else {
NavigationLink {
ClubDetailView(club: club, displayContext: club.hasBeenCreated(by: dataStore.user.id) ? .edition : .lockedForEditing)
} label: {
ClubRowView(club: club)
let onlyCreatedClubs : [Club] = dataStore.user.createdClubsObjectsNotFavorite()
if clubs.isEmpty == false || onlyCreatedClubs.isEmpty == false {
Section {
if clubs.isEmpty && onlyCreatedClubs.isEmpty == false {
_contentUnavailable()
}
ForEach(clubs) { club in
if let selection {
Button {
selection(club)
dismiss()
} label: {
ClubRowView(club: club, displayContext: .selection)
.frame(maxWidth: .infinity)
}
.contentShape(Rectangle())
.buttonStyle(.plain)
} else {
NavigationLink {
ClubDetailView(club: club, displayContext: club.hasBeenCreated(by: dataStore.user.id) ? .edition : .lockedForEditing)
} label: {
ClubRowView(club: club)
}
}
}
} header: {
Text("Clubs favoris")
}
} header: {
Text("Clubs favoris")
}
let onlyCreatedClubs : [Club] = dataStore.user.createdClubsObjectsNotFavorite()
if onlyCreatedClubs.isEmpty == false {
Section {
@ -81,22 +86,11 @@ struct ClubsView: View {
}
}
.overlay {
if dataStore.user.hasFavoriteClubsAndCreatedClubs() == false {
ContentUnavailableView {
Label("Aucun club", systemImage: "house.and.flag.fill")
} description: {
Text("Texte décrivant l'utilité d'un club et les features que cela apporte")
} actions: {
RowButtonView("Créer un nouveau club", systemImage: "plus.circle.fill") {
newClub = Club.newEmptyInstance()
}
RowButtonView("Chercher un club", systemImage: "magnifyingglass.circle.fill") {
presentClubSearchView = true
}
}
if dataStore.user.clubsObjects(includeCreated: true).isEmpty {
_contentUnavailable()
}
}
.navigationTitle(selection == nil ? "Mes clubs" : "Choisir un club")
.navigationTitle(selection == nil ? "Clubs favoris" : "Choisir un club")
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(.visible, for: .navigationBar)
.sheet(isPresented: presentClubCreationView) {
@ -141,6 +135,22 @@ struct ClubsView: View {
}
}
}
private func _contentUnavailable() -> some View {
ContentUnavailableView {
Label("Aucun club", systemImage: "house.and.flag.fill")
} description: {
Text("Avoir un club en favori permet d'accéder aux tournois enregistrés sur Tenup.")
} actions: {
RowButtonView("Créer un nouveau club", systemImage: "plus.circle.fill") {
newClub = Club.newEmptyInstance()
}
RowButtonView("Chercher un club", systemImage: "magnifyingglass.circle.fill") {
presentClubSearchView = true
}
}
}
}
#Preview {

@ -49,7 +49,7 @@ struct MainView: View {
}
var badgeText: Text? {
dataStore.user.username.isEmpty ? Text("!").font(.headline) : nil
Store.main.userId == nil ? Text("!").font(.headline) : nil
}
var body: some View {

Loading…
Cancel
Save