parent
8fc6f09908
commit
470aa619d6
@ -0,0 +1,194 @@ |
|||||||
|
// |
||||||
|
// TournamentSubscriptionView.swift |
||||||
|
// PadelClub |
||||||
|
// |
||||||
|
// Created by Razmig Sarkissian on 09/09/2024. |
||||||
|
// |
||||||
|
|
||||||
|
import SwiftUI |
||||||
|
|
||||||
|
struct TournamentSubscriptionView: View { |
||||||
|
@EnvironmentObject var networkMonitor: NetworkMonitor |
||||||
|
|
||||||
|
let federalTournament: FederalTournament |
||||||
|
let build: any TournamentBuildHolder |
||||||
|
|
||||||
|
@State private var selectedPlayers: [ImportedPlayer] |
||||||
|
@State private var contactType: ContactType? = nil |
||||||
|
@State private var sentError: ContactManagerError? = nil |
||||||
|
|
||||||
|
init(federalTournament: FederalTournament, build: any TournamentBuildHolder, user: User) { |
||||||
|
self.federalTournament = federalTournament |
||||||
|
self.build = build |
||||||
|
_selectedPlayers = .init(wrappedValue: [user.currentPlayerData()].compactMap({ $0 })) |
||||||
|
} |
||||||
|
|
||||||
|
var body: some View { |
||||||
|
List { |
||||||
|
Section { |
||||||
|
LabeledContent("Tournoi") { |
||||||
|
Text(federalTournament.libelle ?? "Tournoi") |
||||||
|
} |
||||||
|
LabeledContent("Club") { |
||||||
|
Text(federalTournament.clubLabel()) |
||||||
|
} |
||||||
|
LabeledContent("Épreuve") { |
||||||
|
Text(build.buildHolderTitle()) |
||||||
|
} |
||||||
|
} header: { |
||||||
|
Text("Informations") |
||||||
|
} |
||||||
|
|
||||||
|
Section { |
||||||
|
ForEach(selectedPlayers) { teamPlayer in |
||||||
|
NavigationLink { |
||||||
|
SelectablePlayerListView(allowSelection: 1, playerSelectionAction: { players in |
||||||
|
if let player = players.first { |
||||||
|
selectedPlayers.remove(elements: [teamPlayer]) |
||||||
|
selectedPlayers.append(player) |
||||||
|
} |
||||||
|
}) |
||||||
|
} label: { |
||||||
|
ImportedPlayerView(player: teamPlayer) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if selectedPlayers.count < 2 { |
||||||
|
NavigationLink { |
||||||
|
SelectablePlayerListView(allowSelection: 1, playerSelectionAction: { players in |
||||||
|
if let player = players.first { |
||||||
|
selectedPlayers.append(player) |
||||||
|
} |
||||||
|
}) |
||||||
|
} label: { |
||||||
|
Text("Choisir un partenaire") |
||||||
|
} |
||||||
|
} |
||||||
|
} header: { |
||||||
|
HStack { |
||||||
|
Text("Poids") |
||||||
|
Spacer() |
||||||
|
Text(selectedPlayers.map { $0.rank }.reduce(0, +).formatted()) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Section { |
||||||
|
LabeledContent("JAP") { |
||||||
|
Text(federalTournament.umpireLabel()) |
||||||
|
} |
||||||
|
LabeledContent("Mail") { |
||||||
|
Text(federalTournament.mailLabel()) |
||||||
|
} |
||||||
|
LabeledContent("Téléphone") { |
||||||
|
Text(federalTournament.phoneLabel()) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
let teams = selectedPlayers.map { $0.pasteData() }.joined(separator: "\n") |
||||||
|
let body = [[build.buildHolderTitle(), federalTournament.computedStartDate].compacted().joined(separator: " "), teams].compactMap { $0 }.joined(separator: "\n") + "\n" |
||||||
|
let subject = [build.buildHolderTitle(), federalTournament.nomClub].compacted().joined(separator: " ") |
||||||
|
if let courrielEngagement = federalTournament.courrielEngagement { |
||||||
|
Section { |
||||||
|
RowButtonView("Contacter par email") { |
||||||
|
contactType = .mail(date: nil, recipients: [courrielEngagement], bccRecipients: nil, body: body, subject: subject, tournamentBuild: build as? TournamentBuild) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if let installation = federalTournament.installation, let telephone = installation.telephone { |
||||||
|
if telephone.isMobileNumber() { |
||||||
|
let body = [[build.buildHolderTitle(), federalTournament.nomClub].compacted().joined(separator: " "), federalTournament.computedStartDate, teams].compacted().joined(separator: "\n") + "\n" |
||||||
|
Section { |
||||||
|
RowButtonView("Contacter par message") { |
||||||
|
contactType = .message(date: nil, recipients: [telephone], body: body, tournamentBuild: build as? TournamentBuild) |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
let number = telephone.replacingOccurrences(of: " ", with: "") |
||||||
|
if let url = URL(string: "tel:\(number)") { |
||||||
|
Link(destination: url) { |
||||||
|
Label("Appeler", systemImage: "phone") |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
.alert("Un problème est survenu", isPresented: messageSentFailed) { |
||||||
|
Button("OK") { |
||||||
|
} |
||||||
|
} message: { |
||||||
|
Text(_networkErrorMessage) |
||||||
|
} |
||||||
|
.sheet(item: $contactType) { contactType in |
||||||
|
Group { |
||||||
|
switch contactType { |
||||||
|
case .message(_, let recipients, let body, _): |
||||||
|
MessageComposeView(recipients: recipients, body: body) { result in |
||||||
|
switch result { |
||||||
|
case .cancelled: |
||||||
|
break |
||||||
|
case .failed: |
||||||
|
self.sentError = .messageFailed |
||||||
|
case .sent: |
||||||
|
if networkMonitor.connected == false { |
||||||
|
self.sentError = .messageNotSent |
||||||
|
} |
||||||
|
@unknown default: |
||||||
|
break |
||||||
|
} |
||||||
|
} |
||||||
|
case .mail(_, let recipients, let bccRecipients, let body, let subject, _): |
||||||
|
MailComposeView(recipients: recipients, bccRecipients: bccRecipients, body: body, subject: subject) { result in |
||||||
|
switch result { |
||||||
|
case .cancelled, .saved: |
||||||
|
self.contactType = nil |
||||||
|
case .failed: |
||||||
|
self.contactType = nil |
||||||
|
self.sentError = .mailFailed |
||||||
|
case .sent: |
||||||
|
if networkMonitor.connected == false { |
||||||
|
self.contactType = nil |
||||||
|
self.sentError = .mailNotSent |
||||||
|
} |
||||||
|
@unknown default: |
||||||
|
break |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
.tint(.master) |
||||||
|
} |
||||||
|
.navigationBarTitleDisplayMode(.inline) |
||||||
|
.toolbarBackground(.visible, for: .navigationBar) |
||||||
|
.navigationTitle("Détail du tournoi") |
||||||
|
} |
||||||
|
|
||||||
|
var messageSentFailed: Binding<Bool> { |
||||||
|
Binding { |
||||||
|
sentError != nil |
||||||
|
} set: { newValue in |
||||||
|
if newValue == false { |
||||||
|
sentError = nil |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private var _networkErrorMessage: String { |
||||||
|
var errors: [String] = [] |
||||||
|
|
||||||
|
if networkMonitor.connected == false { |
||||||
|
errors.append("L'appareil n'est pas connecté à internet.") |
||||||
|
} |
||||||
|
if sentError == .mailNotSent { |
||||||
|
errors.append("Le mail est dans la boîte d'envoi de l'app Mail. Vérifiez son état dans l'app Mail avant d'essayer de le renvoyer.") |
||||||
|
} |
||||||
|
if (sentError == .messageFailed || sentError == .messageNotSent) { |
||||||
|
errors.append("Le SMS n'a pas été envoyé") |
||||||
|
} |
||||||
|
if sentError == .mailFailed { |
||||||
|
errors.append("Le mail n'a pas été envoyé") |
||||||
|
} |
||||||
|
return errors.joined(separator: "\n") |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue