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.
106 lines
4.8 KiB
106 lines
4.8 KiB
//
|
|
// TournamentRankView.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Razmig Sarkissian on 30/04/2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LeStorage
|
|
|
|
struct TournamentRankView: View {
|
|
@Environment(Tournament.self) var tournament: Tournament
|
|
@EnvironmentObject var dataStore: DataStore
|
|
|
|
@State private var rankings: [Int: [TeamRegistration]] = [:]
|
|
|
|
var body: some View {
|
|
List {
|
|
let keys = rankings.keys.sorted()
|
|
ForEach(keys, id: \.self) { key in
|
|
if let rankedTeams = rankings[key] {
|
|
ForEach(rankedTeams) { team in
|
|
HStack {
|
|
VStack(alignment: .trailing) {
|
|
VStack(alignment: .trailing, spacing: -8.0) {
|
|
ZStack(alignment: .trailing) {
|
|
Text(tournament.teamCount.formatted()).hidden()
|
|
Text(key.formatted())
|
|
}
|
|
.monospacedDigit()
|
|
.font(.largeTitle)
|
|
.fontWeight(.bold)
|
|
Text(key.ordinalFormattedSuffix()).font(.caption)
|
|
}
|
|
if let index = tournament.indexOf(team: team) {
|
|
let rankingDifference = index - (key - 1)
|
|
if rankingDifference > 0 {
|
|
HStack(spacing: 0.0) {
|
|
Text(rankingDifference.formatted(.number.sign(strategy: .always())))
|
|
.monospacedDigit()
|
|
Image(systemName: "arrowtriangle.up.fill")
|
|
.imageScale(.small)
|
|
}
|
|
.foregroundColor(.green)
|
|
} else if rankingDifference < 0 {
|
|
HStack(spacing: 0.0) {
|
|
Text(rankingDifference.formatted(.number.sign(strategy: .always())))
|
|
.monospacedDigit()
|
|
Image(systemName: "arrowtriangle.down.fill")
|
|
.imageScale(.small)
|
|
}
|
|
.foregroundColor(.red)
|
|
} else {
|
|
Text("--")
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Divider()
|
|
|
|
VStack(alignment: .leading) {
|
|
ForEach(team.players()) { player in
|
|
VStack(alignment: .leading, spacing: -4.0) {
|
|
Text(player.playerLabel()).bold()
|
|
HStack(alignment: .firstTextBaseline, spacing: 0.0) {
|
|
Text(player.rankLabel())
|
|
if let rank = player.getRank() {
|
|
Text(rank.ordinalFormattedSuffix())
|
|
.font(.caption)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Spacer()
|
|
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("pts").font(.caption)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.listStyle(.grouped)
|
|
.onAppear {
|
|
let finalRanks = tournament.finalRanking()
|
|
finalRanks.keys.sorted().forEach { rank in
|
|
if let rankedTeamIds = finalRanks[rank] {
|
|
rankings[rank] = rankedTeamIds.compactMap { Store.main.findById($0) }
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Classement")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbarBackground(.visible, for: .navigationBar)
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
TournamentRankView()
|
|
}
|
|
|