Skip to content

Commit

Permalink
Merge pull request #2 from russell-archer/OnTransaction-and-OnProducts
Browse files Browse the repository at this point in the history
PR for OnTransaction and OnProductsAvailable
  • Loading branch information
russell-archer authored Jan 26, 2025
2 parents 2fe65fa + d2e6c95 commit b379acc
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .swiftpm/xcode/xcshareddata/xcschemes/SKHelper.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1600"
LastUpgradeVersion = "1620"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
3 changes: 0 additions & 3 deletions Sources/SKHelper/Core/SKHelper+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public extension SKHelper {
/// All `ProductId` that represent purchased products.
var allPurchasedProductIds: [ProductId] { products.filter { $0.hasEntitlement }.map { $0.id }}

/// This property is true if `SKHelper.products` contains a valid collection of products, false otherwise.
var hasProducts: Bool { !products.isEmpty }

/// This property is true if `SKHelper.products` contains one or more consumable products, false otherwise.
var hasConsumableProducts: Bool { !allSKHelperConsumableProducts.isEmpty }

Expand Down
16 changes: 14 additions & 2 deletions Sources/SKHelper/Core/SKHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public typealias SubscriptionStatusChangeClosure = (_ productId: ProductId, _ tr
///
public typealias TransactionUpdateClosure = (_ productId: ProductId, _ reason: SKHelperTransactionUpdateReason, _ transaction: StoreKit.Transaction?) -> Void

public typealias ProductsAvailableClosure = (_ products: [SKHelperProduct]) -> Void

/// SKHelper, a lightweight StoreKit helper.
@MainActor
@available(iOS 17.0, macOS 14.6, *)
Expand All @@ -46,15 +48,18 @@ public class SKHelper: Observable {
/// Array of `SKHelperProduct`, which includes localized `Product` info retrieved from the App Store and a cached product entitlement.
public private(set) var products = [SKHelperProduct]()

/// This property is true if `SKHelper.products` contains a valid collection of products, false otherwise.
public private(set) var hasProducts = false

/// When set to true `SKHelper` will use previously cached values for product entitlements if calls to `Transaction.currentEntitlement(for:)` return nil.
/// Using cached entitlements can help mitigate issues where `Transaction.currentEntitlement(for:)` can sometimes erroneously indicate the user does not have an
/// entitlement to use a product.
public var useCachedEntitlements = true

/// A closure which is called when a subscription changes status.
/// A view modifier which is called when a subscription changes status.
public var subscriptionStatusChange: SubscriptionStatusChangeClosure?

/// Allows you to provide a ``TransactionUpdateClosure`` closure, which is called when a purchase transaction or transaction update is received.
/// Allows you to provide a ``TransactionUpdateClosure`` view modifier, which is called when a purchase transaction or transaction update is received.
///
/// You will receive notifications when:
///
Expand All @@ -68,6 +73,9 @@ public class SKHelper: Observable {
/// Use this means of receiving transaction notifications in preference to the SwiftUI `onInAppPurchaseCompletion(perform:)` view modifier.
///
public var transactionUpdateListener: TransactionUpdateClosure?

/// A view modifier which is called when SKHelper has successfully retrieved a collection of localized products.
public var productsAvailable: ProductsAvailableClosure?

// MARK: - Private properties

Expand Down Expand Up @@ -150,6 +158,10 @@ public class SKHelper: Observable {
products.append(SKHelperProduct(product: localizedProduct, hasEntitlement: purchasedProductIds.contains(localizedProduct.id)))
}

// Signal that we have successfully retrieved localized product information
hasProducts = true
productsAvailable?(products)

SKHelperLog.event(.requestProductsSuccess)
return true
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/SKHelper/Documentation.docc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ support to **iOS 17+** and **macOS 14.6+** SwiftUI apps.

`SKHelper` provides the following features:

- Multi-platform (iOS, macOS) SwiftUI support for purchasing **Consumable** (not yet implemented), **Non-consumable** and **Subscription** products
- Multi-platform (iOS, macOS) SwiftUI support for purchasing **Consumable**, **Non-consumable** and **Subscription** products
- Makes use of Apple's **StoreKit Views** to provide a standard and easily customizable UI
- Supports **Swift 6 Strict Concurrency Checking**
- Designed to be **lightweight**, simple and an easier-to-use refactoring of the `StoreHelper` package
- Detailed **documentation** and an example project
- Supports **transaction validation**, **pending ("ask to buy") transactions**, **cancelled** and **failed** transactions
- Support for direct App Store purchases of **promoted in-app purchases** via Purchase Intents
- Supports customer **refunds** and management of **subscriptions** (not yet implemented)
- Provides detailed **transaction information and history** for non-consumables and subscriptions (not yet implemented)
- Support for direct App Store purchases of **promoted in-app purchases** via Purchase Intents
- Supports Xcode 16's "complete" **Strict Concurrency Checking**

## License

Expand Down
Loading

0 comments on commit b379acc

Please sign in to comment.