-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathStoreKitHelper.swift
More file actions
125 lines (105 loc) · 3.68 KB
/
Copy pathStoreKitHelper.swift
File metadata and controls
125 lines (105 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// SPDX-FileCopyrightText: 2025 Davide De Rosa
//
// SPDX-License-Identifier: GPL-3.0
import Combine
import Foundation
import StoreKit
@MainActor
public final class StoreKitHelper<ProductType>: InAppHelper where ProductType: RawRepresentable & Hashable,
ProductType.RawValue == String {
private let products: [ProductType]
private let inAppIdentifier: (ProductType) -> String
private var activeTransactions: Set<Transaction>
private nonisolated let didUpdateSubject: PassthroughSubject<Void, Never>
private var observer: Task<Void, Never>?
public init(products: [ProductType], inAppIdentifier: @escaping (ProductType) -> String) {
self.products = products
self.inAppIdentifier = inAppIdentifier
activeTransactions = []
didUpdateSubject = PassthroughSubject()
observer = transactionsObserverTask()
}
deinit {
observer?.cancel()
}
}
extension StoreKitHelper {
public nonisolated var canMakePurchases: Bool {
AppStore.canMakePayments
}
public nonisolated var didUpdate: AnyPublisher<Void, Never> {
didUpdateSubject.eraseToAnyPublisher()
}
public func fetchProducts(timeout: TimeInterval) async throws -> [ProductType: InAppProduct] {
let skProducts = try await performTask(withTimeout: timeout) {
try await Product.products(for: self.products.map(self.inAppIdentifier))
}
return skProducts.reduce(into: [:]) {
guard let pid = ProductType(rawValue: $1.id) else {
return
}
$0[pid] = InAppProduct(
productIdentifier: $1.id,
localizedTitle: $1.displayName,
localizedDescription: $1.description,
localizedPrice: $1.displayPrice,
native: $1
)
}
}
public func purchase(_ inAppProduct: InAppProduct) async throws -> InAppPurchaseResult {
guard let skProduct = inAppProduct.native as? Product else {
return .notFound
}
switch try await skProduct.purchase() {
case .success(let verificationResult):
guard let transaction = try? verificationResult.payloadValue else {
break
}
activeTransactions.insert(transaction)
didUpdateSubject.send()
await transaction.finish()
return .done
case .pending:
return .pending
case .userCancelled:
break
@unknown default:
break
}
return .cancelled
}
public func restorePurchases() async throws {
do {
try await AppStore.sync()
} catch StoreKitError.userCancelled {
//
}
}
}
private extension StoreKitHelper {
nonisolated func transactionsObserverTask() -> Task<Void, Never> {
Task {
for await update in Transaction.updates {
guard let transaction = try? update.payloadValue else {
continue
}
await fetchActiveTransactions()
await transaction.finish()
guard !Task.isCancelled else {
break
}
}
}
}
func fetchActiveTransactions() async {
var activeTransactions: Set<Transaction> = []
for await entitlement in Transaction.currentEntitlements {
if let transaction = try? entitlement.payloadValue {
activeTransactions.insert(transaction)
}
}
self.activeTransactions = activeTransactions
didUpdateSubject.send()
}
}