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/Planning/SchedulerView.swift

122 lines
4.3 KiB

//
// SchedulerView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 07/04/2024.
//
import SwiftUI
extension GroupStage: Schedulable {
func titleLabel() -> String {
self.groupStageTitle()
}
}
extension Round: Schedulable {
func titleLabel() -> String {
self.roundTitle()
}
}
struct SchedulerView: View {
@EnvironmentObject var dataStore: DataStore
var tournament: Tournament
var destination: ScheduleDestination
var body: some View {
@Bindable var tournament = tournament
List {
switch destination {
case .scheduleGroupStage:
MatchFormatPickingView(matchFormat: $tournament.groupStageMatchFormat) {
Task {
MatchScheduler.shared.updateSchedule(tournament: tournament)
}
}
.onChange(of: tournament.groupStageMatchFormat) {
let groupStages = tournament.groupStages()
groupStages.forEach { groupStage in
groupStage.updateMatchFormat(tournament.groupStageMatchFormat)
}
try? dataStore.tournaments.addOrUpdate(instance: tournament)
try? dataStore.groupStages.addOrUpdate(contentOfs: groupStages)
}
ForEach(tournament.groupStages()) {
GroupStageScheduleEditorView(groupStage: $0, tournament: tournament)
.id(UUID())
}
case .scheduleBracket:
ForEach(tournament.rounds()) { round in
_roundView(round)
}
default:
EmptyView()
}
}
.headerProminence(.increased)
.monospacedDigit()
}
@ViewBuilder
func _roundView(_ round: Round) -> some View {
Section {
NavigationLink {
RoundScheduleEditorView(round: round, tournament: tournament)
.navigationTitle(round.titleLabel())
.environment(tournament)
} label: {
LabeledContent {
Text(round.matchFormat.format).font(.largeTitle)
} label: {
if let startDate = round.getStartDate() {
HStack {
Text(startDate.formattedAsHourMinute()).font(.largeTitle)
if let estimatedEndDate = round.estimatedEndDate(tournament.additionalEstimationDuration) {
Image(systemName: "arrowshape.forward.fill")
Text(estimatedEndDate.formattedAsHourMinute()).font(.largeTitle)
}
}
Text(startDate.formattedAsDate())
} else {
Text("Aucun horaire")
}
}
}
} header: {
Text(round.titleLabel())
}
Section {
NavigationLink {
LoserRoundScheduleEditorView(upperRound: round, tournament: tournament)
.environment(tournament)
} label: {
LabeledContent {
let count = round.loserRounds().filter({ $0.isDisabled() == false }).count
Text(count.formatted() + " tour" + count.pluralSuffix)
} label: {
if let startDate = round.getLoserRoundStartDate() {
HStack {
Text(startDate.formattedAsHourMinute()).font(.title3)
if let estimatedEndDate = round.estimatedLoserRoundEndDate(tournament.additionalEstimationDuration) {
Image(systemName: "arrowshape.forward.fill")
Text(estimatedEndDate.formattedAsHourMinute()).font(.title3)
}
}
Text(startDate.formattedAsDate())
} else {
Text("Aucun horaire")
}
}
}
} header: {
Text("Match de classement \(round.roundTitle(.short))")
}
}
}
#Preview {
SchedulerView(tournament: Tournament.mock(), destination: .scheduleBracket)
}