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.
175 lines
6.0 KiB
175 lines
6.0 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
|
|
@State private var eventType: EventType = .approvedTournament
|
|
@State private var animationType: AnimationType = .upAndDown
|
|
@State private var startingDate: Date = Date().tomorrowAtNine
|
|
@State private var duration: Int = 3
|
|
@State private var eventName: String = ""
|
|
@State var tournaments: [Tournament] = []
|
|
@State private 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 {
|
|
Stepper(value: $duration, in: 1...3) {
|
|
HStack {
|
|
Text("Durée")
|
|
Spacer()
|
|
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.startDate = startingDate
|
|
tournament.dayDuration = duration
|
|
}
|
|
|
|
try? dataStore.tournaments.addOrUpdate(contentOfs: tournaments)
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
.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)
|
|
}
|
|
|