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/Event/EventCreationView.swift

144 lines
4.8 KiB

//
// EventCreationView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 22/03/2024.
//
import SwiftUI
struct EventCreationView: View {
@Environment(\.dismiss) private var dismiss
@EnvironmentObject var dataStore: DataStore
@State private var eventType: EventType = .approvedTournament
@State private var animationType: AnimationType = .upAndDown
@State private var startingDate: Date = Date()
@State private var duration: Int = 3
@State private var eventName: String = ""
@State var tournaments: [Tournament] = []
var body: some View {
NavigationStack {
Form {
// Section {
// Picker(selection: $eventType) {
// ForEach(EventType.allCases) { eventType in
// Text(eventType.localizedLabel())
// }
// } label: {
// Text("Type")
// }
// }
Section {
TextField("Nom de l'événement", text: $eventName)
}
Section {
DatePicker(selection: $startingDate) {
Text(startingDate.formatted(.dateTime.weekday(.wide)).capitalized)
}
if eventType == .approvedTournament {
Stepper(value: $duration, in: 1...3) {
HStack {
Text("Durée")
Spacer()
Text("\(duration) jour" + duration.pluralSuffix)
}
}
}
} header: {
Text("Démarrage")
}
switch eventType {
case .approvedTournament:
approvedTournamentEditorView
case .friendlyTournament:
approvedTournamentEditorView
case .simulation:
approvedTournamentEditorView
case .animation:
animationEditorView
}
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Annuler", role: .cancel) {
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Valider") {
if tournaments.count > 1 || eventName.trimmed.isEmpty == false {
let event = Event(name: eventName)
tournaments.forEach { tournament in
tournament.event = event.id
}
try? dataStore.events.addOrUpdate(instance: event)
}
try? dataStore.tournaments.addOrUpdate(contentOfs: tournaments)
dismiss()
}
.clipShape(Capsule())
.buttonStyle(.bordered)
}
}
.navigationTitle("Nouvel événement")
}
}
@ViewBuilder
private var approvedTournamentEditorView: some View {
ForEach(tournaments) { tournament in
Section {
TournamentConfigurationView(tournament: tournament)
} header: {
if tournaments.count > 1 {
HStack {
Spacer()
Button {
tournaments.removeAll(where: { $0 == tournament })
// viewContext.delete(tournament)
} label: {
Text("effacer")
}
.textCase(nil)
}
}
}
}
RowButtonView(title: "Ajouter une \((tournaments.count + 1).ordinalFormatted()) épreuve") {
let tournament = Tournament.newEmptyInstance()
self.tournaments.append(tournament)
}
}
@ViewBuilder
var animationEditorView: some View {
Section {
Picker(selection: $animationType) {
ForEach(AnimationType.allCases) { animationType in
Text(animationType.localizedLabel()).tag(animationType)
}
} label: {
Text("Type")
}.pickerStyle(.menu)
}
Section {
Text(animationType.descriptionLabel)
}
}
}
#Preview {
EventCreationView()
.environmentObject(DataStore.shared)
}