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.
166 lines
6.4 KiB
166 lines
6.4 KiB
//
|
|
// EditScoreView.swift
|
|
// Padel Tournament
|
|
//
|
|
// Created by Razmig Sarkissian on 27/02/2023.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LeStorage
|
|
|
|
struct EditScoreView: View {
|
|
|
|
@EnvironmentObject var dataStore: DataStore
|
|
|
|
@StateObject var matchDescriptor: MatchDescriptor
|
|
@State private var presentMatchFormatSelection: Bool = false
|
|
@Binding var confirmScoreEdition: Bool
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
init(match: Match, confirmScoreEdition: Binding<Bool>) {
|
|
let matchDescriptor = MatchDescriptor(match: match)
|
|
_matchDescriptor = .init(wrappedValue: matchDescriptor)
|
|
_confirmScoreEdition = confirmScoreEdition
|
|
}
|
|
|
|
func walkout(_ team: TeamPosition) {
|
|
self.matchDescriptor.match?.setWalkOut(team)
|
|
save()
|
|
dismiss()
|
|
}
|
|
|
|
func pointRange(winner: Bool) -> Int? {
|
|
guard let match = matchDescriptor.match else { return nil }
|
|
guard let tournament = match.currentTournament() else {
|
|
return nil
|
|
}
|
|
|
|
let teamsCount = tournament.teamCount
|
|
guard let round = match.roundObject else { return nil }
|
|
|
|
guard let seedInterval = round.seedInterval(), match.index == 0 else {
|
|
return nil
|
|
}
|
|
|
|
return winner ? tournament.tournamentLevel.points(for: seedInterval.first - 1, count: teamsCount) : tournament.tournamentLevel.points(for: seedInterval.last - 1, count: teamsCount)
|
|
}
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section {
|
|
VStack(alignment: .leading) {
|
|
Text(matchDescriptor.teamLabelOne)
|
|
if matchDescriptor.hasEnded, let pointRange = pointRange(winner: matchDescriptor.winner == .one) {
|
|
Text(pointRange.formatted(.number.sign(strategy: .always())) + " pts")
|
|
.bold()
|
|
}
|
|
}
|
|
.listRowView(isActive: matchDescriptor.teamOneSetupIsActive, color: matchDescriptor.colorTeamOne, hideColorVariation: false)
|
|
HStack {
|
|
Spacer()
|
|
VStack(alignment: .trailing) {
|
|
Text(matchDescriptor.teamLabelTwo).multilineTextAlignment(.trailing)
|
|
if matchDescriptor.hasEnded, let pointRange = pointRange(winner: matchDescriptor.winner == .two) {
|
|
Text(pointRange.formatted(.number.sign(strategy: .always())) + " pts")
|
|
.bold()
|
|
}
|
|
}
|
|
}
|
|
.listRowView(isActive: matchDescriptor.teamTwoSetupIsActive, color: matchDescriptor.colorTeamTwo, hideColorVariation: false, alignment: .trailing)
|
|
} header: {
|
|
if let roundTitle = matchDescriptor.match?.roundAndMatchTitle() {
|
|
Text(roundTitle)
|
|
}
|
|
} footer: {
|
|
HStack {
|
|
Menu {
|
|
Button {
|
|
walkout(.one)
|
|
} label: {
|
|
Text(matchDescriptor.teamLabelOne)
|
|
}
|
|
Button {
|
|
walkout(.two)
|
|
} label: {
|
|
Text(matchDescriptor.teamLabelTwo)
|
|
}
|
|
} label: {
|
|
Text("Forfait d'une équipe ?")
|
|
.underline()
|
|
}
|
|
Spacer()
|
|
|
|
FooterButtonView("Format : \(matchDescriptor.matchFormat.shortFormat)") {
|
|
presentMatchFormatSelection = true
|
|
}
|
|
}
|
|
}
|
|
ForEach($matchDescriptor.setDescriptors) { $setDescriptor in
|
|
SetInputView(setDescriptor: $setDescriptor)
|
|
.onChange(of: setDescriptor.hasEnded) {
|
|
if setDescriptor.hasEnded {
|
|
if matchDescriptor.hasEnded == false {
|
|
matchDescriptor.addNewSet()
|
|
}
|
|
} else {
|
|
let index = matchDescriptor.setDescriptors.firstIndex(where: { $0 == setDescriptor }) ?? 0
|
|
matchDescriptor.setDescriptors = Array(matchDescriptor.setDescriptors[0...index])
|
|
}
|
|
}
|
|
.tint(matchDescriptor.getColor())
|
|
}
|
|
|
|
if matchDescriptor.hasEnded {
|
|
Section {
|
|
HStack {
|
|
Spacer()
|
|
VStack {
|
|
Text(matchDescriptor.winnerLabel)
|
|
}
|
|
.multilineTextAlignment(.center)
|
|
Spacer()
|
|
}
|
|
RowButtonView("Victoire") {
|
|
matchDescriptor.match?.setScore(fromMatchDescriptor: matchDescriptor)
|
|
save()
|
|
dismiss()
|
|
}
|
|
} footer: {
|
|
Text("Termine la rencontre sur ce score")
|
|
}
|
|
}
|
|
|
|
if matchDescriptor.match?.hasEnded() == false {
|
|
Section {
|
|
RowButtonView("Mise à jour") {
|
|
matchDescriptor.match?.updateScore(fromMatchDescriptor: matchDescriptor)
|
|
save()
|
|
dismiss()
|
|
}
|
|
} footer: {
|
|
Text("Met à jour le score, ne termine pas la rencontre")
|
|
}
|
|
}
|
|
}
|
|
.sheet(isPresented: $presentMatchFormatSelection) {
|
|
MatchFormatSelectionView(matchFormat: $matchDescriptor.matchFormat, additionalEstimationDuration: matchDescriptor.match?.currentTournament()?.additionalEstimationDuration)
|
|
.tint(.master)
|
|
}
|
|
.onChange(of: matchDescriptor.matchFormat) {
|
|
presentMatchFormatSelection = false
|
|
matchDescriptor.setDescriptors.removeAll()
|
|
matchDescriptor.addNewSet()
|
|
}
|
|
}
|
|
|
|
func save() {
|
|
self.confirmScoreEdition = true
|
|
if let match = matchDescriptor.match {
|
|
do {
|
|
try match.tournamentStore.matches.addOrUpdate(instance: match)
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|