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

215 lines
7.5 KiB

//
// EventCreationView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 22/03/2024.
//
import SwiftUI
import TipKit
import LeStorage
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?
@FocusState private var textFieldIsFocus: Bool
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, displayContext: .selection)
} else {
Text("Choisir un club")
}
}
VStack(alignment: .leading) {
Text("Nom de l'événement").font(.footnote)
ZStack {
Text(eventName).hidden()
TextEditor(text: $eventName)
.focused($textFieldIsFocus)
.autocorrectionDisabled()
.keyboardType(.alphabet)
}
}
LabeledContent {
Text(tournaments.count.formatted())
} label: {
Text("Nombre d'épreuve")
}
} header: {
Text("")
}
switch eventType {
case .approvedTournament:
approvedTournamentEditorView
case .friendlyTournament:
approvedTournamentEditorView
case .simulation:
approvedTournamentEditorView
case .animation:
animationEditorView
}
Section {
RowButtonView("Valider") {
let event = Event(creator: dataStore.user.id, name: eventName)
event.club = selectedClub?.id
tournaments.forEach { tournament in
tournament.event = event.id
}
do {
try dataStore.events.addOrUpdate(instance: event)
} catch {
Logger.error(error)
}
tournaments.forEach { tournament in
tournament.courtCount = selectedClub?.courtCount ?? 2
tournament.startDate = startingDate
tournament.dayDuration = duration
tournament.setupFederalSettings()
}
do {
try dataStore.tournaments.addOrUpdate(contentOfs: tournaments)
} catch {
Logger.error(error)
}
dismiss()
navigation.path.append(tournaments.first!)
}
.disabled(tournaments.isEmpty)
}
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Annuler", role: .cancel) {
dismiss()
}
}
ToolbarItem(placement: .topBarTrailing) {
BarButtonView("Ajouter une épreuve", icon: "plus.circle.fill") {
let tournament = Tournament.newEmptyInstance()
self.tournaments.append(tournament)
}
.popoverTip(multiTournamentsEventTip)
}
ToolbarItemGroup(placement: .keyboard) {
if textFieldIsFocus {
Spacer()
Button {
textFieldIsFocus = false
} label: {
Text("Valider")
}
.buttonStyle(.bordered)
}
}
}
.navigationTitle("Nouvel événement")
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(.visible, for: .navigationBar)
.onAppear {
if selectedClub == nil {
selectedClub = dataStore.user.clubsObjects().first
}
}
}
}
@ViewBuilder
private var approvedTournamentEditorView: some View {
ForEach(tournaments) { tournament in
Section {
TournamentConfigurationView(tournament: tournament)
} footer: {
if tournaments.count > 1 {
FooterButtonView("effacer") {
tournaments.removeAll(where: { $0 == tournament })
}
}
}
}
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)
}