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.
164 lines
5.4 KiB
164 lines
5.4 KiB
//
|
|
// TournamentGeneralSettingsView.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Razmig Sarkissian on 18/04/2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LeStorage
|
|
|
|
struct TournamentGeneralSettingsView: View {
|
|
@EnvironmentObject var dataStore: DataStore
|
|
|
|
@Bindable var tournament: Tournament
|
|
@State private var tournamentName: String = ""
|
|
@State private var entryFee: Double? = nil
|
|
@FocusState private var focusedField: Tournament.CodingKeys?
|
|
|
|
init(tournament: Tournament) {
|
|
self.tournament = tournament
|
|
_tournamentName = State(wrappedValue: tournament.name ?? "")
|
|
_entryFee = State(wrappedValue: tournament.entryFee)
|
|
}
|
|
|
|
var body: some View {
|
|
@Bindable var tournament = tournament
|
|
Form {
|
|
Section {
|
|
TournamentDatePickerView()
|
|
TournamentDurationManagerView()
|
|
}
|
|
|
|
Section {
|
|
TournamentLevelPickerView()
|
|
}
|
|
|
|
Section {
|
|
Picker(selection: $tournament.loserBracketMode) {
|
|
ForEach(LoserBracketMode.allCases) {
|
|
Text($0.localizedLoserBracketMode()).tag($0)
|
|
}
|
|
} label: {
|
|
Text("Position des perdants")
|
|
}
|
|
.onChange(of: tournament.loserBracketMode) {
|
|
|
|
_save()
|
|
|
|
let rounds = tournament.rounds()
|
|
rounds.forEach { round in
|
|
round.loserBracketMode = tournament.loserBracketMode
|
|
}
|
|
|
|
do {
|
|
try self.tournament.tournamentStore.rounds.addOrUpdate(contentOfs: rounds)
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
}
|
|
} header: {
|
|
Text("Matchs de classement")
|
|
} footer: {
|
|
if dataStore.user.loserBracketMode != tournament.loserBracketMode {
|
|
_footerView()
|
|
.onTapGesture(perform: {
|
|
self.dataStore.user.loserBracketMode = tournament.loserBracketMode
|
|
self.dataStore.saveUser()
|
|
})
|
|
} else {
|
|
Text(tournament.loserBracketMode.localizedLoserBracketModeDescription())
|
|
}
|
|
}
|
|
|
|
Section {
|
|
LabeledContent {
|
|
TextField(tournament.isFree() ? "Gratuite" : "Inscription", value: $entryFee, format: .currency(code: Locale.current.currency?.identifier ?? "EUR"))
|
|
.keyboardType(.decimalPad)
|
|
.multilineTextAlignment(.trailing)
|
|
.frame(maxWidth: .infinity)
|
|
.focused($focusedField, equals: ._entryFee)
|
|
} label: {
|
|
Text("Inscription")
|
|
}
|
|
}
|
|
|
|
Section {
|
|
TextField("Nom du tournoi", text: $tournamentName, axis: .vertical)
|
|
.lineLimit(2)
|
|
.frame(maxWidth: .infinity)
|
|
.keyboardType(.alphabet)
|
|
.focused($focusedField, equals: ._name)
|
|
}
|
|
}
|
|
.toolbarBackground(.visible, for: .navigationBar)
|
|
.toolbar {
|
|
if focusedField != nil {
|
|
ToolbarItem(placement: .keyboard) {
|
|
HStack {
|
|
Spacer()
|
|
Button("Valider") {
|
|
if focusedField == ._name {
|
|
if tournamentName.trimmed.isEmpty {
|
|
tournament.name = nil
|
|
} else {
|
|
tournament.name = tournamentName
|
|
}
|
|
} else if focusedField == ._entryFee {
|
|
tournament.entryFee = entryFee
|
|
}
|
|
focusedField = nil
|
|
}
|
|
.buttonStyle(.bordered)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.onChange(of: tournament.startDate) {
|
|
_save()
|
|
}
|
|
.onChange(of: tournament.entryFee) {
|
|
_save()
|
|
}
|
|
.onChange(of: tournament.name) {
|
|
_save()
|
|
}
|
|
.onChange(of: tournament.dayDuration) {
|
|
_save()
|
|
}
|
|
.onChange(of: [
|
|
tournament.federalCategory,
|
|
]) {
|
|
_save()
|
|
}
|
|
.onChange(of: [
|
|
tournament.federalLevelCategory,
|
|
]) {
|
|
_save()
|
|
}
|
|
.onChange(of: [
|
|
tournament.federalAgeCategory,
|
|
]) {
|
|
_save()
|
|
}
|
|
.onChange(of: [
|
|
tournament.groupStageSortMode,
|
|
]) {
|
|
_save()
|
|
}
|
|
}
|
|
|
|
private func _save() {
|
|
do {
|
|
try dataStore.tournaments.addOrUpdate(instance: tournament)
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
}
|
|
|
|
private func _footerView() -> some View {
|
|
Text(tournament.loserBracketMode.localizedLoserBracketModeDescription())
|
|
+
|
|
Text(" Modifier le réglage par défaut pour tous vos tournois").foregroundStyle(.blue)
|
|
}
|
|
}
|
|
|