fixes stuff for subscriptions

multistore
Laurent 2 years ago
parent 7996e4b1d6
commit a3c2db3823
  1. 1
      PadelClub/Utils/SourceFileManager.swift
  2. 15
      PadelClub/Views/Calling/CallView.swift
  3. 13
      PadelClub/Views/Calling/SendToAllView.swift
  4. 6
      PadelClub/Views/Match/MatchDetailView.swift
  5. 2
      PadelClub/Views/Navigation/Umpire/UmpireView.swift
  6. 11
      PadelClub/Views/Subscription/Guard.swift
  7. 49
      PadelClub/Views/Subscription/SubscriptionView.swift

@ -35,7 +35,6 @@ class SourceFileManager {
}
}
var lastDataSource: String? {
DataStore.shared.appSettings.lastDataSource
}

@ -59,6 +59,8 @@ struct CallView: View {
@State private var contactType: ContactType? = nil
@State private var sentError: ContactManagerError? = nil
@State var cannotPayForTournament: Bool = false
var messageSentFailed: Binding<Bool> {
Binding {
sentError != nil
@ -82,8 +84,6 @@ struct CallView: View {
ContactType.callingMessage(tournament: tournament, startDate: callDate, roundLabel: roundLabel, matchFormat: matchFormat)
}
// TODO: Guard
var body: some View {
let callWord = teams.allSatisfy({ $0.called() }) ? "Reconvoquer" : "Convoquer"
HStack {
@ -110,6 +110,9 @@ struct CallView: View {
.underline()
}
}
.onAppear {
self.cannotPayForTournament = Guard.main.paymentForNewTournament() == nil
}
.font(.subheadline)
.buttonStyle(.borderless)
.alert("Un problème est survenu", isPresented: messageSentFailed) {
@ -123,7 +126,7 @@ struct CallView: View {
Group {
switch contactType {
case .message(_, let recipients, let body, _):
if Guard.main.paymentForNewTournament() != nil {
if !self.cannotPayForTournament {
MessageComposeView(recipients: recipients, body: body) { result in
switch result {
case .cancelled:
@ -142,10 +145,10 @@ struct CallView: View {
}
}
} else {
SubscriptionView(showLackOfPlanMessage: true)
SubscriptionView(isPresented: self.$cannotPayForTournament, showLackOfPlanMessage: true)
}
case .mail(_, let recipients, let bccRecipients, let body, let subject, _):
if Guard.main.paymentForNewTournament() != nil {
if !self.cannotPayForTournament {
MailComposeView(recipients: recipients, bccRecipients: bccRecipients, body: body, subject: subject) { result in
switch result {
case .cancelled, .saved:
@ -166,7 +169,7 @@ struct CallView: View {
}
}
} else {
SubscriptionView(showLackOfPlanMessage: true)
SubscriptionView(isPresented: self.$cannotPayForTournament, showLackOfPlanMessage: true)
}
}
}

@ -17,6 +17,8 @@ struct SendToAllView: View {
@State private var contactRecipients: Set<String> = Set()
@State private var sentError: ContactManagerError? = nil
@State var cannotPayForTournament: Bool = false
var messageSentFailed: Binding<Bool> {
Binding {
sentError != nil
@ -93,7 +95,7 @@ struct SendToAllView: View {
Group {
switch contactType {
case .message(_, let recipients, let body, _):
if Guard.main.paymentForNewTournament() != nil {
if !self.cannotPayForTournament {
MessageComposeView(recipients: recipients, body: body) { result in
switch result {
case .cancelled:
@ -109,10 +111,10 @@ struct SendToAllView: View {
}
}
} else {
SubscriptionView(showLackOfPlanMessage: true)
SubscriptionView(isPresented: self.$cannotPayForTournament, showLackOfPlanMessage: true)
}
case .mail(_, let recipients, let bccRecipients, let body, let subject, _):
if Guard.main.paymentForNewTournament() != nil {
if !self.cannotPayForTournament {
MailComposeView(recipients: recipients, bccRecipients: bccRecipients, body: body, subject: subject) { result in
switch result {
case .cancelled, .saved:
@ -130,13 +132,16 @@ struct SendToAllView: View {
}
}
} else {
SubscriptionView(showLackOfPlanMessage: true)
SubscriptionView(isPresented: self.$cannotPayForTournament, showLackOfPlanMessage: true)
}
}
}
.tint(.master)
}
}
.onAppear {
self.cannotPayForTournament = Guard.main.paymentForNewTournament() == nil
}
}
func _teams() -> [TeamRegistration] {

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

@ -167,7 +167,7 @@ struct UmpireView: View {
}
.sheet(isPresented: self.$showSubscriptions, content: {
NavigationStack {
SubscriptionView()
SubscriptionView(isPresented: self.$showSubscriptions)
}
})
.sheet(isPresented: $presentSearchView) {

@ -87,6 +87,7 @@ import LeStorage
@MainActor
func updatePurchasedIdentifiers(_ transaction: StoreKit.Transaction) async {
Logger.log("\(transaction.productID) > purchase = \(transaction.originalPurchaseDate), exp date= \(transaction.expirationDate), rev date = \(transaction.revocationDate)")
do {
if transaction.revocationDate == nil {
// If the App Store has not revoked the transaction, add it to the list of `purchasedIdentifiers`.
@ -205,8 +206,14 @@ import LeStorage
return Tournament.TournamentPayment.subscriptionUnit
}
}
return nil
return self._paymentWithoutSubscription()
default:
return self._paymentWithoutSubscription()
}
}
fileprivate func _paymentWithoutSubscription() -> Tournament.TournamentPayment? {
let freelyPayed: Int = DataStore.shared.tournaments.filter { $0.payment == .free && $0.isCanceled == false }.count
if freelyPayed < 1 {
return Tournament.TournamentPayment.free
@ -219,8 +226,6 @@ import LeStorage
return nil
}
}
var remainingTournaments: Int {
let unitlyPayed = DataStore.shared.tournaments.filter { $0.payment == Tournament.TournamentPayment.unit }.count
let tournamentCreditCount = self._purchasedTournamentCount()

@ -38,7 +38,6 @@ class SubscriptionModel: ObservableObject, StoreDelegate {
@Published var error: Error? = nil
@Published var isLoading: Bool = false
@Published var isPurchasing: Bool = false
@Published var selectedProduct: Product? = nil {
didSet {
self._computePrice()
@ -52,7 +51,6 @@ class SubscriptionModel: ObservableObject, StoreDelegate {
}
@Published var products: [Product] = []
@Published var totalPrice: String = ""
@State var showSuccessfulPurchaseView: Bool = false
func load() {
self.isLoading = true
@ -72,30 +70,22 @@ class SubscriptionModel: ObservableObject, StoreDelegate {
self.error = error
}
func purchase() {
func purchase() async throws -> Bool {
Logger.log("start purchase...")
guard let product: Product = self.selectedProduct, let storeManager = self.storeManager else {
Logger.w("missing product or store manager")
return
return false
}
self.isPurchasing = true
Task {
do {
if product.item.isConsumable {
if let _ = try await storeManager.purchase(product, quantity: self.quantity) {
self.isPurchasing = false
self.showSuccessfulPurchaseView = true
return true
}
} else {
let _ = try await storeManager.purchase(product)
self.isPurchasing = false
}
} catch {
Logger.error(error)
self.isPurchasing = false
}
return true
}
return false
}
fileprivate func _computePrice() {
@ -118,9 +108,13 @@ struct SubscriptionView: View {
@ObservedObject var model: SubscriptionModel = SubscriptionModel()
@Binding var isPresented: Bool
var showLackOfPlanMessage: Bool = false
@State var isRestoring: Bool = false
@State var showLoginView: Bool = false
@State var isPurchasing: Bool = false
@State var showSuccessfulPurchaseView: Bool = false
var body: some View {
@ -178,7 +172,7 @@ struct SubscriptionView: View {
}
} label: {
HStack {
if self.model.isPurchasing {
if self.isPurchasing {
Spacer()
ProgressView().tint(.white)
Spacer()
@ -230,7 +224,26 @@ struct SubscriptionView: View {
}
fileprivate func _purchase() {
self.model.purchase()
self.isPurchasing = true
Task {
do {
let success = try await self.model.purchase()
DispatchQueue.main.async {
self.isPurchasing = false
self.showSuccessfulPurchaseView = true
if success {
self.isPresented = false
}
}
} catch {
Logger.error(error)
DispatchQueue.main.async {
self.isPurchasing = false
}
}
}
}
fileprivate func _load() {
@ -337,6 +350,6 @@ struct SubscriptionFooterView: View {
#Preview {
NavigationStack {
SubscriptionView(showLackOfPlanMessage: false)
SubscriptionView(isPresented: .constant(true), showLackOfPlanMessage: false)
}
}

Loading…
Cancel
Save