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.
 
 
PadelClub/PadelClub/Views/Tournament/Screen/Components/UpdateSourceRankDateView.swift

101 lines
4.2 KiB

//
// UpdateSourceRankDateView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 25/03/2024.
//
import SwiftUI
import LeStorage
struct UpdateSourceRankDateView: View {
@EnvironmentObject var dataStore: DataStore
@Binding var currentRankSourceDate: Date?
@Binding var confirmUpdateRank: Bool
@State private var forceRefreshLockWeight: Bool = false
@State private var updatingRank = false
var tournament: Tournament
var body: some View {
NavigationStack {
List {
let suffix = (tournament.inscriptionClosed() ? "Les incriptions sont closes. Les équipes ne pourront pas être déplacé entre les poules et le tableau. Seule leur position au sein des poules et du tableau, respectivement, seront modifiée." : "Les inscriptions sont toujours ouvertes. Les équipes pourront être déplacé entre les poules et le tableau.")
Section {
Text("Vous êtes sur le point de mettre à jour les rangs des équipes." + "\n" + suffix)
}
if tournament.inscriptionClosed() {
Section {
Toggle(isOn: $forceRefreshLockWeight) {
Text("Ne pas en tenir compte")
}
}
}
RowButtonView("Valider") {
updatingRank = true
Task {
do {
try await tournament.updateRank(to: currentRankSourceDate)
try await MainActor.run {
tournament.unsortedPlayers().forEach { player in
player.setWeight(in: tournament)
}
try dataStore.playerRegistrations.addOrUpdate(contentOfs: tournament.unsortedPlayers())
tournament.unsortedTeams().forEach { team in
team.setWeight(from: team.players(), inTournamentCategory: tournament.tournamentCategory)
if forceRefreshLockWeight {
team.lockWeight = team.weight
}
}
try dataStore.teamRegistrations.addOrUpdate(contentOfs: tournament.unsortedTeams())
try dataStore.tournaments.addOrUpdate(instance: tournament)
updatingRank = false
confirmUpdateRank = false
}
} catch {
Logger.error(error)
}
}
}.disabled(updatingRank)
if updatingRank {
Section {
let message = currentRankSourceDate == nil ? "Mise à jour des classements" : "Mise à jour des classements provenant de \(currentRankSourceDate!.monthYearFormatted) pour tous les joueurs."
ContentUnavailableView("Mise à jour", systemImage: "exclamationmark.triangle", description: Text(message))
}
Section {
HStack {
Spacer()
ProgressView()
Spacer()
}
}
}
}
.navigationTitle("Attention")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button(role: .cancel) {
confirmUpdateRank = false
} label: {
Text("Annuler")
}
}
}
}
.interactiveDismissDisabled(updatingRank)
}
}
#Preview {
UpdateSourceRankDateView(currentRankSourceDate: .constant(Date()), confirmUpdateRank: .constant(true), tournament: Tournament.mock())
}