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

114 lines
3.5 KiB

//
// TournamentScheduleView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 07/04/2024.
//
import SwiftUI
import PadelClubData
protocol Schedulable: Identifiable {
var startDate: Date? { get set }
var matchFormat: MatchFormat { get set }
func playedMatches() -> [Match]
func titleLabel() -> String
}
extension Schedulable {
func getStartDate() -> Date? {
return startDate ?? playedMatches().first?.startDate
}
}
enum ScheduleDestination: String, Identifiable, Selectable, Equatable {
static func == (lhs: ScheduleDestination, rhs: ScheduleDestination) -> Bool {
return lhs.id == rhs.id
}
var id: String { self.rawValue }
case planning
case planningByCourt
case scheduleGroupStage
case scheduleBracket
func selectionLabel(index: Int) -> String {
switch self {
case .scheduleGroupStage:
return "Poules"
case .scheduleBracket:
return "Tableau"
case .planning:
return "Horaires"
case .planningByCourt:
return "Prog."
}
}
func badgeValue() -> Int? {
return nil
}
func badgeValueColor() -> Color? {
return nil
}
func badgeImage() -> Badge? {
return nil
}
}
struct TournamentScheduleView: View {
var tournament: Tournament
@State private var selectedScheduleDestination: ScheduleDestination? = .planning
let allDestinations: [ScheduleDestination]
init(tournament: Tournament) {
self.tournament = tournament
var destinations = [ScheduleDestination.planning, ScheduleDestination.planningByCourt]
if tournament.groupStages().isEmpty == false {
destinations.append(.scheduleGroupStage)
}
if tournament.rounds().isEmpty == false {
destinations.append(.scheduleBracket)
}
self.allDestinations = destinations
}
var body: some View {
VStack(spacing: 0) {
GenericDestinationPickerView(selectedDestination: $selectedScheduleDestination, destinations: allDestinations, nilDestinationIsValid: true)
let allMatches = tournament.allMatches()
switch selectedScheduleDestination {
case .none:
PlanningSettingsView(tournament: tournament)
case .some(let selectedSchedule):
switch selectedSchedule {
case .scheduleGroupStage:
SchedulerView(tournament: tournament, destination: selectedSchedule)
case .scheduleBracket:
SchedulerView(tournament: tournament, destination: selectedSchedule)
case .planning:
PlanningView(matches: allMatches, selectedScheduleDestination: $selectedScheduleDestination)
case .planningByCourt:
PlanningByCourtView(matches: allMatches, selectedScheduleDestination: $selectedScheduleDestination, startDate: allMatches.filter({ $0.isRunning() }).sorted(by: \.computedStartDateForSorting).first?.startDate ?? tournament.startDate)
}
}
}
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(.visible, for: .navigationBar)
.navigationTitle("Horaires et formats")
.ifAvailableiOS26 {
if #available(iOS 26.0, *) {
$0.navigationSubtitle(tournament.tournamentTitle())
}
}
}
}
//#Preview {
// TournamentScheduleView(tournament: Tournament.mock())
//}