parent
bb17216ec6
commit
15323dbb4a
@ -0,0 +1,141 @@ |
||||
// |
||||
// LoserRoundScheduleEditorView.swift |
||||
// PadelClub |
||||
// |
||||
// Created by Razmig Sarkissian on 14/04/2024. |
||||
// |
||||
|
||||
import SwiftUI |
||||
struct LoserRoundStepScheduleEditorView: View { |
||||
@EnvironmentObject var dataStore: DataStore |
||||
@Environment(Tournament.self) var tournament: Tournament |
||||
|
||||
var round: Round |
||||
var upperRound: Round |
||||
var matches: [Match] |
||||
@State private var startDate: Date |
||||
@State private var matchFormat: MatchFormat |
||||
|
||||
init(round: Round, upperRound: Round) { |
||||
self.upperRound = upperRound |
||||
self.round = round |
||||
let _matches = upperRound.loserRounds(forRoundIndex: round.index).flatMap({ $0.playedMatches() }) |
||||
self.matches = _matches |
||||
self._startDate = State(wrappedValue: round.startDate ?? _matches.first?.startDate ?? Date()) |
||||
self._matchFormat = State(wrappedValue: round.matchFormat) |
||||
} |
||||
|
||||
var body: some View { |
||||
@Bindable var round = round |
||||
Section { |
||||
MatchFormatPickerView(headerLabel: "Format", matchFormat: $round.matchFormat) |
||||
DatePicker(selection: $startDate) { |
||||
Text(startDate.formatted(.dateTime.weekday())) |
||||
} |
||||
|
||||
RowButtonView("Valider la modification") { |
||||
_updateSchedule() |
||||
} |
||||
|
||||
} header: { |
||||
Text(round.selectionLabel()) |
||||
} footer: { |
||||
NavigationLink { |
||||
List { |
||||
ForEach(matches) { match in |
||||
if match.disabled == false { |
||||
MatchScheduleEditorView(match: match) |
||||
} |
||||
} |
||||
} |
||||
.headerProminence(.increased) |
||||
.navigationTitle(round.selectionLabel()) |
||||
.environment(tournament) |
||||
} label: { |
||||
Text("voir tous les matchs") |
||||
} |
||||
} |
||||
.headerProminence(.increased) |
||||
} |
||||
|
||||
private func _updateSchedule() { |
||||
upperRound.loserRounds(forRoundIndex: round.index).forEach({ round in |
||||
round.resetRound(updateMatchFormat: round.matchFormat) |
||||
}) |
||||
|
||||
try? dataStore.matches.addOrUpdate(contentOfs: matches) |
||||
_save() |
||||
|
||||
MatchScheduler.shared.updateSchedule(tournament: tournament, fromRoundId: round.id, fromMatchId: nil, startDate: startDate) |
||||
_save() |
||||
} |
||||
|
||||
private func _save() { |
||||
try? dataStore.rounds.addOrUpdate(contentOfs: upperRound.loserRounds(forRoundIndex: round.index)) |
||||
} |
||||
} |
||||
|
||||
struct LoserRoundScheduleEditorView: View { |
||||
@EnvironmentObject var dataStore: DataStore |
||||
@Environment(Tournament.self) var tournament: Tournament |
||||
|
||||
var upperRound: Round |
||||
var loserRounds: [Round] |
||||
@State private var startDate: Date |
||||
@State private var matchFormat: MatchFormat |
||||
|
||||
init(upperRound: Round) { |
||||
self.upperRound = upperRound |
||||
let _loserRounds = upperRound.loserRounds() |
||||
self.loserRounds = _loserRounds |
||||
self._startDate = State(wrappedValue: _loserRounds.first?.startDate ?? _loserRounds.first?.playedMatches().first?.startDate ?? Date()) |
||||
self._matchFormat = State(wrappedValue: _loserRounds.first?.matchFormat ?? upperRound.matchFormat) |
||||
} |
||||
|
||||
var body: some View { |
||||
List { |
||||
Section { |
||||
MatchFormatPickerView(headerLabel: "Format", matchFormat: $matchFormat) |
||||
DatePicker(selection: $startDate) { |
||||
Text(startDate.formatted(.dateTime.weekday())) |
||||
} |
||||
RowButtonView("Valider la modification") { |
||||
_updateSchedule() |
||||
} |
||||
} header: { |
||||
Text("Classement " + upperRound.roundTitle()) |
||||
} |
||||
|
||||
|
||||
ForEach(upperRound.loserRounds()) { loserRound in |
||||
if loserRound.isDisabled() == false { |
||||
LoserRoundStepScheduleEditorView(round: loserRound, upperRound: upperRound) |
||||
} |
||||
} |
||||
} |
||||
.headerProminence(.increased) |
||||
.navigationTitle("Réglages") |
||||
.toolbarBackground(.visible, for: .navigationBar) |
||||
.navigationBarTitleDisplayMode(.inline) |
||||
} |
||||
|
||||
private func _updateSchedule() { |
||||
let matches = upperRound.loserRounds().flatMap({ round in |
||||
round.playedMatches() |
||||
}) |
||||
upperRound.loserRounds().forEach({ round in |
||||
round.resetRound(updateMatchFormat: matchFormat) |
||||
}) |
||||
|
||||
try? dataStore.matches.addOrUpdate(contentOfs: matches) |
||||
_save() |
||||
|
||||
MatchScheduler.shared.updateSchedule(tournament: tournament, fromRoundId: upperRound.loserRounds().first?.id, fromMatchId: nil, startDate: startDate) |
||||
_save() |
||||
} |
||||
|
||||
private func _save() { |
||||
try? dataStore.rounds.addOrUpdate(contentOfs: upperRound.loserRounds()) |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue