|
|
|
|
@ -14,13 +14,21 @@ struct TournamentRankView: View { |
|
|
|
|
|
|
|
|
|
@State private var rankings: [Int: [TeamRegistration]] = [:] |
|
|
|
|
@State private var calculating = false |
|
|
|
|
@State private var selectedTeam: TeamRegistration? |
|
|
|
|
|
|
|
|
|
var isEditingTeam: Binding<Bool> { |
|
|
|
|
Binding { |
|
|
|
|
selectedTeam != nil |
|
|
|
|
} set: { value in |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var body: some View { |
|
|
|
|
List { |
|
|
|
|
@Bindable var tournament = tournament |
|
|
|
|
Section { |
|
|
|
|
let matchs = tournament.runningMatches(tournament.allMatches()) |
|
|
|
|
let rankingPublished = tournament.selectedSortedTeams().allSatisfy({ $0.finalRanking != nil }) |
|
|
|
|
Section { |
|
|
|
|
LabeledContent { |
|
|
|
|
Text(matchs.count.formatted()) |
|
|
|
|
} label: { |
|
|
|
|
@ -30,8 +38,10 @@ struct TournamentRankView: View { |
|
|
|
|
LabeledContent { |
|
|
|
|
if rankingPublished { |
|
|
|
|
Image(systemName: "checkmark") |
|
|
|
|
.foregroundStyle(.green) |
|
|
|
|
} else { |
|
|
|
|
Image(systemName: "xmark") |
|
|
|
|
.foregroundStyle(.logoRed) |
|
|
|
|
} |
|
|
|
|
} label: { |
|
|
|
|
Text("Classement publié") |
|
|
|
|
@ -41,38 +51,37 @@ struct TournamentRankView: View { |
|
|
|
|
Text("Masquer les points gagnés") |
|
|
|
|
} |
|
|
|
|
.onChange(of: tournament.hidePointsEarned) { |
|
|
|
|
do { |
|
|
|
|
try dataStore.tournaments.addOrUpdate(instance: tournament) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
dataStore.tournaments.addOrUpdate(instance: tournament) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
RowButtonView(rankingPublished ? "Re-publier le classement" : "Publier le classement", role: .destructive) { |
|
|
|
|
rankings.keys.sorted().forEach { rank in |
|
|
|
|
if let rankedTeams = rankings[rank] { |
|
|
|
|
rankedTeams.forEach { team in |
|
|
|
|
team.finalRanking = rank |
|
|
|
|
team.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: rank - 1, count: tournament.teamCount) |
|
|
|
|
} |
|
|
|
|
if rankingPublished == false { |
|
|
|
|
RowButtonView("Publier le classement", role: .destructive) { |
|
|
|
|
_publishRankings() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
_save() |
|
|
|
|
} |
|
|
|
|
} footer: { |
|
|
|
|
FooterButtonView("masquer le classement", role: .destructive) { |
|
|
|
|
|
|
|
|
|
if rankingPublished { |
|
|
|
|
Section { |
|
|
|
|
RowButtonView("Supprimer le classement", role: .destructive) { |
|
|
|
|
tournament.unsortedTeams().forEach { team in |
|
|
|
|
team.finalRanking = nil |
|
|
|
|
team.pointsEarned = nil |
|
|
|
|
} |
|
|
|
|
_save() |
|
|
|
|
} |
|
|
|
|
} footer: { |
|
|
|
|
Text(.init("Masque également le classement sur le site [Padel Club](\(URLs.main.rawValue))")) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let keys = rankings.keys.sorted() |
|
|
|
|
ForEach(keys, id: \.self) { key in |
|
|
|
|
if let rankedTeams = rankings[key] { |
|
|
|
|
ForEach(rankedTeams) { team in |
|
|
|
|
if rankingPublished { |
|
|
|
|
Section { |
|
|
|
|
ForEach(tournament.teamsRanked()) { team in |
|
|
|
|
let key = team.finalRanking! |
|
|
|
|
Button { |
|
|
|
|
selectedTeam = team |
|
|
|
|
} label: { |
|
|
|
|
HStack { |
|
|
|
|
VStack(alignment: .trailing) { |
|
|
|
|
VStack(alignment: .trailing, spacing: -8.0) { |
|
|
|
|
@ -141,7 +150,40 @@ struct TournamentRankView: View { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.frame(maxWidth: .infinity) |
|
|
|
|
} |
|
|
|
|
.contentShape(Rectangle()) |
|
|
|
|
.buttonStyle(.plain) |
|
|
|
|
} |
|
|
|
|
} footer: { |
|
|
|
|
Text("Vous pouvez appuyer sur une ligne pour éditer manuellement le classement calculé par Padel Club.") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// let keys = rankings.keys.sorted() |
|
|
|
|
// ForEach(keys, id: \.self) { key in |
|
|
|
|
// if let rankedTeams = rankings[key] { |
|
|
|
|
// ForEach(rankedTeams) { team in |
|
|
|
|
// |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
} |
|
|
|
|
.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) |
|
|
|
|
dataStore.teamRegistrations.addOrUpdate(instance: selectedTeam) |
|
|
|
|
self.selectedTeam = nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Button("Annuler", role: .cancel) { |
|
|
|
|
self.selectedTeam = nil |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -151,25 +193,41 @@ struct TournamentRankView: View { |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.onAppear { |
|
|
|
|
let rankingPublished = tournament.selectedSortedTeams().allSatisfy({ $0.finalRanking != nil }) |
|
|
|
|
if rankingPublished == false { |
|
|
|
|
calculating = true |
|
|
|
|
Task { |
|
|
|
|
await calculateRankings() |
|
|
|
|
await _calculateRankings() |
|
|
|
|
_publishRankings() |
|
|
|
|
calculating = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.navigationTitle("Classement") |
|
|
|
|
.navigationBarTitleDisplayMode(.inline) |
|
|
|
|
.toolbarBackground(.visible, for: .navigationBar) |
|
|
|
|
.toolbar { |
|
|
|
|
ToolbarItem(placement: .topBarTrailing) { |
|
|
|
|
if let url = tournament.shareURL(.rankings) { |
|
|
|
|
actionForURL(url) |
|
|
|
|
_actionForURL(url) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private func _publishRankings() { |
|
|
|
|
rankings.keys.sorted().forEach { rank in |
|
|
|
|
if let rankedTeams = rankings[rank] { |
|
|
|
|
rankedTeams.forEach { team in |
|
|
|
|
team.finalRanking = rank |
|
|
|
|
team.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: rank - 1, count: tournament.teamCount) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
_save() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func calculateRankings() async { |
|
|
|
|
private func _calculateRankings() async { |
|
|
|
|
let finalRanks = await tournament.finalRanking() |
|
|
|
|
finalRanks.keys.sorted().forEach { rank in |
|
|
|
|
if let rankedTeamIds = finalRanks[rank] { |
|
|
|
|
@ -179,7 +237,7 @@ struct TournamentRankView: View { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ViewBuilder |
|
|
|
|
func actionForURL(_ url: URL, removeSource: Bool = false) -> some View { |
|
|
|
|
private func _actionForURL(_ url: URL, removeSource: Bool = false) -> some View { |
|
|
|
|
Menu { |
|
|
|
|
Button { |
|
|
|
|
UIApplication.shared.open(url) |
|
|
|
|
|