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. 42
      PadelClub/Views/Club/ClubsView.swift
  4. 2
      PadelClub/Views/Navigation/MainView.swift

@ -90,7 +90,7 @@ class User: ModelObject, UserBase, Storable {
} }
func createdClubsObjectsNotFavorite() -> [Club] { 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) { func saveMatchFormatsDefaultDuration(_ matchFormat: MatchFormat, estimatedDuration: Int) {

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

@ -28,7 +28,13 @@ struct ClubsView: View {
var body: some View { var body: some View {
List { List {
let clubs : [Club] = dataStore.user.clubsObjects(includeCreated: false) let clubs : [Club] = dataStore.user.clubsObjects(includeCreated: false)
let onlyCreatedClubs : [Club] = dataStore.user.createdClubsObjectsNotFavorite()
if clubs.isEmpty == false || onlyCreatedClubs.isEmpty == false {
Section { Section {
if clubs.isEmpty && onlyCreatedClubs.isEmpty == false {
_contentUnavailable()
}
ForEach(clubs) { club in ForEach(clubs) { club in
if let selection { if let selection {
Button { Button {
@ -51,8 +57,7 @@ struct ClubsView: View {
} header: { } header: {
Text("Clubs favoris") Text("Clubs favoris")
} }
}
let onlyCreatedClubs : [Club] = dataStore.user.createdClubsObjectsNotFavorite()
if onlyCreatedClubs.isEmpty == false { if onlyCreatedClubs.isEmpty == false {
Section { Section {
@ -81,22 +86,11 @@ struct ClubsView: View {
} }
} }
.overlay { .overlay {
if dataStore.user.hasFavoriteClubsAndCreatedClubs() == false { if dataStore.user.clubsObjects(includeCreated: true).isEmpty {
ContentUnavailableView { _contentUnavailable()
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
}
} }
} }
} .navigationTitle(selection == nil ? "Clubs favoris" : "Choisir un club")
.navigationTitle(selection == nil ? "Mes clubs" : "Choisir un club")
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)
.toolbarBackground(.visible, for: .navigationBar) .toolbarBackground(.visible, for: .navigationBar)
.sheet(isPresented: presentClubCreationView) { .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 { #Preview {

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

Loading…
Cancel
Save