Skip to content

Commit 429ece1

Browse files
committedJan 9, 2025
Refactor store service
1 parent bc699af commit 429ece1

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed
 

‎RELEASE_NOTES.md

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ Until then, deprecated features may be removed in any minor version, and breakin
99

1010
This version removes deprecated code and adjusts some service terminology.
1111

12+
### ‼️ Breaking Changes
13+
14+
This version updates `StoreService` by renaming `restorePurchases()` to `getValidProductTransations()`.
15+
16+
This should have been done in 0.4. As the function only returned transactions, this was very confusing.
17+
18+
By renaming it, we no longer risk calling it with the intention of restoring purchases, which it didn't do.
19+
20+
We can now use `syncStoreData(to:)` and `restorePurchases(syncWith:)` to perform the exact operation we want.
21+
22+
Since these changes involve protocol changes, the changes are breaking. They should hopefully be easy to fix.
23+
1224

1325

1426
## 0.5

‎Sources/StoreKitPlus/Services/StandardStoreService.swift

+4-8
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ open class StandardStoreService: StoreService {
8888
#endif
8989
}
9090

91-
@discardableResult
92-
open func restorePurchases() async throws -> [Transaction] {
91+
open func getValidProductTransations() async throws -> [Transaction] {
9392
var transactions: [Transaction] = []
9493
for id in productIds {
9594
if let transaction = try await getValidTransaction(for: id) {
@@ -104,7 +103,7 @@ open class StandardStoreService: StoreService {
104103
) async throws {
105104
let products = try await getProducts()
106105
await context.updateProducts(products)
107-
try await restorePurchases(syncWith: context)
106+
try await restorePurchases(with: context)
108107
}
109108

110109

@@ -117,11 +116,8 @@ open class StandardStoreService: StoreService {
117116
let transaction = try result.verify()
118117
await transaction.finish()
119118
}
120-
121-
/// Try to resolve a valid transaction for a certain ID.
122-
///
123-
/// This function will fetch and verify all transactions
124-
/// before returning them.
119+
120+
/// Get all valid transations for a certain product.
125121
open func getValidTransaction(
126122
for productId: ProductID
127123
) async throws -> Transaction? {

‎Sources/StoreKitPlus/Services/StoreService.swift

+7-18
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ public protocol StoreService {
1919

2020
/// Get all available products.
2121
func getProducts() async throws -> [Product]
22+
23+
/// Get all valid product transations.
24+
func getValidProductTransations() async throws -> [Transaction]
2225

2326
/// Purchase a certain product.
2427
@discardableResult
2528
func purchase(
2629
_ product: Product
2730
) async throws -> (Product.PurchaseResult, Transaction?)
2831

29-
/// Restore previous purchases.
30-
@discardableResult
31-
func restorePurchases() async throws -> [Transaction]
32-
3332
/// Sync StoreKit products and purchases to a context.
3433
func syncStoreData(
3534
to context: StoreContext
@@ -56,20 +55,10 @@ public extension StoreService {
5655
/// Restore previous purchases.
5756
///
5857
/// This function will sync the result to the context.
59-
@discardableResult
6058
func restorePurchases(
61-
syncWith context: StoreContext
62-
) async throws -> [Transaction] {
63-
let result = try await restorePurchases()
64-
await context.updatePurchaseTransactions(result)
65-
return result
66-
}
67-
}
68-
69-
public extension StoreService {
70-
71-
@available(*, deprecated, message: "You have to pass in a context now.")
72-
func syncStoreData() async throws {
73-
try await syncStoreData(to: .init())
59+
with context: StoreContext
60+
) async throws {
61+
let transactions = try await getValidProductTransations()
62+
await context.updatePurchaseTransactions(transactions)
7463
}
7564
}

‎Sources/StoreKitPlus/StoreKitPlus.docc/Getting-Started.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ Since purchases involve a bunch of steps and can become complicated, you can als
113113

114114
```swift
115115
let ids = ["com.your-app.productid"]
116+
let context = StoreContext()
116117
let service = StandardStorePurchaseService(productIds: ids)
117-
let result = try await service.purchase(product)
118+
let result = try await service.purchase(product, syncWith: context)
118119
```
119120

120121
The ``StandardStoreService`` will automatically verify and finish the purchase transactions.
@@ -130,7 +131,7 @@ However, this involves many steps and can become complicated. To make things eas
130131
```swift
131132
let ids = ["com.your-app.productid"]
132133
let service = StandardStorePurchaseService(productIds: ids)
133-
try await service.restorePurchases()
134+
try await service.restorePurchases(with: context)
134135
```
135136

136137
The standard service communicates with StoreKit and syncs the result with the provided context.

0 commit comments

Comments
 (0)