diff --git a/PadelClub/Views/Cashier/CashierDetailView.swift b/PadelClub/Views/Cashier/CashierDetailView.swift index fd5952e..860a0b1 100644 --- a/PadelClub/Views/Cashier/CashierDetailView.swift +++ b/PadelClub/Views/Cashier/CashierDetailView.swift @@ -9,8 +9,6 @@ import SwiftUI struct CashierDetailView: View { var tournaments : [Tournament] - @State private var calculateEarnings: Bool = false - @State private var calculateCompletion: Bool = false @State private var earnings: Double? = nil @State private var paidCompletion: Double? = nil @@ -46,26 +44,19 @@ struct CashierDetailView: View { } ForEach(tournaments) { tournament in - Section { - LabeledContent { - Text(tournament.earnings().formatted(.currency(code: "EUR").precision(.fractionLength(0)))) - } label: { - Text("Encaissement") - Text(tournament.paidCompletion().formatted(.percent.precision(.fractionLength(0)))).foregroundStyle(.secondary) - } - _tournamentCashierDetailView(tournament) - } header: { - if tournaments.count > 1 { - Text(tournament.tournamentTitle()) - } - } + CashierSectionView(tournament: tournament, showTournamentTitle: tournaments.count > 1) } } .headerProminence(.increased) .onAppear { Task { - _getEarnings() - _getPaidCompletion() + if earnings == nil { + _getEarnings() + } + + if paidCompletion == nil { + _getPaidCompletion() + } } } } @@ -80,42 +71,105 @@ struct CashierDetailView: View { paidCompletion = Double(selectedPlayers.filter { $0.hasPaid() }.count) / Double(selectedPlayers.count) } - private func _tournamentCashierDetailView(_ tournament: Tournament) -> some View { + private func _tournamentsCashierDetailView(_ tournaments: [Tournament]) -> some View { DisclosureGroup { - let selectedPlayers = tournament.selectedPlayers() ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in - let count = selectedPlayers.filter({ $0.paymentType == type }).count - if count > 0 { - LabeledContent { - if let entryFee = tournament.entryFee { - let sum = Double(count) * entryFee - Text(sum.formatted(.currency(code: "EUR"))) - } - } label: { - Text(type.localizedLabel()) - Text(count.formatted()) - } - } + PaymentTypeCashierRowView(tournaments: tournaments, type: type) } } label: { Text("Voir le détail") } } - private func _tournamentsCashierDetailView(_ tournaments: [Tournament]) -> some View { - DisclosureGroup { - ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in - let value = tournaments.compactMap({ $0.paidSelectedPlayers(type: type) }).reduce(0,+) - if value > 0 { - LabeledContent { - Text(value.formatted(.currency(code: "EUR"))) - } label: { - Text(type.localizedLabel()) + struct CashierSectionView: View { + let tournament: Tournament + let showTournamentTitle: Bool + @State private var earnings: Double? = nil + @State private var paidCompletion: Double? = nil + + var body: some View { + Section { + LabeledContent { + if let earnings { + Text(earnings.formatted(.currency(code: "EUR").precision(.fractionLength(0)))) + } else { + ProgressView() } + } label: { + Text("Encaissement") + if let paidCompletion { + Text(paidCompletion.formatted(.percent.precision(.fractionLength(0)))).foregroundStyle(.secondary) + } + } + CashierDetailDisclosureView(tournament: tournament) + } header: { + if showTournamentTitle { + Text(tournament.tournamentTitle()) } } - } label: { - Text("Voir le détail") + .onAppear { + Task { + if earnings == nil { + earnings = tournament.earnings() + } + + if paidCompletion == nil { + paidCompletion = tournament.paidCompletion() + } + } + } + } + } + + struct PaymentTypeCashierRowView: View { + let tournaments: [Tournament] + let type: PlayerRegistration.PlayerPaymentType + @State private var value: Double? + + + var body: some View { + LabeledContent { + if let value { + Text(value.formatted(.currency(code: "EUR"))) + } else { + ProgressView() + } + } label: { + Text(type.localizedLabel()) + } + .onAppear { + Task { + if value == nil { + value = tournaments.compactMap({ $0.paidSelectedPlayers(type: type) }).reduce(0,+) + } + } + } + } + } + + struct CashierDetailDisclosureView: View { + let tournament: Tournament + + var body: some View { + DisclosureGroup { + let selectedPlayers = tournament.selectedPlayers() + ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in + let count = selectedPlayers.filter({ $0.paymentType == type }).count + if count > 0 { + LabeledContent { + if let entryFee = tournament.entryFee { + let sum = Double(count) * entryFee + Text(sum.formatted(.currency(code: "EUR"))) + } + } label: { + Text(type.localizedLabel()) + Text(count.formatted()) + } + } + } + } label: { + Text("Voir le détail") + } } } }