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

179 lines
6.3 KiB

//
// EventCreationView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 22/03/2024.
//
import SwiftUI
import TipKit
struct EventCreationView: View {
@Environment(\.dismiss) private var dismiss
@EnvironmentObject var dataStore: DataStore
@Environment(NavigationViewModel.self) private var navigation: NavigationViewModel
@State private var eventType: EventType = .approvedTournament
@State private var animationType: AnimationType = .upAndDown
@State var startingDate: Date = Date().tomorrowAtNine
@State private var duration: Int = 3
@State private var eventName: String = ""
@State var tournaments: [Tournament] = []
@State var selectedClub: Club?
let multiTournamentsEventTip = MultiTournamentsEventTip()
var body: some View {
NavigationStack {
Form {
// Section {
// Picker(selection: $eventType) {
// ForEach(EventType.allCases) { eventType in
// Text(eventType.localizedLabel())
// }
// } label: {
// Text("Type")
// }
// }
Section {
DatePicker(selection: $startingDate) {
Text(startingDate.formatted(.dateTime.weekday(.wide)).capitalized)
}
if eventType == .approvedTournament {
LabeledContent {
StepperView(count: $duration, minimum: 1)
} label: {
Text("Durée")
Text("\(duration) jour" + duration.pluralSuffix)
}
}
NavigationLink {
ClubsView() { club in
print("club", club.acronym)
selectedClub = club
}
} label: {
if let selectedClub {
ClubRowView(club: selectedClub)
} else {
Text("Choisir un club")
}
}
TextField("Nom de l'événement", text: $eventName)
} header: {
Text("Informations générales")
}
Section {
TipView(multiTournamentsEventTip) { action in
let tournament = Tournament.newEmptyInstance()
self.tournaments.append(tournament)
}
.tipStyle(tint: .orange)
}
switch eventType {
case .approvedTournament:
approvedTournamentEditorView
case .friendlyTournament:
approvedTournamentEditorView
case .simulation:
approvedTournamentEditorView
case .animation:
animationEditorView
}
Section {
RowButtonView("Valider") {
if tournaments.count > 1 || eventName.trimmed.isEmpty == false || selectedClub != nil {
let event = Event(name: eventName)
event.club = selectedClub?.id
tournaments.forEach { tournament in
tournament.event = event.id
}
try? dataStore.events.addOrUpdate(instance: event)
}
tournaments.forEach { tournament in
tournament.courtCount = selectedClub?.courts.count ?? 2
tournament.startDate = startingDate
tournament.dayDuration = duration
tournament.setupFederalSettings()
}
try? dataStore.tournaments.addOrUpdate(contentOfs: tournaments)
dismiss()
navigation.path.append(tournaments.first!)
}
.disabled(tournaments.isEmpty)
}
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Annuler", role: .cancel) {
dismiss()
}
}
}
.navigationTitle("Nouvel événement")
.navigationBarTitleDisplayMode(.large)
.toolbarBackground(.visible, for: .navigationBar)
}
}
@ViewBuilder
private var approvedTournamentEditorView: some View {
ForEach(tournaments) { tournament in
Section {
TournamentConfigurationView(tournament: tournament)
} footer: {
if tournaments.count > 1 {
HStack {
Spacer()
Button(role: .destructive) {
tournaments.removeAll(where: { $0 == tournament })
} label: {
LabelDelete()
}
.textCase(nil)
.font(.caption)
}
}
}
}
Section {
RowButtonView("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)
}