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/Tournament/Screen/Components/TournamentMatchFormatsSetti...

113 lines
3.6 KiB

//
// TournamentMatchFormatsSettingsView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 18/04/2024.
//
import SwiftUI
import LeStorage
struct TournamentMatchFormatsSettingsView: View {
@Environment(NavigationViewModel.self) var navigation: NavigationViewModel
@Environment(Tournament.self) var tournament: Tournament
@EnvironmentObject var dataStore: DataStore
@State private var confirmUpdate: Bool = false
@State private var updateCompleted: Bool = false
var body: some View {
@Bindable var tournament = tournament
List {
if confirmUpdate {
RowButtonView("Modifier les matchs existants", role: .destructive) {
_updateAllFormat()
}
}
TournamentFormatSelectionView()
Section {
LabeledContent {
StepperView(title: "minutes", count: $tournament.additionalEstimationDuration, step: 5)
} label: {
Text("Modifier les durées moyennes")
}
} footer: {
Text("Cette valeur est rajoutée ou soustraite aux valeurs par défaut. Par exemple, cela peut aider à mieux planifier un tournoi débutant ou jeune.")
}
Section {
NavigationLink {
DurationSettingsView()
} label: {
Label("Définir les durées moyennes", systemImage: "deskclock")
}
} footer: {
Text("Vous pouvez définir vos propres estimations de durées de match en fonction du format de jeu.")
}
}
.onChange(of: [tournament.loserBracketMatchFormat,
tournament.groupStageMatchFormat,
tournament.matchFormat,
]) {
_save()
_confirmOrSave()
}
.onChange(of: tournament.additionalEstimationDuration) {
_save()
}
.onChange(of: dataStore.appSettings.matchFormatsDefaultDuration) {
_confirmOrSave()
}
.overlay(alignment: .bottom) {
if updateCompleted {
Label("Formats mis à jour", systemImage: "checkmark.circle.fill")
.toastFormatted()
.deferredRendering(for: .seconds(2))
}
}
}
private func _confirmOrSave() {
switch tournament.state() {
case .build:
confirmUpdate = true
default:
break
}
}
private func _updateAllFormat() {
updateCompleted = false
let groupStages = tournament.groupStages()
groupStages.forEach { groupStage in
groupStage.updateMatchFormat(tournament.groupStageMatchFormat)
}
let allRounds = tournament.allRounds()
allRounds.forEach { round in
if round.isLoserBracket() {
round.updateMatchFormatAndAllMatches(tournament.loserBracketMatchFormat)
} else {
round.updateMatchFormatAndAllMatches(tournament.matchFormat)
}
}
try? dataStore.groupStages.addOrUpdate(contentOfs: groupStages)
try? dataStore.rounds.addOrUpdate(contentOfs: allRounds)
confirmUpdate = false
updateCompleted = true
}
private func _save() {
do {
try dataStore.tournaments.addOrUpdate(instance: tournament)
} catch {
Logger.error(error)
}
}
}
#Preview {
TournamentMatchFormatsSettingsView()
}