You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
PadelClub/PadelClub/Views/Club/ClubsView.swift

193 lines
6.8 KiB

//
// ClubsView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 20/03/2024.
//
import SwiftUI
import TipKit
import LeStorage
import PadelClubData
struct ClubsView: View {
@EnvironmentObject var dataStore: DataStore
@Environment(\.dismiss) private var dismiss
@State private var presentClubSearchView: Bool = false
@State private var newClub: Club?
var selection: ((Club) -> ())? = nil
var presentClubCreationView: Binding<Bool> { Binding(
get: { newClub != nil },
set: { isPresented in
if isPresented == false {
newClub = nil
}
}
)}
var body: some View {
List {
// #if DEBUG
// Section {
// RowButtonView("Delete unexisted clubs", action: {
// let ids = dataStore.user.clubs
// ids.forEach { clubId in
// if dataStore.clubs.findById(clubId) == nil {
// dataStore.user.clubs.removeAll(where: { $0 == clubId })
// }
// }
// dataStore.saveUser()
// })
// }
// #endif
let clubs : [Club] = dataStore.user.clubsObjects(includeCreated: false)
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: StoreCenter.main.userId) ? .edition : .lockedForEditing)
} label: {
ClubRowView(club: club)
}
}
}
} header: {
Text("Clubs favoris")
}
}
if onlyCreatedClubs.isEmpty == false {
Section {
ForEach(onlyCreatedClubs) { 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: StoreCenter.main.userId) ? .edition : .lockedForEditing)
} label: {
ClubRowView(club: club)
}
}
}
} header: {
Text("Clubs créés retirés des favoris")
}
}
}
.overlay {
if dataStore.user.clubsObjects(includeCreated: true).isEmpty {
_contentUnavailable()
}
}
.navigationTitle(selection == nil ? "Clubs favoris" : "Choisir un club")
.toolbarBackground(.visible, for: .navigationBar)
.sheet(isPresented: presentClubCreationView) {
if let newClub {
CreateClubView(club: newClub) { club in
if let selection {
selection(club)
dismiss()
}
}
.tint(.master)
}
}
.sheet(isPresented: $presentClubSearchView) {
ClubImportView() { club in
if let selection {
selection(club)
dismiss()
}
}
.tint(.master)
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
if #available(iOS 26.0, *) {
Button("Chercher", systemImage: "magnifyingglass") {
presentClubSearchView = true
}
} else {
Button {
presentClubSearchView = true
} label: {
Image(systemName: "magnifyingglass.circle.fill")
.resizable()
.scaledToFit()
.frame(minHeight: 28)
}
}
}
if #available(iOS 26.0, *) {
ToolbarSpacer(placement: .topBarTrailing)
}
ToolbarItem(placement: .topBarTrailing) {
if #available(iOS 26.0, *) {
Button("Ajouter", systemImage: "plus") {
newClub = Club.newEmptyInstance()
}
} else {
Button {
newClub = Club.newEmptyInstance()
} label: {
Image(systemName: "plus.circle.fill")
.resizable()
.scaledToFit()
.frame(minHeight: 28)
}
}
}
}
}
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 {
// NavigationStack {
// ClubsView()
// .environmentObject(DataStore.shared)
// }
//}