|
|
|
|
@ -41,6 +41,7 @@ struct AddTeamView: View { |
|
|
|
|
@State private var homonyms: [PlayerRegistration] = [] |
|
|
|
|
@State private var confirmHomonym: Bool = false |
|
|
|
|
@State private var editableTextField: String = "" |
|
|
|
|
@State private var textHeight: CGFloat = 100 // Default height |
|
|
|
|
|
|
|
|
|
var tournamentStore: TournamentStore { |
|
|
|
|
return self.tournament.tournamentStore |
|
|
|
|
@ -67,6 +68,7 @@ struct AddTeamView: View { |
|
|
|
|
_fetchPlayers = FetchRequest<ImportedPlayer>(sortDescriptors: [NSSortDescriptor(keyPath: \ImportedPlayer.rank, ascending: true)], predicate: SearchViewModel.pastePredicate(pasteField: pasteString, mostRecentDate: tournament.rankSourceDate, filterOption: tournament.tournamentCategory.playerFilterOption)) |
|
|
|
|
_autoSelect = .init(wrappedValue: true) |
|
|
|
|
_editableTextField = .init(wrappedValue: pasteString) |
|
|
|
|
_textHeight = .init(wrappedValue: Self._calculateHeight(text: pasteString)) |
|
|
|
|
cancelShouldDismiss = true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -121,7 +123,7 @@ struct AddTeamView: View { |
|
|
|
|
selectionSearchField = nil |
|
|
|
|
}) { |
|
|
|
|
NavigationStack { |
|
|
|
|
SelectablePlayerListView(allowSelection: -1, filterOption: _filterOption(), showFemaleInMaleAssimilation: tournament.tournamentCategory.showFemaleInMaleAssimilation) { players in |
|
|
|
|
SelectablePlayerListView(allowSelection: -1, searchField: searchField, filterOption: _filterOption(), showFemaleInMaleAssimilation: tournament.tournamentCategory.showFemaleInMaleAssimilation) { players in |
|
|
|
|
selectionSearchField = nil |
|
|
|
|
players.forEach { player in |
|
|
|
|
let newPlayer = PlayerRegistration(importedPlayer: player) |
|
|
|
|
@ -147,7 +149,7 @@ struct AddTeamView: View { |
|
|
|
|
} |
|
|
|
|
.toolbar { |
|
|
|
|
ToolbarItem(placement: .cancellationAction) { |
|
|
|
|
Button("Annuler", role: .cancel) { |
|
|
|
|
Button("Terminer", role: .cancel) { |
|
|
|
|
dismiss() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -166,7 +168,7 @@ struct AddTeamView: View { |
|
|
|
|
} |
|
|
|
|
.navigationBarBackButtonHidden(true) |
|
|
|
|
.toolbarBackground(.visible, for: .navigationBar) |
|
|
|
|
.toolbarBackground(.visible, for: .bottomBar) |
|
|
|
|
.toolbarBackground(.automatic, for: .bottomBar) |
|
|
|
|
.navigationBarTitleDisplayMode(.inline) |
|
|
|
|
.navigationTitle(editedTeam == nil ? "Ajouter une équipe" : "Modifier l'équipe") |
|
|
|
|
.environment(\.editMode, Binding.constant(EditMode.active)) |
|
|
|
|
@ -291,14 +293,16 @@ struct AddTeamView: View { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
createdPlayers.removeAll() |
|
|
|
|
createdPlayerIds.removeAll() |
|
|
|
|
pasteString = nil |
|
|
|
|
editableTextField = "" |
|
|
|
|
pasteString = nil |
|
|
|
|
editableTextField = "" |
|
|
|
|
|
|
|
|
|
if team.players().count > 1 { |
|
|
|
|
dismiss() |
|
|
|
|
} |
|
|
|
|
if team.players().count > 1 { |
|
|
|
|
createdPlayers.removeAll() |
|
|
|
|
createdPlayerIds.removeAll() |
|
|
|
|
dismiss() |
|
|
|
|
} else { |
|
|
|
|
editedTeam = team |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private func _updateTeam(checkDuplicates: Bool) { |
|
|
|
|
@ -320,23 +324,37 @@ struct AddTeamView: View { |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
createdPlayers.removeAll() |
|
|
|
|
createdPlayerIds.removeAll() |
|
|
|
|
|
|
|
|
|
pasteString = nil |
|
|
|
|
editableTextField = "" |
|
|
|
|
|
|
|
|
|
self.editedTeam = nil |
|
|
|
|
if editedTeam.players().count > 1 { |
|
|
|
|
dismiss() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Calculating the height based on the content of the TextEditor |
|
|
|
|
static private func _calculateHeight(text: String) -> CGFloat { |
|
|
|
|
let size = CGSize(width: UIScreen.main.bounds.width - 32, height: .infinity) |
|
|
|
|
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 17)] |
|
|
|
|
let boundingRect = text.boundingRect( |
|
|
|
|
with: size, |
|
|
|
|
options: .usesLineFragmentOrigin, |
|
|
|
|
attributes: attributes, |
|
|
|
|
context: nil |
|
|
|
|
) |
|
|
|
|
return max(boundingRect.height + 20, 40) // Add some padding and set a minimum height |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ViewBuilder |
|
|
|
|
private func _buildingTeamView() -> some View { |
|
|
|
|
if let pasteString { |
|
|
|
|
Section { |
|
|
|
|
TextEditor(text: $editableTextField) |
|
|
|
|
.frame(minHeight: 120, maxHeight: .infinity) |
|
|
|
|
.frame(height: textHeight) |
|
|
|
|
.onChange(of: editableTextField) { |
|
|
|
|
textHeight = Self._calculateHeight(text: pasteString) |
|
|
|
|
} |
|
|
|
|
.focused($focusedField, equals: .pasteField) |
|
|
|
|
.toolbar { |
|
|
|
|
ToolbarItemGroup(placement: .keyboard) { |
|
|
|
|
@ -360,12 +378,10 @@ struct AddTeamView: View { |
|
|
|
|
self.focusedField = .pasteField |
|
|
|
|
} |
|
|
|
|
Spacer() |
|
|
|
|
FooterButtonView("effacer", role: .destructive) { |
|
|
|
|
FooterButtonView("effacer le texte") { |
|
|
|
|
self.focusedField = nil |
|
|
|
|
self.editableTextField = "" |
|
|
|
|
self.pasteString = nil |
|
|
|
|
self.createdPlayers.removeAll() |
|
|
|
|
self.createdPlayerIds.removeAll() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -379,11 +395,12 @@ struct AddTeamView: View { |
|
|
|
|
Text("Déjà inscrit !").foregroundStyle(.logoRed).bold() |
|
|
|
|
} |
|
|
|
|
PlayerView(player: p).tag(p.id) |
|
|
|
|
.environment(tournament) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if let p = fetchPlayers.first(where: { $0.license == id }) { |
|
|
|
|
VStack(alignment: .leading, spacing: 0) { |
|
|
|
|
if unsortedPlayers.first(where: { $0.licenceId == p.license }) != nil { |
|
|
|
|
if pasteString != nil, unsortedPlayers.first(where: { $0.licenceId == p.license }) != nil { |
|
|
|
|
Text("Déjà inscrit !").foregroundStyle(.logoRed).bold() |
|
|
|
|
} |
|
|
|
|
ImportedPlayerView(player: p).tag(p.license!) |
|
|
|
|
@ -437,7 +454,9 @@ struct AddTeamView: View { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let pasteString { |
|
|
|
|
if fetchPlayers.isEmpty { |
|
|
|
|
let sortedPlayers = fetchPlayers.filter({ $0.contains(searchField) || searchField.isEmpty }) |
|
|
|
|
|
|
|
|
|
if sortedPlayers.isEmpty { |
|
|
|
|
ContentUnavailableView { |
|
|
|
|
Label("Aucun résultat", systemImage: "person.2.slash") |
|
|
|
|
} description: { |
|
|
|
|
@ -447,6 +466,10 @@ struct AddTeamView: View { |
|
|
|
|
presentPlayerCreation = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
RowButtonView("Chercher dans la base") { |
|
|
|
|
presentPlayerSearch = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
RowButtonView("Effacer cette recherche") { |
|
|
|
|
self.pasteString = nil |
|
|
|
|
self.editableTextField = "" |
|
|
|
|
@ -489,6 +512,7 @@ struct AddTeamView: View { |
|
|
|
|
fetchPlayers.nsSortDescriptors = [NSSortDescriptor(keyPath: \ImportedPlayer.rank, ascending: true)] |
|
|
|
|
pasteString = first |
|
|
|
|
editableTextField = first |
|
|
|
|
textHeight = Self._calculateHeight(text: first) |
|
|
|
|
autoSelect = true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -503,6 +527,7 @@ struct AddTeamView: View { |
|
|
|
|
Section { |
|
|
|
|
ForEach(sortedPlayers) { player in |
|
|
|
|
ImportedPlayerView(player: player).tag(player.license!) |
|
|
|
|
//Text(player.getLastName() + " " + player.getFirstName()).tag(player.license!) |
|
|
|
|
} |
|
|
|
|
} header: { |
|
|
|
|
Text(sortedPlayers.count.formatted() + " résultat" + sortedPlayers.count.pluralSuffix) |
|
|
|
|
|