parent
e8d41853ff
commit
b611ee9afd
@ -0,0 +1,107 @@ |
||||
// |
||||
// EventStatusView.swift |
||||
// PadelClub |
||||
// |
||||
// Created by Razmig Sarkissian on 04/06/2025. |
||||
// |
||||
|
||||
import SwiftUI |
||||
import PadelClubData |
||||
|
||||
struct EventStatusView: View { |
||||
@State private var teamsCount: Int? |
||||
|
||||
let tournaments: [Tournament] |
||||
|
||||
init(tournament: Tournament) { |
||||
self.tournaments = [tournament] |
||||
} |
||||
|
||||
init(event: Event) { |
||||
self.tournaments = event.confirmedTournaments() |
||||
} |
||||
|
||||
init(tournaments: [Tournament]) { |
||||
self.tournaments = tournaments |
||||
} |
||||
|
||||
private func _calculateTeamsCount() async { |
||||
Task { |
||||
self.teamsCount = tournaments.map({ $0.selectedSortedTeams().count }).reduce(0, +) |
||||
} |
||||
} |
||||
|
||||
private func _valueView<T: Numeric & CustomStringConvertible>(value: T, value2: T? = nil) -> some View { |
||||
if let value2 { |
||||
Text("\(value.description)/\(value2.description)") |
||||
.font(.title3) |
||||
} else { |
||||
Text("\(value.description)") |
||||
.font(.title3) |
||||
} |
||||
} |
||||
|
||||
private func _currencyView(value: Double, value2: Double? = nil) -> some View { |
||||
|
||||
let maps = [value, value2].compactMap({ $0 }).map { |
||||
$0.formatted(.currency(code: Locale.defaultCurrency()).precision(.fractionLength(0))) |
||||
} |
||||
|
||||
let string = maps.joined(separator: " / ") |
||||
return Text(string) |
||||
} |
||||
|
||||
|
||||
var body: some View { |
||||
List { |
||||
Section { |
||||
LabeledContent { |
||||
_valueView(value: tournaments.count) |
||||
} label: { |
||||
Text("Tournois") |
||||
} |
||||
|
||||
LabeledContent { |
||||
if let teamsCount { |
||||
_valueView(value: teamsCount, value2: tournaments.map({ $0.teamCount }).reduce(0, +)) |
||||
} else { |
||||
ProgressView() |
||||
} |
||||
} label: { |
||||
Text("Inscriptions") |
||||
} |
||||
|
||||
LabeledContent { |
||||
_currencyView(value: tournaments.map({ $0.earnings() }).reduce(0, +), value2: tournaments.map({ $0.totalIncome() }).reduce(0, +)) |
||||
} label: { |
||||
Text("Recettes") |
||||
} |
||||
} header: { |
||||
if let tournament = tournaments.first, tournaments.count == 1 { |
||||
Text(tournament.tournamentTitle()) |
||||
} else { |
||||
Text("Statistiques globales") |
||||
} |
||||
} |
||||
|
||||
if tournaments.count > 1 { |
||||
Section { |
||||
ForEach(tournaments) { tournament in |
||||
NavigationLink(destination: EventStatusView(tournament: tournament)) { |
||||
LabeledContent { |
||||
_valueView(value: tournament.selectedSortedTeams().count, value2: tournament.teamCount) |
||||
} label: { |
||||
Text(tournament.tournamentTitle()) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
.task { |
||||
await self._calculateTeamsCount() |
||||
} |
||||
.toolbarBackground(.visible, for: .navigationBar) |
||||
.navigationBarTitleDisplayMode(.inline) |
||||
} |
||||
} |
||||
Loading…
Reference in new issue