Skip to content

Commit

Permalink
Added OnSubscriptionChange view modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
russell-archer committed Oct 6, 2024
1 parent 410fa48 commit 8184c76
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Check out the [Quick Start Tutorial](https://russell-archer.github.io/SKHelper/t
---

# Recent Major Changes
- 6 Oct, 2024
- Fixed logging bug with release build; added `onSubscriptionChange(onChange:)` View modifier
- 27 Sep, 2024
- Improved support for consumables
- 23 Sep, 2024
Expand Down
51 changes: 50 additions & 1 deletion Sources/SKHelper/Core/SKHelper+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Russell Archer on 04/08/2024.
//

public import SwiftUI
public import StoreKit

public typealias ProductId = String
Expand Down Expand Up @@ -233,7 +234,7 @@ public extension SKHelper {
/// - Parameter transactionId: The transaction's unique id.
/// - Returns: Returns the `Transaction` associated with the unique id, or nil if the transaction cannot be found or can't be verified.
///
func transaction(for transactionId: UInt64) async -> Transaction? {
func transaction(for transactionId: UInt64) async -> StoreKit.Transaction? {
for await transactionResult in Transaction.all {
if transactionResult.unsafePayloadValue.id != transactionId { continue }

Expand Down Expand Up @@ -364,3 +365,51 @@ public enum SKHelperEntitlementState {
/// Access to the purchase has been revoked by the App Store.
case revoked
}

// MARK: - OnSubscriptionChange

/// The `OnSubscriptionChange` ViewModifier allows you to be notified of changes to the status of all subscriptions.
/// See also the `onSubscriptionChange(onChange:)` View extension.
public struct OnSubscriptionChange: ViewModifier {
/// The `SKHelper` object.
@Environment(SKHelper.self) private var store

/// Optional handler allows you to be notified of changes to the status of all subscriptions.
private var onChange: SubscriptionStatusChangeClosure?

/// Creates an `OnSubscriptionChange` ViewModifier.
/// - Parameter onChange: Optional handler allows you to be notified of changes to the status of all subscriptions.
public init(onChange: SubscriptionStatusChangeClosure? = nil) { self.onChange = onChange }

/// Builds the body of the `OnSubscriptionChange` view modifier.
/// - Parameter content: The View's content.
/// - Returns: Returns the body of the `OnSubscriptionChange` view modifier.
public func body(content: Content) -> some View {
content
.onAppear {
store.subscriptionStatusChange = { productId, transactionId, renewalState, hasExpired in
let newState = renewalState.localizedDescription.lowercased(with: Locale.current)
print("Subscription \(productId) now \"\(newState)\" with transaction id \(transactionId). The subscription has \(hasExpired ? "expired" : "not expired").")

onChange?(productId, transactionId, renewalState, hasExpired)
}
}
}
}

// MARK: - onSubscriptionChange(onChange:) View extension

public extension View {

/// View extension to provide a `onSubscriptionChange(onChange:)` modifier.
/// ```
/// // Example usage:
/// SKHelperSubscriptionStoreView()
/// .onSubscriptionChange() { productId, transactionId, renewalState, hasExpired in
/// print("The status of subscription \(productId) changed to \(renewalState.localizedDescription)")
/// }
/// ```
public func onSubscriptionChange(onChange: SubscriptionStatusChangeClosure? = nil) -> some View {
modifier(OnSubscriptionChange(onChange: onChange))
}
}

0 comments on commit 8184c76

Please sign in to comment.