parent
6d006d2917
commit
8e3a218979
@ -0,0 +1,83 @@ |
|||||||
|
// |
||||||
|
// ClubCourtSetupView.swift |
||||||
|
// PadelClub |
||||||
|
// |
||||||
|
// Created by Razmig Sarkissian on 20/05/2024. |
||||||
|
// |
||||||
|
|
||||||
|
import SwiftUI |
||||||
|
import LeStorage |
||||||
|
|
||||||
|
struct ClubCourtSetupView: View { |
||||||
|
@EnvironmentObject var dataStore: DataStore |
||||||
|
@Bindable var club: Club |
||||||
|
let displayContext: DisplayContext |
||||||
|
@Binding var selectedCourt: Court? |
||||||
|
|
||||||
|
@ViewBuilder |
||||||
|
var body: some View { |
||||||
|
Section { |
||||||
|
TournamentFieldsManagerView(localizedStringKey: "Terrains", count: $club.courtCount) |
||||||
|
.disabled(displayContext == .lockedForEditing) |
||||||
|
.onChange(of: club.courtCount) { |
||||||
|
if displayContext != .addition { |
||||||
|
do { |
||||||
|
try dataStore.clubs.addOrUpdate(instance: club) |
||||||
|
} catch { |
||||||
|
Logger.error(error) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} footer: { |
||||||
|
if displayContext == .lockedForEditing { |
||||||
|
Text("Édition impossible, vous n'êtes pas le créateur de ce club.").foregroundStyle(.logoRed) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Section { |
||||||
|
ForEach((0..<club.courtCount), id: \.self) { courtIndex in |
||||||
|
_courtView(atIndex: courtIndex, tournamentClub: club) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@ViewBuilder |
||||||
|
private func _courtView(atIndex index: Int, tournamentClub: Club) -> some View { |
||||||
|
let court = tournamentClub.customizedCourts.first(where: { $0.index == index }) |
||||||
|
LabeledContent { |
||||||
|
if displayContext == .edition { |
||||||
|
FooterButtonView("personnaliser") { |
||||||
|
if let court { |
||||||
|
selectedCourt = court |
||||||
|
} else { |
||||||
|
let newCourt = Court(index: index, club: tournamentClub.id) |
||||||
|
do { |
||||||
|
try dataStore.courts.addOrUpdate(instance: newCourt) |
||||||
|
} catch { |
||||||
|
Logger.error(error) |
||||||
|
} |
||||||
|
selectedCourt = newCourt |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} label: { |
||||||
|
if let court { |
||||||
|
Text(court.courtTitle()) |
||||||
|
HStack { |
||||||
|
if court.indoor { |
||||||
|
Text("Couvert") |
||||||
|
} |
||||||
|
if court.exitAllowed { |
||||||
|
Text("Sortie autorisée") |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
Text(_courtName(atIndex: index)) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private func _courtName(atIndex index: Int) -> String { |
||||||
|
Court.courtIndexedTitle(atIndex: index) |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue