Fixes payment when summoning

multistore
Laurent 1 year ago
parent bc75a1e9ea
commit c733a0f386
  1. 72
      PadelClub/Views/Calling/CallView.swift
  2. 24
      PadelClub/Views/Calling/SendToAllView.swift
  3. 8
      PadelClub/Views/Match/MatchDetailView.swift
  4. 2
      PadelClub/Views/Subscription/Guard.swift
  5. 64
      PadelClub/Views/Subscription/SubscriptionView.swift
  6. 6
      PadelClub/Views/User/UserCreationView.swift

@ -59,7 +59,7 @@ struct CallView: View {
@State private var contactType: ContactType? = nil @State private var contactType: ContactType? = nil
@State private var sentError: ContactManagerError? = nil @State private var sentError: ContactManagerError? = nil
@State var cannotPayForTournament: Bool = false @State var showSubscriptionView: Bool = false
var messageSentFailed: Binding<Bool> { var messageSentFailed: Binding<Bool> {
Binding { Binding {
@ -101,22 +101,23 @@ struct CallView: View {
Text(callWord + " ces \(teams.count) paires par") Text(callWord + " ces \(teams.count) paires par")
} }
Button { Button {
contactType = .message(date: callDate, recipients: teams.flatMap { $0.getPhoneNumbers() }, body: finalMessage, tournamentBuild: nil) self._payTournamentAndExecute {
self._contactByMessage()
}
} label: { } label: {
Text("sms") Text("sms")
.underline() .underline()
} }
Text("ou") Text("ou")
Button { Button {
contactType = .mail(date: callDate, recipients: tournament.umpireMail(), bccRecipients: teams.flatMap { $0.getMail() }, body: finalMessage, subject: tournament.tournamentTitle(), tournamentBuild: nil) self._payTournamentAndExecute {
self._contactByMail()
}
} label: { } label: {
Text("mail") Text("mail")
.underline() .underline()
} }
} }
.onAppear {
self.cannotPayForTournament = Guard.main.paymentForNewTournament() == nil
}
.font(.subheadline) .font(.subheadline)
.buttonStyle(.borderless) .buttonStyle(.borderless)
.alert("Un problème est survenu", isPresented: messageSentFailed) { .alert("Un problème est survenu", isPresented: messageSentFailed) {
@ -130,7 +131,6 @@ struct CallView: View {
Group { Group {
switch contactType { switch contactType {
case .message(_, let recipients, let body, _): case .message(_, let recipients, let body, _):
if !self.cannotPayForTournament {
MessageComposeView(recipients: recipients, body: body) { result in MessageComposeView(recipients: recipients, body: body) { result in
switch result { switch result {
case .cancelled: case .cancelled:
@ -147,35 +147,51 @@ struct CallView: View {
break break
} }
} }
} else {
SubscriptionView(isPresented: self.$cannotPayForTournament, showLackOfPlanMessage: true)
}
case .mail(_, let recipients, let bccRecipients, let body, let subject, _): case .mail(_, let recipients, let bccRecipients, let body, let subject, _):
if !self.cannotPayForTournament { MailComposeView(recipients: recipients, bccRecipients: bccRecipients, body: body, subject: subject) { result in
MailComposeView(recipients: recipients, bccRecipients: bccRecipients, body: body, subject: subject) { result in switch result {
switch result { case .cancelled, .saved:
case .cancelled, .saved: self.contactType = nil
case .failed:
self.contactType = nil
self.sentError = .mailFailed
case .sent:
if networkMonitor.connected == false {
self.contactType = nil self.contactType = nil
case .failed: self.sentError = .mailNotSent
self.contactType = nil } else {
self.sentError = .mailFailed _called(true)
case .sent:
if networkMonitor.connected == false {
self.contactType = nil
self.sentError = .mailNotSent
} else {
_called(true)
}
@unknown default:
break
} }
@unknown default:
break
} }
} else {
SubscriptionView(isPresented: self.$cannotPayForTournament, showLackOfPlanMessage: true)
} }
} }
} }
.tint(.master) .tint(.master)
} }
.sheet(isPresented: self.$showSubscriptionView, content: {
NavigationStack {
SubscriptionView(isPresented: self.$showSubscriptionView, showLackOfPlanMessage: true)
}
})
}
fileprivate func _payTournamentAndExecute(_ handler: () -> ()) {
do {
try tournament.payIfNecessary()
handler()
} catch {
self.showSubscriptionView = true
}
}
fileprivate func _contactByMessage() {
contactType = .message(date: callDate, recipients: teams.flatMap { $0.getPhoneNumbers() }, body: finalMessage, tournamentBuild: nil)
} }
fileprivate func _contactByMail() {
contactType = .mail(date: callDate, recipients: tournament.umpireMail(), bccRecipients: teams.flatMap { $0.getMail() }, body: finalMessage, subject: tournament.tournamentTitle(), tournamentBuild: nil)
}
} }

