From 8497ab6e78080f03ceff587e00bbc177771b6436 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 20 Jun 2024 15:12:47 +0200 Subject: [PATCH] Requires an account for summons --- PadelClub/Views/Calling/CallView.swift | 118 +++++++++++------ PadelClub/Views/Calling/SendToAllView.swift | 122 +++++++++++------- .../Components/EditablePlayerView.swift | 3 +- 3 files changed, 152 insertions(+), 91 deletions(-) diff --git a/PadelClub/Views/Calling/CallView.swift b/PadelClub/Views/Calling/CallView.swift index 606e61c..107221e 100644 --- a/PadelClub/Views/Calling/CallView.swift +++ b/PadelClub/Views/Calling/CallView.swift @@ -24,7 +24,7 @@ struct CallView: View { Text("Aucun horaire").font(.body) } Spacer() - Text(count.formatted() + "/" + total.formatted()) + Text("\(count.formatted())/\(total.formatted())") } .font(.largeTitle) HStack { @@ -61,6 +61,10 @@ struct CallView: View { @State private var sentError: ContactManagerError? = nil @State var showSubscriptionView: Bool = false + @State var showUserCreationView: Bool = false + + @State var summonParamByMessage: Bool = false + @State var summonParamReSummon: Bool = false var messageSentFailed: Binding { Binding { @@ -74,7 +78,7 @@ struct CallView: View { private func _called(_ success: Bool) { if success { - teams.forEach { team in + self.teams.forEach { team in team.callDate = callDate } do { @@ -90,25 +94,25 @@ struct CallView: View { } var reSummon: Bool { - teams.allSatisfy({ $0.called() }) + return self.teams.allSatisfy({ $0.called() }) } var body: some View { let callWord : String = (reSummon ? "Reconvoquer" : "Convoquer") HStack { - if teams.count == 1 { + if self.teams.count == 1 { if let previousCallDate = teams.first?.callDate, Calendar.current.compare(previousCallDate, to: callDate, toGranularity: .minute) != .orderedSame { - Text("Reconvoquer " + callDate.localizedDate() + " par") + Text("Reconvoquer \(self.callDate.localizedDate()) par") } else { - Text(callWord + " cette paire par") + Text("\(callWord) cette paire par") } } else { - Text(callWord + " ces \(teams.count) paires par") + Text("\(callWord) ces \(self.teams.count) paires par") } - _summonMenu(byMessage: true) + self._summonMenu(byMessage: true) Text("ou") - _summonMenu(byMessage: false) + self._summonMenu(byMessage: false) } .font(.subheadline) .buttonStyle(.borderless) @@ -116,29 +120,28 @@ struct CallView: View { Button("OK") { } } message: { - let message = [networkMonitor.connected == false ? "L'appareil n'est pas connecté à internet." as String? : nil, sentError == .mailNotSent ? "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." as String? : nil, (sentError == .messageFailed || sentError == .messageNotSent) ? "Le SMS n'a pas été envoyé" as String? : nil, sentError == .mailFailed ? "Le mail n'a pas été envoyé" as String? : nil].compacted().joined(separator: "\n") - Text(message) + Text(self._failureMessage()) } .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 - } else { - _called(true) - } - @unknown default: - break + 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 + } else { + self._called(true) } - } + @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 { @@ -152,7 +155,7 @@ struct CallView: View { self.contactType = nil self.sentError = .mailNotSent } else { - _called(true) + self._called(true) } @unknown default: break @@ -167,18 +170,35 @@ struct CallView: View { SubscriptionView(isPresented: self.$showSubscriptionView, showLackOfPlanMessage: true) } }) + .sheet(isPresented: self.$showUserCreationView, content: { + NavigationStack { + LoginView { _ in + self.showUserCreationView = false + self._summon(byMessage: self.summonParamByMessage, + reSummon: self.summonParamByMessage) + } + } + }) + } + + fileprivate func _failureMessage() -> String { + return [networkMonitor.connected == false ? "L'appareil n'est pas connecté à internet." as String? : nil, + sentError == .mailNotSent ? "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." as String? : nil, + (sentError == .messageFailed || sentError == .messageNotSent) ? "Le SMS n'a pas été envoyé" as String? : nil, + sentError == .mailFailed ? "Le mail n'a pas été envoyé" as String? : nil] + .compacted().joined(separator: "\n") } @ViewBuilder private func _summonMenu(byMessage: Bool) -> some View { - if reSummon { + if self.reSummon { Menu { Button("Convoquer") { - _summon(byMessage: byMessage, reSummon: false) + self._summon(byMessage: byMessage, reSummon: false) } Button("Re-convoquer") { - _summon(byMessage: byMessage, reSummon: true) + self._summon(byMessage: byMessage, reSummon: true) } } label: { @@ -187,24 +207,36 @@ struct CallView: View { } } else { Button(byMessage ? "sms" : "mail") { - _summon(byMessage: byMessage, reSummon: false) + self._summon(byMessage: byMessage, reSummon: false) } } } private func _summon(byMessage: Bool, reSummon: Bool) { - self._payTournamentAndExecute { - if byMessage { - self._contactByMessage(reSummon: reSummon) - } else { - self._contactByMail(reSummon: reSummon) + self.summonParamByMessage = byMessage + self.summonParamReSummon = reSummon + self._verifyUser { + self._payTournamentAndExecute { + if byMessage { + self._contactByMessage(reSummon: reSummon) + } else { + self._contactByMail(reSummon: reSummon) + } } } } + fileprivate func _verifyUser(_ handler: () -> ()) { + if Store.main.userId != nil { + handler() + } else { + self.showUserCreationView = true + } + } + fileprivate func _payTournamentAndExecute(_ handler: () -> ()) { do { - try tournament.payIfNecessary() + try self.tournament.payIfNecessary() handler() } catch { self.showSubscriptionView = true @@ -212,11 +244,19 @@ struct CallView: View { } fileprivate func _contactByMessage(reSummon: Bool) { - contactType = .message(date: callDate, recipients: teams.flatMap { $0.getPhoneNumbers() }, body: finalMessage(reSummon: reSummon), tournamentBuild: nil) + self.contactType = .message(date: callDate, + recipients: teams.flatMap { $0.getPhoneNumbers() }, + body: finalMessage(reSummon: reSummon), + tournamentBuild: nil) } fileprivate func _contactByMail(reSummon: Bool) { - contactType = .mail(date: callDate, recipients: tournament.umpireMail(), bccRecipients: teams.flatMap { $0.getMail() }, body: finalMessage(reSummon: reSummon), subject: tournament.tournamentTitle(), tournamentBuild: nil) + self.contactType = .mail(date: callDate, + recipients: tournament.umpireMail(), + bccRecipients: teams.flatMap { $0.getMail() }, + body: finalMessage(reSummon: reSummon), + subject: tournament.tournamentTitle(), + tournamentBuild: nil) } } diff --git a/PadelClub/Views/Calling/SendToAllView.swift b/PadelClub/Views/Calling/SendToAllView.swift index 4b60117..18ee7a6 100644 --- a/PadelClub/Views/Calling/SendToAllView.swift +++ b/PadelClub/Views/Calling/SendToAllView.swift @@ -19,9 +19,15 @@ struct SendToAllView: View { @State private var contactRecipients: Set = Set() @State private var sentError: ContactManagerError? = nil let addLink: Bool - @State var cannotPayForTournament: Bool = false +// @State var cannotPayForTournament: Bool = false @State private var pageLink: PageLink = .teams + @State var showSubscriptionView: Bool = false + @State var showUserCreationView: Bool = false + + @State var summonParamByMessage: Bool = false + @State var summonParamReSummon: Bool = false + var messageSentFailed: Binding { Binding { sentError != nil @@ -31,8 +37,7 @@ struct SendToAllView: View { } } } - // TODO: Guard - + var body: some View { NavigationStack { List(selection: $contactRecipients) { @@ -88,7 +93,7 @@ struct SendToAllView: View { Section { RowButtonView("Contacter \(_totalString())") { - self._contactAndPay() + self._contact() } } } @@ -116,52 +121,55 @@ struct SendToAllView: View { Group { switch contactType { case .message(_, let recipients, let body, _): - if !self.cannotPayForTournament { - 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 + 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 } - } else { - SubscriptionView(isPresented: self.$cannotPayForTournament, showLackOfPlanMessage: true) } case .mail(_, let recipients, let bccRecipients, let body, let subject, _): - if !self.cannotPayForTournament { - MailComposeView(recipients: recipients, bccRecipients: bccRecipients, body: body, subject: subject) { result in - switch result { - case .cancelled, .saved: + 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 - case .failed: - self.contactType = nil - self.sentError = .mailFailed - case .sent: - if networkMonitor.connected == false { - self.contactType = nil - self.sentError = .mailNotSent - } - @unknown default: - break + self.sentError = .mailNotSent } + @unknown default: + break } - } else { - SubscriptionView(isPresented: self.$cannotPayForTournament, showLackOfPlanMessage: true) } } } .tint(.master) } - } - .onAppear { - self.cannotPayForTournament = Guard.main.paymentForNewTournament() == nil + .sheet(isPresented: self.$showSubscriptionView, content: { + NavigationStack { + SubscriptionView(isPresented: self.$showSubscriptionView, showLackOfPlanMessage: true) + } + }) + .sheet(isPresented: self.$showUserCreationView, content: { + NavigationStack { + LoginView { _ in + self.showUserCreationView = false + self._contact() + } + } + }) + } } @@ -191,15 +199,6 @@ struct SendToAllView: View { } } - fileprivate func _contactAndPay() { - do { - try tournament.payIfNecessary() - self._contact() - } catch { - self.cannotPayForTournament = true - } - } - func finalMessage() -> String { var message = [String?]() message.append("\n\n") @@ -215,13 +214,36 @@ struct SendToAllView: View { } fileprivate func _contact() { - if contactMethod == 0 { - contactType = .message(date: nil, recipients: _teams().flatMap { $0.unsortedPlayers() }.compactMap { $0.phoneNumber }, body: finalMessage(), tournamentBuild: nil) + + self._verifyUser { + self._payTournamentAndExecute { + + if contactMethod == 0 { + contactType = .message(date: nil, recipients: _teams().flatMap { $0.unsortedPlayers() }.compactMap { $0.phoneNumber }, body: finalMessage(), tournamentBuild: nil) + } else { + contactType = .mail(date: nil, recipients: tournament.umpireMail(), bccRecipients: _teams().flatMap { $0.unsortedPlayers() }.compactMap { $0.email }, body: finalMessage(), subject: tournament.tournamentTitle(), tournamentBuild: nil) + } + } + } + + } + + fileprivate func _verifyUser(_ handler: () -> ()) { + if Store.main.userId != nil { + handler() } else { - contactType = .mail(date: nil, recipients: tournament.umpireMail(), bccRecipients: _teams().flatMap { $0.unsortedPlayers() }.compactMap { $0.email }, body: finalMessage(), subject: tournament.tournamentTitle(), tournamentBuild: nil) + self.showUserCreationView = true } } + fileprivate func _payTournamentAndExecute(_ handler: () -> ()) { + do { + try tournament.payIfNecessary() + handler() + } catch { + self.showSubscriptionView = true + } + } } //#Preview { diff --git a/PadelClub/Views/Player/Components/EditablePlayerView.swift b/PadelClub/Views/Player/Components/EditablePlayerView.swift index b6f7ccd..4eb3b75 100644 --- a/PadelClub/Views/Player/Components/EditablePlayerView.swift +++ b/PadelClub/Views/Player/Components/EditablePlayerView.swift @@ -60,8 +60,7 @@ struct EditablePlayerView: View { } } - // TODO: Guard - + @ViewBuilder func computedPlayerView(_ player: PlayerRegistration) -> some View { VStack(alignment: .leading, spacing: 0.0) {