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/TournamentView.swift

179 lines
6.7 KiB

//
// TournamentView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 29/02/2024.
//
import SwiftUI
import LeStorage
struct TournamentView: View {
@EnvironmentObject var dataStore: DataStore
@Environment(Tournament.self) var tournament: Tournament
var presentationContext: PresentationContext = .agenda
var lastDataSource: String? {
dataStore.appSettings.lastDataSource
}
var _lastDataSourceDate: Date? {
guard let lastDataSource else { return nil }
return URL.importDateFormatter.date(from: lastDataSource)
}
var body: some View {
VStack(spacing: 0.0) {
List {
SubscriptionInfoView()
if tournament.state() != .canceled {
Section {
NavigationLink(value: Screen.inscription) {
LabeledContent {
Text(tournament.unsortedTeams().count.formatted() + "/" + tournament.teamCount.formatted())
} label: {
Text("Gestion des inscriptions")
if let closedRegistrationDate = tournament.closedRegistrationDate {
Text("clôturé le " + closedRegistrationDate.formatted(date: .abbreviated, time: .shortened))
}
}
}
if let endOfInscriptionDate = tournament.mandatoryRegistrationCloseDate(), tournament.inscriptionClosed() == false && tournament.hasStarted() == false {
LabeledContent {
Text(endOfInscriptionDate.formatted(date: .abbreviated, time: .shortened))
} label: {
Text("Date limite")
}
}
} footer: {
if tournament.inscriptionClosed() == false && tournament.state() == .build && tournament.unsortedTeams().isEmpty == false && tournament.hasStarted() == false {
Button {
tournament.lockRegistration()
_save()
} label: {
Text("clôturer les inscriptions")
.underline()
}
.buttonStyle(.borderless)
}
}
}
switch tournament.state() {
case .canceled:
Section {
RowButtonView("Reprendre le tournoi", role: .destructive) {
tournament.isCanceled = false
_save()
}
} footer: {
Text("todo expliquer cet état")
}
case .initial:
TournamentInitView()
NavigationLink(value: Screen.broadcast) {
Text("Diffusion")
}
case .build:
TournamentRunningView(tournament: tournament)
if tournament.hasEnded() {
NavigationLink(value: Screen.rankings) {
Text("Classement")
}
}
}
}
}
.toolbarBackground(.visible, for: .navigationBar)
.navigationDestination(for: Screen.self, destination: { screen in
Group {
switch screen {
case .structure:
TableStructureView()
case .settings:
TournamentSettingsView()
case .inscription:
InscriptionManagerView(tournament: tournament)
case .groupStage:
GroupStagesView(tournament: tournament)
case .round:
RoundsView(tournament: tournament)
case .schedule:
TournamentScheduleView(tournament: tournament)
case .cashier:
TournamentCashierView(tournament: tournament)
case .call:
TournamentCallView(tournament: tournament)
case .rankings:
TournamentRankView()
case .broadcast:
BroadcastView()
}
}
.environment(tournament)
})
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(.visible, for: .navigationBar)
.toolbar {
ToolbarItem(placement: .principal) {
VStack(spacing: -4.0) {
Text(tournament.tournamentTitle()).font(.headline)
Text(tournament.formattedDate())
.font(.subheadline).foregroundStyle(.secondary)
}
}
ToolbarItem(placement: .topBarTrailing) {
Menu {
if presentationContext == .agenda {
Button {
} label: {
Label("Voir dans le gestionnaire", systemImage: "line.diagonal.arrow")
}
}
Divider()
if tournament.state() == .build {
NavigationLink(value: Screen.settings) {
LabelSettings()
}
NavigationLink(value: Screen.structure) {
LabelStructure()
}
NavigationLink(value: Screen.rankings) {
Text("Classement")
}
NavigationLink(value: Screen.broadcast) {
Text("Diffusion")
}
}
} label: {
LabelOptions()
}
}
}
.onAppear {
Logger.log("Payment = \(String(describing: self.tournament.payment)), canceled = \(self.tournament.isCanceled)")
}
}
private func _save() {
do {
try dataStore.tournaments.addOrUpdate(instance: tournament)
} catch {
Logger.error(error)
}
}
}
#Preview {
NavigationStack {
TournamentView(presentationContext: .agenda)
.environment(Tournament.mock())
}
}