@ -71,11 +71,7 @@ struct SendToAllView: View {
Section { Section {
RowButtonView("Contacter \(_totalString())") { RowButtonView("Contacter \(_totalString())") {
if contactMethod == 0 { self._contactAndPay()
contactType = .message(date: nil, recipients: _teams().flatMap { $0.unsortedPlayers() }.compactMap { $0.phoneNumber }, body: addLink ? tournament.shareURL()?.absoluteString : nil, tournamentBuild: nil)
} else {
contactType = .mail(date: nil, recipients: tournament.umpireMail(), bccRecipients: _teams().flatMap { $0.unsortedPlayers() }.compactMap { $0.email }, body: addLink ? tournament.shareURL()?.absoluteString : nil, subject: tournament.tournamentTitle(), tournamentBuild: nil)
}
} }
} }
} }
@ -166,6 +162,24 @@ struct SendToAllView: View {
return teams.count.formatted() + " équipe" + teams.count.pluralSuffix return teams.count.formatted() + " équipe" + teams.count.pluralSuffix
} }
} }
fileprivate func _contactAndPay() {
do {
try tournament.payIfNecessary()
self._contact()
} catch {
self.cannotPayForTournament = true
}
}
fileprivate func _contact() {
if contactMethod == 0 {
contactType = .message(date: nil, recipients: _teams().flatMap { $0.unsortedPlayers() }.compactMap { $0.phoneNumber }, body: addLink ? tournament.shareURL()?.absoluteString : nil, tournamentBuild: nil)
} else {
contactType = .mail(date: nil, recipients: tournament.umpireMail(), bccRecipients: _teams().flatMap { $0.unsortedPlayers() }.compactMap { $0.email }, body: addLink ? tournament.shareURL()?.absoluteString : nil, subject: tournament.tournamentTitle(), tournamentBuild: nil)
}
}
} }
#Preview { #Preview {

@ -254,7 +254,9 @@ struct MatchDetailView: View {
} }
} }
} else { } else {
SubscriptionView(isPresented: self.$showSubscriptionView, showLackOfPlanMessage: true) NavigationStack {
SubscriptionView(isPresented: self.$showSubscriptionView, showLackOfPlanMessage: true)
}
} }
case .mail(_, let recipients, let bccRecipients, let body, let subject, _): case .mail(_, let recipients, let bccRecipients, let body, let subject, _):
if Guard.main.paymentForNewTournament() != nil { if Guard.main.paymentForNewTournament() != nil {
@ -275,7 +277,9 @@ struct MatchDetailView: View {
} }
} }
} else { } else {
SubscriptionView(isPresented: self.$showSubscriptionView, showLackOfPlanMessage: true) NavigationStack {
SubscriptionView(isPresented: self.$showSubscriptionView, showLackOfPlanMessage: true)
}
} }
} }
} }

@ -163,7 +163,7 @@ import LeStorage
let userTransactions = self.purchasedTransactions.filter { currentUserUUID == $0.appAccountToken } let userTransactions = self.purchasedTransactions.filter { currentUserUUID == $0.appAccountToken }
let now = Date() let now: Date = Date()
// print("now = \(now)") // print("now = \(now)")
return userTransactions.filter { transaction in return userTransactions.filter { transaction in
if let expirationDate = transaction.expirationDate { if let expirationDate = transaction.expirationDate {

@ -52,6 +52,10 @@ class SubscriptionModel: ObservableObject, StoreDelegate {
@Published var products: [Product] = [] @Published var products: [Product] = []
@Published var totalPrice: String = "" @Published var totalPrice: String = ""
init() {
Logger.log("SubscriptionModel init ")
}
func load() { func load() {
self.isLoading = true self.isLoading = true
if self.storeManager == nil { if self.storeManager == nil {
@ -116,9 +120,17 @@ struct SubscriptionView: View {
@State var isPurchasing: Bool = false @State var isPurchasing: Bool = false
@State var showSuccessfulPurchaseView: Bool = false @State var showSuccessfulPurchaseView: Bool = false
init(isPresented: Binding<Bool>, showLackOfPlanMessage: Bool = false) {
self._isPresented = isPresented
self.showLackOfPlanMessage = showLackOfPlanMessage
Logger.log(">>> SubscriptionView init")
}
var body: some View { var body: some View {
Group { VStack {
if self.showLoginView { if self.showLoginView {
LoginView { _ in LoginView { _ in
self.showLoginView = false self.showLoginView = false
@ -126,16 +138,13 @@ struct SubscriptionView: View {
} }
} else { } else {
List { if self.showLackOfPlanMessage {
SubscriptionDetailView()
.clipShape(.rect(cornerRadius: 16.0))
.padding()
}
if self.showLackOfPlanMessage { List {
HStack {
Image(systemName: "exclamationmark.bubble.fill").foregroundStyle(Color.accentColor)
.font(.title)
Text("Vous ne disposez malheureusement plus d'offre pour continuer votre tournoi. Voici ce que nous proposons:")
.fontWeight(.semibold)
}
}
if self.model.products.count > 0 { if self.model.products.count > 0 {
@ -198,6 +207,20 @@ struct SubscriptionView: View {
} }
} }
} else {
if self.model.isLoading {
ProgressView()
} else {
HStack {
if let plan = Guard.main.currentPlan {
Image(systemName: plan.systemImage)
} else {
Image(systemName: "questionmark.diamond.fill")
}
Text("Il n'y a pas de produits à vous proposer")
}
}
} }
} }
.listStyle(.grouped) .listStyle(.grouped)
@ -344,6 +367,27 @@ struct SubscriptionFooterView: View {
} }
} }
struct SubscriptionDetailView: View {
var body: some View {
HStack {
Image(systemName: "exclamationmark.bubble.fill")
//.foregroundStyle(Color.accentColor)
.font(.title)
Text("Vous ne disposez malheureusement plus d'offre pour continuer votre tournoi. Voici ce que nous proposons:")
.fontWeight(.semibold)
}
.padding()
.background(.orange)
.foregroundStyle(.black)
}
}
#Preview {
SubscriptionDetailView()
}
#Preview { #Preview {
NavigationStack { NavigationStack {
SubscriptionView(isPresented: .constant(true), showLackOfPlanMessage: false) SubscriptionView(isPresented: .constant(true), showLackOfPlanMessage: false)

@ -25,6 +25,7 @@ struct UserCreationFormView: View {
@State var alertMessage: String = "" { @State var alertMessage: String = "" {
didSet { didSet {
self.showAlertView = true self.showAlertView = true
self.isLoading = false
} }
} }
@ -97,7 +98,6 @@ struct UserCreationFormView: View {
} ) } )
} }
fileprivate func _selectCountry() { fileprivate func _selectCountry() {
guard let regionCode = Locale.current.region?.identifier, let country = Locale.current.localizedString(forRegionCode: regionCode) else { guard let regionCode = Locale.current.region?.identifier, let country = Locale.current.localizedString(forRegionCode: regionCode) else {
return return
@ -108,6 +108,8 @@ struct UserCreationFormView: View {
fileprivate func _create() { fileprivate func _create() {
self.isLoading = true
guard self.password1 == self.password2 else { guard self.password1 == self.password2 else {
self.alertMessage = "Les mots de passe ne correspondent pas" self.alertMessage = "Les mots de passe ne correspondent pas"
return return
@ -133,8 +135,6 @@ struct UserCreationFormView: View {
return return
} }
self.isLoading = true
Task { Task {
do { do {
let userCreationForm = UserCreationForm( let userCreationForm = UserCreationForm(

Loading…
Cancel
Save