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

192 lines
7.0 KiB

//
// TournamentView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 29/02/2024.
//
import SwiftUI
import LeStorage
import TipKit
struct TournamentView: View {
@EnvironmentObject var dataStore: DataStore
@Environment(NavigationViewModel.self) var navigation: NavigationViewModel
@State var tournament: Tournament
var presentationContext: PresentationContext = .agenda
let tournamentSelectionTip = TournamentSelectionTip()
let tournamentRunningTip = TournamentRunningTip()
var selectedTournamentId: Binding<String> { Binding(
get: { tournament.id },
set: { id in
if let tournamentFound: Tournament = Store.main.findById(id) {
tournament = tournamentFound
}
}
)}
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 {
TipView(tournamentRunningTip)
.tipStyle(tint: nil)
if tournament.state() != .finished {
SubscriptionInfoView()
}
switch tournament.state() {
case .canceled:
Section {
RowButtonView("Reprendre le tournoi", role: .destructive) {
tournament.isCanceled = false
_save()
}
} footer: {
Text("todo expliquer cet état")
}
case .initial:
TournamentInscriptionView(tournament: tournament)
TournamentInitView(tournament: tournament)
case .build:
TournamentInscriptionView(tournament: tournament)
TournamentInitView(tournament: tournament)
TournamentBuildView(tournament: tournament)
case .running, .finished:
TournamentInscriptionView(tournament: tournament)
TournamentBuildView(tournament: tournament)
TournamentRunningView(tournament: tournament)
}
}
}
.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()
case .event:
if let event = tournament.eventObject() {
EventView(event: event)
}
case .print:
PrintSettingsView(tournament: tournament)
}
}
.environment(tournament)
})
.navigationBarTitleDisplayMode(.inline)
.toolbarTitleMenu {
if let event = tournament.eventObject() {
Picker(selection: selectedTournamentId) {
ForEach(event.tournaments) { tournament in
Text(tournament.tournamentTitle()).tag(tournament.id as String)
}
} label: {
}
}
}
.toolbarBackground(.visible, for: .navigationBar)
.toolbar {
ToolbarItem(placement: .principal) {
VStack(spacing: -4.0) {
Text(tournament.tournamentTitle(.short)).font(.headline)
Text(tournament.formattedDate())
.font(.subheadline).foregroundStyle(.secondary)
}
.popoverTip(tournamentSelectionTip)
.onAppear {
TournamentSelectionTip.tournamentCount = tournament.eventObject()?.tournaments.count
}
}
if presentationContext == .agenda || tournament.state() == .running {
ToolbarItem(placement: .topBarTrailing) {
Menu {
if presentationContext == .agenda {
Button {
navigation.openTournamentInOrganizer(tournament)
} label: {
Label("Voir dans le gestionnaire", systemImage: "line.diagonal.arrow")
}
}
Divider()
if tournament.state() == .running || tournament.state() == .finished {
NavigationLink(value: Screen.event) {
Text("Gestion de l'événement")
}
NavigationLink(value: Screen.settings) {
LabelSettings()
}
NavigationLink(value: Screen.structure) {
LabelStructure()
}
NavigationLink(value: Screen.broadcast) {
Label("Publication", systemImage: "airplayvideo")
}
NavigationLink(value: Screen.print) {
Label("Imprimer", systemImage: "printer")
}
}
} label: {
LabelOptions()
}
}
}
}
.onAppear {
TournamentRunningTip.isRunning = tournament.state() == .running
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(tournament: Tournament.mock(), presentationContext: .agenda)
}
}