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.
223 lines
8.0 KiB
223 lines
8.0 KiB
//
|
|
// PlayerDetailView.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Razmig Sarkissian on 17/04/2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LeStorage
|
|
|
|
struct PlayerDetailView: View {
|
|
|
|
@Environment(Tournament.self) var tournament: Tournament
|
|
@EnvironmentObject var dataStore: DataStore
|
|
|
|
@Bindable var player: PlayerRegistration
|
|
@State private var licenceId: String
|
|
@State private var phoneNumber: String
|
|
@State private var email: String
|
|
@FocusState var focusedField: PlayerRegistration.CodingKeys?
|
|
|
|
var tournamentStore: TournamentStore {
|
|
return self.tournament.tournamentStore
|
|
}
|
|
|
|
init(player: PlayerRegistration) {
|
|
self.player = player
|
|
_licenceId = .init(wrappedValue: player.licenceId ?? "")
|
|
_email = .init(wrappedValue: player.email ?? "")
|
|
_phoneNumber = .init(wrappedValue: player.phoneNumber ?? "")
|
|
}
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section {
|
|
Toggle("Joueur sur place", isOn: $player.hasArrived)
|
|
|
|
LabeledContent {
|
|
TextField("Nom", text: $player.lastName)
|
|
.keyboardType(.alphabet)
|
|
.multilineTextAlignment(.trailing)
|
|
.frame(maxWidth: .infinity)
|
|
.onSubmit(of: .text) {
|
|
player.lastName = player.lastName.trimmedMultiline
|
|
_save()
|
|
}
|
|
} label: {
|
|
Text("Nom")
|
|
}
|
|
|
|
LabeledContent {
|
|
TextField("Prénom", text: $player.firstName)
|
|
.keyboardType(.alphabet)
|
|
.multilineTextAlignment(.trailing)
|
|
.frame(maxWidth: .infinity)
|
|
.onSubmit(of: .text) {
|
|
player.firstName = player.firstName.trimmedMultiline
|
|
_save()
|
|
}
|
|
} label: {
|
|
Text("Prénom")
|
|
}
|
|
|
|
PlayerSexPickerView(player: player)
|
|
}
|
|
|
|
Section {
|
|
LabeledContent {
|
|
TextField("Rang", value: $player.rank, format: .number)
|
|
.keyboardType(.decimalPad)
|
|
.multilineTextAlignment(.trailing)
|
|
.frame(maxWidth: .infinity)
|
|
.focused($focusedField, equals: ._rank)
|
|
} label: {
|
|
Text("Rang")
|
|
}
|
|
} header: {
|
|
Text("Classement actuel")
|
|
} footer: {
|
|
if player.rank == nil {
|
|
Text("Classement calculé : " + player.computedRank.formatted())
|
|
}
|
|
}
|
|
|
|
if player.isMalePlayer() == false && tournament.tournamentCategory == .men, let rank = player.rank {
|
|
Section {
|
|
let value = PlayerRegistration.addon(for: rank, manMax: tournament.maleUnrankedValue ?? 0, womanMax: tournament.femaleUnrankedValue ?? 0)
|
|
LabeledContent {
|
|
Text(value.formatted())
|
|
} label: {
|
|
Text("Valeur à rajouter")
|
|
}
|
|
LabeledContent {
|
|
TextField("Rang", value: $player.computedRank, format: .number)
|
|
.keyboardType(.decimalPad)
|
|
.multilineTextAlignment(.trailing)
|
|
.frame(maxWidth: .infinity)
|
|
.focused($focusedField, equals: ._computedRank)
|
|
} label: {
|
|
Text("Poids re-calculé")
|
|
}
|
|
} header: {
|
|
Text("Ré-assimilation")
|
|
} footer: {
|
|
Text("Calculé en fonction du sexe")
|
|
}
|
|
}
|
|
|
|
Section {
|
|
LabeledContent {
|
|
TextField("Licence", text: $licenceId)
|
|
.keyboardType(.alphabet)
|
|
.multilineTextAlignment(.trailing)
|
|
.autocorrectionDisabled()
|
|
.frame(maxWidth: .infinity)
|
|
.onSubmit(of: .text) {
|
|
player.licenceId = licenceId
|
|
_save()
|
|
}
|
|
} label: {
|
|
Text("Licence")
|
|
}
|
|
} footer: {
|
|
CopyPasteButtonView(pasteValue: player.licenceId?.strippedLicense)
|
|
}
|
|
|
|
Section {
|
|
LabeledContent {
|
|
TextField("Téléphone", text: $phoneNumber)
|
|
.keyboardType(.namePhonePad)
|
|
.multilineTextAlignment(.trailing)
|
|
.autocorrectionDisabled()
|
|
.frame(maxWidth: .infinity)
|
|
.onSubmit(of: .text) {
|
|
player.phoneNumber = phoneNumber
|
|
_save()
|
|
}
|
|
} label: {
|
|
Text("Téléphone")
|
|
}
|
|
} footer: {
|
|
CopyPasteButtonView(pasteValue: player.phoneNumber)
|
|
}
|
|
|
|
Section {
|
|
LabeledContent {
|
|
TextField("Email", text: $email)
|
|
.keyboardType(.emailAddress)
|
|
.multilineTextAlignment(.trailing)
|
|
.autocorrectionDisabled()
|
|
.frame(maxWidth: .infinity)
|
|
.onSubmit(of: .text) {
|
|
player.email = email
|
|
_save()
|
|
}
|
|
} label: {
|
|
Text("Email")
|
|
}
|
|
} footer: {
|
|
CopyPasteButtonView(pasteValue: player.email)
|
|
}
|
|
|
|
}
|
|
.toolbar {
|
|
ToolbarItem(placement: .topBarTrailing) {
|
|
ShareLink(item: player.pasteData()) {
|
|
Label("Partager", systemImage: "square.and.arrow.up")
|
|
}
|
|
}
|
|
}
|
|
.scrollDismissesKeyboard(.immediately)
|
|
.onChange(of: player.hasArrived) {
|
|
_save()
|
|
}
|
|
.onChange(of: player.sex) {
|
|
_save()
|
|
}
|
|
.onChange(of: player.computedRank) {
|
|
player.team()?.updateWeight(inTournamentCategory: tournament.tournamentCategory)
|
|
_save()
|
|
}
|
|
.headerProminence(.increased)
|
|
.navigationTitle("Édition")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbarBackground(.visible, for: .navigationBar)
|
|
.toolbar {
|
|
if focusedField == ._rank || focusedField == ._computedRank {
|
|
ToolbarItem(placement: .keyboard) {
|
|
Button("Valider") {
|
|
if focusedField == ._rank {
|
|
player.setComputedRank(in: tournament)
|
|
player.team()?.updateWeight(inTournamentCategory: tournament.tournamentCategory)
|
|
_save()
|
|
} else if focusedField == ._computedRank {
|
|
player.team()?.updateWeight(inTournamentCategory: tournament.tournamentCategory)
|
|
_save()
|
|
}
|
|
focusedField = nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func _save() {
|
|
do {
|
|
try self.tournamentStore.playerRegistrations.addOrUpdate(instance: player)
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
if let team = player.team() {
|
|
do {
|
|
try self.tournamentStore.teamRegistrations.addOrUpdate(instance: team)
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//#Preview {
|
|
// PlayerDetailView(player: PlayerRegistration.mock())
|
|
//}
|
|
|