Fix purchase row issue

multistore
Laurent 1 year ago
parent a3c2db3823
commit 7f943fabd8
  1. 9
      PadelClub/Views/Subscription/Guard.swift
  2. 33
      PadelClub/Views/Subscription/PurchaseListView.swift
  3. 20
      PadelClub/Views/Subscription/SubscriptionView.swift

@ -87,7 +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)")
// 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`.
@ -156,16 +156,19 @@ import LeStorage
}
func userFilteredPurchases() -> [StoreKit.Transaction] {
Logger.log("self.purchasedTransactions = \(self.purchasedTransactions.count)")
// Logger.log("self.purchasedTransactions = \(self.purchasedTransactions.count)")
guard let currentUserUUID: UUID = Store.main.currentUserUUID else {
return []
}
let userTransactions = self.purchasedTransactions.filter { currentUserUUID == $0.appAccountToken }
let now = Date()
// print("now = \(now)")
return userTransactions.filter { transaction in
if let expirationDate = transaction.expirationDate {
return expirationDate > Date()
// print("exp = \(expirationDate)")
return expirationDate > now
} else {
return true
}

@ -19,7 +19,8 @@ class PurchaseManager: ObservableObject {
@Published var purchaseRows: [PurchaseRow] = []
init() {
NotificationCenter.default.addObserver(self, selector: #selector(_purchasesChanged(notification:)), name: NSNotification.Name.CollectionDidChange, object: Guard.main.purchases)
// NotificationCenter.default.addObserver(self, selector: #selector(_purchasesChanged(notification:)), name: NSNotification.Name.CollectionDidChange, object: Guard.main.purchases)
NotificationCenter.default.addObserver(self, selector: #selector(_purchasesChanged(notification:)), name: NSNotification.Name.StoreEventHappened, object: nil)
let identifiers: [String] = StoreItem.allCases.map { $0.rawValue }
Task {
@ -54,20 +55,30 @@ class PurchaseManager: ObservableObject {
var rows: [PurchaseRow] = []
let userPurchases: [StoreKit.Transaction] = Guard.main.userFilteredPurchases()
// Subscriptions
for userPurchase in userPurchases {
let subscriptions: [StoreItem] = StoreItem.allCases.filter { !$0.isConsumable }
for subscription in subscriptions {
if let item = StoreItem(rawValue: userPurchase.productID),
let product = self._products.first(where: { $0.id == item.rawValue } ) {
switch item {
case .fivePerMonth, .monthlyUnlimited:
rows.append(PurchaseRow(id: userPurchase.originalID, name: product.displayName, item: item))
case .unit:
break
}
if let userPurchase = userPurchases.first(where: { $0.productID == subscription.rawValue }), let product = self._products.first(where: { $0.id == subscription.rawValue }) {
rows.append(PurchaseRow(id: userPurchase.originalID, name: product.displayName, item: subscription))
}
}
// Subscriptions
// for userPurchase in userPurchases {
// print("from = \(userPurchase.purchaseDate) / to = \(userPurchase.expirationDate)")
// if let item = StoreItem(rawValue: userPurchase.productID),
// let product = self._products.first(where: { $0.id == item.rawValue } ) {
// switch item {
// case .fivePerMonth, .monthlyUnlimited:
// rows.append(PurchaseRow(id: userPurchase.originalID, name: product.displayName, item: item))
// case .unit:
// break
// }
// }
// }
// Units
let remainingTournaments = Guard.main.remainingTournaments
if remainingTournaments > 0 {

@ -76,15 +76,15 @@ class SubscriptionModel: ObservableObject, StoreDelegate {
Logger.w("missing product or store manager")
return false
}
if product.item.isConsumable {
if let _ = try await storeManager.purchase(product, quantity: self.quantity) {
return true
}
} else {
let _ = try await storeManager.purchase(product)
if product.item.isConsumable {
if let _ = try await storeManager.purchase(product, quantity: self.quantity) {
return true
}
} else {
let _ = try await storeManager.purchase(product)
return true
}
return false
}
@ -219,7 +219,7 @@ struct SubscriptionView: View {
.preferredColorScheme(.dark)
.navigationTitle("Abonnements")
.onAppear {
self._load()
self.model.load()
}
}
@ -246,10 +246,6 @@ struct SubscriptionView: View {
}
}
fileprivate func _load() {
self.model.load()
}
fileprivate func _restore() {
Task {

Loading…
Cancel
Save