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/Tournament/Screen/Components/TournamentClubSettingsView....

121 lines
3.9 KiB

//
// TournamentClubSettingsView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 18/04/2024.
//
import SwiftUI
import LeStorage
struct TournamentClubSettingsView: View {
@Environment(Tournament.self) private var tournament: Tournament
@EnvironmentObject var dataStore: DataStore
@State var selectedCourt: Court?
var body: some View {
@Bindable var tournament = tournament
List {
let event = tournament.eventObject()
let selectedClub = event?.clubObject()
Section {
if let selectedClub {
NavigationLink {
ClubDetailView(club: selectedClub, displayContext: selectedClub.hasBeenCreated(by: dataStore.user?.id) ? .edition : .lockedForEditing)
} label: {
ClubRowView(club: selectedClub)
}
} else {
NavigationLink {
ClubsView() { club in
if let event {
event.club = club.id
do {
try dataStore.events.addOrUpdate(instance: event)
} catch {
Logger.error(error)
}
}
}
} label: {
Text("Choisir un club")
}
}
} header: {
Text("Lieu du tournoi")
} footer: {
if let event, selectedClub != nil {
HStack {
Spacer()
Button("modifier", role: .destructive) {
event.club = nil
try? dataStore.events.addOrUpdate(instance: event)
}
}
}
}
Section {
TournamentFieldsManagerView(localizedStringKey: "Terrains maximum", count: $tournament.courtCount)
}
if let selectedClub {
Section {
ForEach((0..<tournament.courtCount), id: \.self) { courtIndex in
_courtView(atIndex: courtIndex, tournamentClub: selectedClub)
}
}
}
}
.onChange(of: tournament.courtCount) {
do {
try dataStore.tournaments.addOrUpdate(instance: tournament)
} catch {
Logger.error(error)
}
}
.navigationDestination(item: $selectedCourt) { court in
CourtView(court: court)
}
}
@ViewBuilder
private func _courtView(atIndex index: Int, tournamentClub: Club) -> some View {
if let court = tournamentClub.courts.first(where: { $0.index == index }) {
LabeledContent {
if let name = court.name {
Text(name)
}
} label: {
Text(court.courtIndexTitle())
HStack {
if court.indoor {
Text("Couvert")
}
if court.exitAllowed {
Text("Sortie autorisée")
}
}
}
} else {
LabeledContent {
FooterButtonView("personnaliser") {
let court = Court(club: tournamentClub.id, index: index)
try? dataStore.courts.addOrUpdate(instance: court)
selectedCourt = court
}
} label: {
Text(_courtName(atIndex: index))
}
}
}
private func _courtName(atIndex index: Int) -> String {
Court.courtIndexedTitle(atIndex: index)
}
}
#Preview {
TournamentClubSettingsView()
}