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/Match/MatchSetupView.swift

72 lines
2.8 KiB

//
// MatchSetupView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 23/03/2024.
//
import SwiftUI
struct MatchSetupView: View {
@EnvironmentObject var dataStore: DataStore
var match: Match
@State private var seedGroup: SeedInterval?
@ViewBuilder
var body: some View {
_teamView(match.team(.one), teamPosition: 0)
_teamView(match.team(.two), teamPosition: 1)
}
@ViewBuilder
func _teamView(_ team: TeamRegistration?, teamPosition: Int) -> some View {
if let team {
TeamRowView(team: team, teamPosition: teamPosition)
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button(role: .cancel) {
team.bracketPosition = nil
match._toggleLoserMatchDisableState(false)
try? dataStore.teamRegistrations.addOrUpdate(instance: team)
} label: {
Label("retirer", systemImage: "xmark")
}
}
} else {
HStack {
TeamPickerView(teamPicked: { team in
print(team.pasteData())
team.setSeedPosition(inSpot: match, upperBranch: teamPosition, opposingSeeding: false)
try? dataStore.matches.addOrUpdate(instance: match)
try? dataStore.teamRegistrations.addOrUpdate(instance: team)
})
if let tournament = match.currentTournament() {
let availableSeedGroups = tournament.availableSeedGroups()
Menu {
ForEach(availableSeedGroups, id: \.self) { seedGroup in
Button {
if let randomTeam = tournament.randomSeed(fromSeedGroup: seedGroup) {
randomTeam.setSeedPosition(inSpot: match, upperBranch: teamPosition, opposingSeeding: false)
try? dataStore.matches.addOrUpdate(instance: match)
try? dataStore.teamRegistrations.addOrUpdate(instance: randomTeam)
}
} label: {
Label(seedGroup.localizedLabel(), systemImage: "dice")
}
}
} label: {
Text("Tirage").tag(nil as SeedInterval?)
}
.disabled(availableSeedGroups.isEmpty)
}
}
.fixedSize(horizontal: false, vertical: true)
.buttonBorderShape(.capsule)
.buttonStyle(.borderedProminent)
}
}
}
#Preview {
MatchSetupView(match: Match.mock())
.environmentObject(DataStore.shared)
}