Skip to content

Commit

Permalink
New CurrentOfferingSubscriptionStoreView to simplify creating `Subs…
Browse files Browse the repository at this point in the history
…criptionStoreView`

Follow up to #2593.
  • Loading branch information
NachoSoto committed Jun 9, 2023
1 parent 5ba1822 commit b85fc32
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Sources/Support/PaywallExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ extension SubscriptionStoreView {
/// with custom marketing content.
/// When the user purchases products through this paywall, the `RevenueCat` SDK will handle
/// the result automatically. All you need to do is to dismiss the paywall
///
/// - Seealso: ``CurrentOfferingSubscriptionStoreView``
public init(
offering: Offering,
@ViewBuilder marketingContent: () -> (Content)
Expand All @@ -71,6 +73,8 @@ extension SubscriptionStoreView {

/// Creates a ``SubscriptionStoreView`` from an ``Offering``
/// that doesn't take a custom view to use for marketing content.
///
/// - Seealso: ``CurrentOfferingSubscriptionStoreView``
public init(
offering: Offering
) where Content == AutomaticSubscriptionStoreMarketingContent {
Expand All @@ -79,6 +83,55 @@ extension SubscriptionStoreView {

}

/// ``_StoreKit_SwiftUI/SubscriptionStoreView`` that displays subscription products in the current ``Offering``.
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *)
public struct CurrentOfferingSubscriptionStoreView<Content: View>: View {

@State
private var currentOffering: Offering?
private let marketingContent: (() -> Content)?

/// Creates a view to load all subscriptions in a subscription group from the App Store.
///
/// When the user purchases products through this paywall, the `RevenueCat` SDK will handle
/// the result automatically. All you need to do is to dismiss the paywall
public init() where Content == AutomaticSubscriptionStoreMarketingContent {
self.marketingContent = nil
}

/// Creates a view to load all subscriptions in a subscription group from the App Store, and merchandise
/// them with a custom marketing content.
///
/// When the user purchases products through this paywall, the `RevenueCat` SDK will handle
/// the result automatically. All you need to do is to dismiss the paywall
public init(@ViewBuilder marketingContent: @escaping () -> Content) {
self.marketingContent = marketingContent
}

// swiftlint:disable:next missing_docs
public var body: some View {
Group {
if let currentOffering {
if let marketingContent {
SubscriptionStoreView(offering: currentOffering,
marketingContent: marketingContent)
} else {
SubscriptionStoreView(offering: currentOffering)
}
} else {
ProgressView()
.progressViewStyle(.circular)
}
}
.task {
if let offering = try? await Purchases.shared.offerings().current {
self.currentOffering = offering
}
}
}

}

private extension Offering {

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
Expand Down
5 changes: 5 additions & 0 deletions Tests/APITesters/SwiftAPITester/SwiftAPITester/OtherAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ struct PaywallViews: View {
SubscriptionStoreView(offering: self.offering) {
Text("Marketing content")
}

CurrentOfferingSubscriptionStoreView()
CurrentOfferingSubscriptionStoreView {
Text("Marketing content")
}
}

}
Expand Down

0 comments on commit b85fc32

Please sign in to comment.