|
|
|
|
@ -15,18 +15,14 @@ struct TournamentRankView: View { |
|
|
|
|
|
|
|
|
|
@State private var rankings: [Int: [TeamRegistration]] = [:] |
|
|
|
|
@State private var calculating = false |
|
|
|
|
@State private var selectedTeam: TeamRegistration? |
|
|
|
|
@State private var assimilationSelection: TournamentLevel? |
|
|
|
|
|
|
|
|
|
var tournamentStore: TournamentStore? { |
|
|
|
|
return self.tournament.tournamentStore |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var isEditingTeam: Binding<Bool> { |
|
|
|
|
Binding { |
|
|
|
|
selectedTeam != nil |
|
|
|
|
} set: { value in |
|
|
|
|
} |
|
|
|
|
var tournamentLevel: TournamentLevel { |
|
|
|
|
assimilationSelection ?? tournament.tournamentLevel |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var hideRankings: Binding<Bool> { |
|
|
|
|
@ -70,6 +66,10 @@ struct TournamentRankView: View { |
|
|
|
|
} label: { |
|
|
|
|
Text("Assimilation") |
|
|
|
|
} |
|
|
|
|
.disabled(calculating) |
|
|
|
|
.onChange(of: assimilationSelection) { |
|
|
|
|
_refreshPointsEarned() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//affiche l'onglet sur le site, car sur le broadcast c'est dispo automatiquement de toute façon |
|
|
|
|
@ -134,9 +134,7 @@ struct TournamentRankView: View { |
|
|
|
|
if calculating == false && rankingsCalculated && teamsRanked.isEmpty == false { |
|
|
|
|
Section { |
|
|
|
|
ForEach(teamsRanked) { team in |
|
|
|
|
if let key = team.finalRanking { |
|
|
|
|
TeamRankCellView(team: team, key: key) |
|
|
|
|
} |
|
|
|
|
TeamRankCellView(team: team, tournamentLevel: tournamentLevel) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -149,30 +147,6 @@ struct TournamentRankView: View { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.alert("Position", isPresented: isEditingTeam) { |
|
|
|
|
if let selectedTeam { |
|
|
|
|
@Bindable var team = selectedTeam |
|
|
|
|
TextField("Position", value: $team.finalRanking, format: .number) |
|
|
|
|
.keyboardType(.numberPad) |
|
|
|
|
.multilineTextAlignment(.trailing) |
|
|
|
|
.frame(maxWidth: .infinity) |
|
|
|
|
|
|
|
|
|
Button("Valider") { |
|
|
|
|
selectedTeam.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: selectedTeam.finalRanking! - 1, count: tournament.teamCount) |
|
|
|
|
do { |
|
|
|
|
try self.tournamentStore?.teamRegistrations.addOrUpdate(instance: selectedTeam) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self.selectedTeam = nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Button("Annuler", role: .cancel) { |
|
|
|
|
self.selectedTeam = nil |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.onAppear { |
|
|
|
|
let rankingPublished = tournament.selectedSortedTeams().anySatisfy({ $0.finalRanking != nil }) |
|
|
|
|
if rankingPublished == false { |
|
|
|
|
@ -196,18 +170,23 @@ struct TournamentRankView: View { |
|
|
|
|
@EnvironmentObject var dataStore: DataStore |
|
|
|
|
@Environment(\.editMode) private var editMode |
|
|
|
|
@Environment(Tournament.self) var tournament: Tournament |
|
|
|
|
@State private var editedKey: Int = 0 |
|
|
|
|
@State private var isEditingTeam: Bool = false |
|
|
|
|
@Bindable var team: TeamRegistration |
|
|
|
|
@State var key: Int |
|
|
|
|
|
|
|
|
|
var key: Int { |
|
|
|
|
team.finalRanking ?? 0 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let tournamentLevel: TournamentLevel |
|
|
|
|
|
|
|
|
|
var body: some View { |
|
|
|
|
VStack { |
|
|
|
|
if editMode?.wrappedValue.isEditing == true { |
|
|
|
|
if key > 1 { |
|
|
|
|
FooterButtonView("monter") { |
|
|
|
|
key -= 1 |
|
|
|
|
team.finalRanking = key |
|
|
|
|
team.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: key - 1, count: tournament.teamCount) |
|
|
|
|
team.finalRanking = key - 1 |
|
|
|
|
team.pointsEarned = tournament.isAnimation() ? nil : tournamentLevel.points(for: key - 1, count: tournament.teamCount) |
|
|
|
|
do { |
|
|
|
|
try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) |
|
|
|
|
} catch { |
|
|
|
|
@ -218,6 +197,7 @@ struct TournamentRankView: View { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Button { |
|
|
|
|
editedKey = key |
|
|
|
|
isEditingTeam = true |
|
|
|
|
} label: { |
|
|
|
|
HStack { |
|
|
|
|
@ -293,7 +273,7 @@ struct TournamentRankView: View { |
|
|
|
|
if tournament.isAnimation() == false && key > 0 { |
|
|
|
|
VStack(alignment: .trailing) { |
|
|
|
|
HStack(alignment: .lastTextBaseline, spacing: 0.0) { |
|
|
|
|
Text(tournament.tournamentLevel.points(for: key - 1, count: tournament.teamCount).formatted(.number.sign(strategy: .always()))) |
|
|
|
|
Text(tournamentLevel.points(for: key - 1, count: tournament.teamCount).formatted(.number.sign(strategy: .always()))) |
|
|
|
|
Text("pts").font(.caption) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -306,9 +286,8 @@ struct TournamentRankView: View { |
|
|
|
|
|
|
|
|
|
if editMode?.wrappedValue.isEditing == true { |
|
|
|
|
FooterButtonView("descendre") { |
|
|
|
|
key += 1 |
|
|
|
|
team.finalRanking = key |
|
|
|
|
team.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: key - 1, count: tournament.teamCount) |
|
|
|
|
team.finalRanking = key + 1 |
|
|
|
|
team.pointsEarned = tournament.isAnimation() ? nil : tournamentLevel.points(for: key - 1, count: tournament.teamCount) |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) |
|
|
|
|
@ -319,13 +298,14 @@ struct TournamentRankView: View { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.alert("Position", isPresented: $isEditingTeam) { |
|
|
|
|
TextField("Position", value: $team.finalRanking, format: .number) |
|
|
|
|
TextField("Position", value: $editedKey, format: .number) |
|
|
|
|
.keyboardType(.numberPad) |
|
|
|
|
.multilineTextAlignment(.trailing) |
|
|
|
|
.frame(maxWidth: .infinity) |
|
|
|
|
|
|
|
|
|
Button("Valider") { |
|
|
|
|
team.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: key - 1, count: tournament.teamCount) |
|
|
|
|
team.finalRanking = editedKey |
|
|
|
|
team.pointsEarned = tournament.isAnimation() ? nil : tournamentLevel.points(for: editedKey - 1, count: tournament.teamCount) |
|
|
|
|
do { |
|
|
|
|
try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) |
|
|
|
|
} catch { |
|
|
|
|
@ -333,7 +313,7 @@ struct TournamentRankView: View { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
isEditingTeam = false |
|
|
|
|
} |
|
|
|
|
}.disabled(editedKey < 1) |
|
|
|
|
|
|
|
|
|
Button("Annuler", role: .cancel) { |
|
|
|
|
isEditingTeam = false |
|
|
|
|
@ -342,6 +322,10 @@ struct TournamentRankView: View { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private func _refreshPointsEarned() { |
|
|
|
|
tournament.refreshPointsEarned(assimilationLevel: assimilationSelection) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private func _calculateRankings() async { |
|
|
|
|
await MainActor.run { |
|
|
|
|
calculating = true |
|
|
|
|
|