|
|
|
|
@ -86,30 +86,51 @@ import LeStorage |
|
|
|
|
|
|
|
|
|
@MainActor |
|
|
|
|
func updatePurchasedIdentifiers(_ transaction: StoreKit.Transaction) async { |
|
|
|
|
if transaction.revocationDate == nil { |
|
|
|
|
// If the App Store has not revoked the transaction, add it to the list of `purchasedIdentifiers`. |
|
|
|
|
purchasedTransactions.insert(transaction) |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
if transaction.revocationDate == nil { |
|
|
|
|
// If the App Store has not revoked the transaction, add it to the list of `purchasedIdentifiers`. |
|
|
|
|
purchasedTransactions.insert(transaction) |
|
|
|
|
|
|
|
|
|
try self._addPurchaseIfPossible(transaction: transaction) |
|
|
|
|
|
|
|
|
|
// if self._purchaseById(transaction.originalID) == nil { |
|
|
|
|
// let purchase: Purchase = transaction.purchase() |
|
|
|
|
// try self.purchases.addOrUpdate(instance: purchase) |
|
|
|
|
// } |
|
|
|
|
} else { |
|
|
|
|
// If the App Store has revoked this transaction, remove it from the list of `purchasedIdentifiers`. |
|
|
|
|
purchasedTransactions.remove(transaction) |
|
|
|
|
try self._updatePurchaseIfPossible(transaction: transaction) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self._updateBestPlan() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _addPurchaseIfPossible(transaction: StoreKit.Transaction) throws { |
|
|
|
|
if self.purchases.hasLoadedFromServer { |
|
|
|
|
if self._purchaseById(transaction.originalID) == nil { |
|
|
|
|
let purchase: Purchase = transaction.purchase() |
|
|
|
|
// let json = try? purchase.jsonString() ?? "nope" |
|
|
|
|
// Logger.log("Add or update purchase = \(json) ") |
|
|
|
|
try self.purchases.addOrUpdate(instance: purchase) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// If the App Store has revoked this transaction, remove it from the list of `purchasedIdentifiers`. |
|
|
|
|
purchasedTransactions.remove(transaction) |
|
|
|
|
if let existing = self.purchases.first(where: { $0.identifier == transaction.originalID }) { |
|
|
|
|
do { |
|
|
|
|
try self.purchases.delete(instance: existing) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _updatePurchaseIfPossible(transaction: StoreKit.Transaction) throws { |
|
|
|
|
if self.purchases.hasLoadedFromServer { |
|
|
|
|
if let existing: Purchase = self._purchaseById(transaction.originalID) { |
|
|
|
|
existing.revocationDate = transaction.revocationDate |
|
|
|
|
try self.purchases.addOrUpdate(instance: existing) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
self._updateBestPlan() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _purchaseById(_ transactionId: UInt64) -> Purchase? { |
|
|
|
|
return self.purchases.first(where: { $0.identifier == transactionId }) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func processTransactionResult(_ result: VerificationResult<StoreKit.Transaction>) async throws -> StoreKit.Transaction { |
|
|
|
|
@ -123,19 +144,19 @@ import LeStorage |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var currentPlan: StoreItem? { |
|
|
|
|
#if DEBUG |
|
|
|
|
return nil |
|
|
|
|
#else |
|
|
|
|
// #if DEBUG |
|
|
|
|
// return nil |
|
|
|
|
// #else |
|
|
|
|
if let currentBestPlan = self.currentBestPlan, let plan = StoreItem(rawValue: currentBestPlan.productID) { |
|
|
|
|
return plan |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
#endif |
|
|
|
|
// #endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func userFilteredPurchases() -> [StoreKit.Transaction] { |
|
|
|
|
Logger.log("self.purchasedTransactions = \(self.purchasedTransactions.count)") |
|
|
|
|
guard let currentUserUUID = Store.main.currentUserUUID else { |
|
|
|
|
guard let currentUserUUID: UUID = Store.main.currentUserUUID else { |
|
|
|
|
return [] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -179,18 +200,18 @@ import LeStorage |
|
|
|
|
return Tournament.TournamentPayment.unlimited |
|
|
|
|
case .fivePerMonth: |
|
|
|
|
if let purchaseDate = self.currentBestPlan?.originalPurchaseDate { |
|
|
|
|
let tournaments = DataStore.shared.tournaments.filter { $0.creationDate > purchaseDate && $0.isCanceled == false } |
|
|
|
|
let tournaments = DataStore.shared.tournaments.filter { $0.creationDate > purchaseDate && $0.payment == .subscriptionUnit && $0.isCanceled == false } |
|
|
|
|
if tournaments.count < StoreItem.five { |
|
|
|
|
return Tournament.TournamentPayment.subscriptionUnit |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
default: |
|
|
|
|
let freelyPayed = DataStore.shared.tournaments.filter { $0.payment == .free && $0.isCanceled == false }.count |
|
|
|
|
let freelyPayed: Int = DataStore.shared.tournaments.filter { $0.payment == .free && $0.isCanceled == false }.count |
|
|
|
|
if freelyPayed < 1 { |
|
|
|
|
return Tournament.TournamentPayment.free |
|
|
|
|
} |
|
|
|
|
let tournamentCreditCount = self._purchasedTournamentCount() |
|
|
|
|
let tournamentCreditCount: Int = self._purchasedTournamentCount() |
|
|
|
|
let unitlyPayed = DataStore.shared.tournaments.filter { $0.payment == .unit && $0.isCanceled == false }.count |
|
|
|
|
if tournamentCreditCount > unitlyPayed { |
|
|
|
|
return Tournament.TournamentPayment.unit |
|
|
|
|
|