Skip to content

Commit 7f2a1db

Browse files
committed
perf: Optimize error alert handling.
1 parent 99ab4f7 commit 7f2a1db

4 files changed

Lines changed: 15 additions & 31 deletions

File tree

Sources/StoreKitHelper/Comps/RestorePurchasesButton.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ struct RestorePurchasesButton: View {
2727
Button(action: {
2828
Task {
2929
restoringPurchase = true
30-
do {
31-
try await store.restorePurchases()
32-
restoringPurchase = false
33-
if store.purchasedProductIDs.count > 0 {
34-
popupDismissHandle?()
35-
} else if showError(error: store.storeError) == true {
36-
NotifyAlert.alert(title: store.storeError?.description(locale: locale) ?? noPurchaseTitle, message: "")
37-
}
38-
} catch {
39-
restoringPurchase = false
40-
NotifyAlert.alert(title: restoreFailedTitle, message: error.localizedDescription)
30+
await store.restorePurchases()
31+
restoringPurchase = false
32+
if store.purchasedProductIDs.count > 0 {
33+
popupDismissHandle?()
34+
} else if showError(error: store.storeError) == true {
35+
NotifyAlert.alert(title: store.storeError?.description(locale: locale) ?? noPurchaseTitle, message: "")
4136
}
4237
}
4338
}, label: {

Sources/StoreKitHelper/StoreKitHelper.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ public final class StoreContext: ObservableObject {
110110

111111
do {
112112
// 同步 App Store 状态
113-
let result = try await AppStore.sync()
114-
print("AppStore.sync:", result)
113+
try await AppStore.sync()
115114
// 更新购买状态
116115
await updatePurchasedProducts()
117116

Sources/StoreKitHelper/Views/StoreKitHelperSelectionView.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,10 @@ public struct StoreKitHelperSelectionView: View {
110110
let purchaseFailed = String.localizedString(key: "purchase_failed", locale: locale)
111111
Task {
112112
buyingProductID = product.id
113-
do {
114-
try await store.purchase(product)
115-
buyingProductID = nil
116-
if store.isPurchased(product.id) == true {
117-
popupDismissHandle?()
118-
}
119-
} catch {
120-
buyingProductID = nil
121-
NotifyAlert.alert(title: purchaseFailed, message: error.localizedDescription)
113+
await store.purchase(product)
114+
buyingProductID = nil
115+
if store.isPurchased(product.id) == true {
116+
popupDismissHandle?()
122117
}
123118
}
124119
}

Sources/StoreKitHelper/Views/StoreKitHelperView.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,10 @@ struct ProductsLoadList: View {
8484
let purchaseFailed = String.localizedString(key: "purchase_failed", locale: locale)
8585
Task {
8686
buyingProductID = product.id
87-
do {
88-
try await store.purchase(product)
89-
buyingProductID = nil
90-
if store.isPurchased(product.id) == true {
91-
popupDismissHandle?()
92-
}
93-
} catch {
94-
buyingProductID = nil
95-
NotifyAlert.alert(title: purchaseFailed, message: error.localizedDescription)
87+
await store.purchase(product)
88+
buyingProductID = nil
89+
if store.isPurchased(product.id) == true {
90+
popupDismissHandle?()
9691
}
9792
}
9893
}

0 commit comments

Comments
 (0)