@ -25,7 +25,7 @@ class PurchaseManager: ObservableObject {
Task {
do {
self . _products = try await Product . products ( for : identifiers )
self . _buildRows ( )
self . _buildRowsOnMainThread ( )
} catch {
Logger . error ( error )
}
@ -38,33 +38,46 @@ class PurchaseManager: ObservableObject {
}
self . _purchases . removeAll ( )
self . _purchases . append ( contentsOf : collection )
self . _buildRows ( )
self . _buildRowsOnMainThread ( )
}
fileprivate func _buildRows ( ) {
fileprivate func _buildRowsOnMainThread ( ) {
DispatchQueue . main . async {
self . _buildRows ( )
}
}
fileprivate func _buildRows ( ) {
var rows : [ PurchaseRow ] = [ ]
let userPurchases : [ StoreKit . Transaction ] = Guard . main . userFilteredPurchases ( )
// S u b s c r i p t i o n s
for userPurchase in userPurchases {
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 ( name : product . displayName , item : item ) )
case . unit :
let remainingTournaments = Guard . main . remainingTournaments
if remainingTournaments > 0 {
rows . append ( PurchaseRow ( name : product . displayName , item : item , quantity : remainingTournaments ) )
break
}
default :
rows . append ( PurchaseRow ( name : product . displayName , item : item ) )
}
}
// U n i t s
let remainingTournaments = Guard . main . remainingTournaments
if remainingTournaments > 0 {
let unitItem : StoreItem = StoreItem . unit
if let product = self . _products . first ( where : { $0 . id = = unitItem . rawValue } ) {
rows . append ( PurchaseRow ( name : product . displayName , item : unitItem , quantity : remainingTournaments ) )
}
self . purchaseRows = rows
}
self . purchaseRows = rows
}
}
@ -104,7 +117,7 @@ struct PurchaseView: View {
Spacer ( )
if let quantity = purchaseRow . quantity {
let remaining = Guard . main . remainingTournaments
Text ( " \( remaining ) / \( quantity . formatted ( ) ) " )
Text ( " \( remaining ) " )
}
}
}