Skip to content

Commit

Permalink
Paywalls: .presentPaywallIfNeeded allows overriding Offering (#…
Browse files Browse the repository at this point in the history
…3370)

This brings parity with `PaywallView()`.

I've also added notes mentioning that this would disable automatic
offering selection for experiments.
  • Loading branch information
NachoSoto authored Nov 1, 2023
1 parent c203198 commit dd03843
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions RevenueCatUI/PaywallView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public struct PaywallView: View {
///
/// - Note: if `offering` does not have a current paywall, or it fails to load due to invalid data,
/// a default paywall will be displayed.
/// - Note: Specifying this parameter means that it will ignore the offering configured in an active experiment.
/// - Warning: `Purchases` must have been configured prior to displaying it.
public init(
offering: Offering,
Expand Down
18 changes: 17 additions & 1 deletion RevenueCatUI/View+PresentPaywall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,25 @@ extension View {
/// ```
/// - Note: If loading the `CustomerInfo` fails (for example, if Internet is offline),
/// the paywall won't be displayed.
///
///
/// - Parameter offering: The `Offering` containing the desired `PaywallData` to display.
/// If `nil` (the default), `Offerings.current` will be used. Note that specifying this parameter means
/// that it will ignore the offering configured in an active experiment.
/// - Parameter fonts: An optional `PaywallFontProvider`.
///
/// ### Related Articles
/// [Documentation](https://rev.cat/paywalls)
public func presentPaywallIfNeeded(
requiredEntitlementIdentifier: String,
offering: Offering? = nil,
fonts: PaywallFontProvider = DefaultPaywallFontProvider(),
purchaseCompleted: PurchaseOrRestoreCompletedHandler? = nil,
restoreCompleted: PurchaseOrRestoreCompletedHandler? = nil,
onDismiss: (() -> Void)? = nil
) -> some View {
return self.presentPaywallIfNeeded(
offering: offering,
fonts: fonts,
shouldDisplay: { info in
!info.entitlements
.activeInCurrentEnvironment
Expand Down Expand Up @@ -73,14 +81,22 @@ extension View {
/// ```
/// - Note: If loading the `CustomerInfo` fails (for example, if Internet is offline),
/// the paywall won't be displayed.
///
/// - Parameter offering: The `Offering` containing the desired `PaywallData` to display.
/// If `nil` (the default), `Offerings.current` will be used. Note that specifying this parameter means
/// that it will ignore the offering configured in an active experiment.
/// - Parameter fonts: An optional `PaywallFontProvider`.
public func presentPaywallIfNeeded(
offering: Offering? = nil,
fonts: PaywallFontProvider = DefaultPaywallFontProvider(),
shouldDisplay: @escaping @Sendable (CustomerInfo) -> Bool,
purchaseCompleted: PurchaseOrRestoreCompletedHandler? = nil,
restoreCompleted: PurchaseOrRestoreCompletedHandler? = nil,
onDismiss: (() -> Void)? = nil
) -> some View {
return self.presentPaywallIfNeeded(
offering: offering,
fonts: fonts,
shouldDisplay: shouldDisplay,
purchaseCompleted: purchaseCompleted,
restoreCompleted: restoreCompleted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ struct App: View {
Text("")
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "")
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", onDismiss: self.paywallDismissed)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", offering: nil)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", offering: self.offering)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", fonts: self.fonts)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", offering: self.offering, fonts: self.fonts)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "",
purchaseCompleted: self.purchaseOrRestoreCompleted)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "",
Expand All @@ -57,7 +60,14 @@ struct App: View {
purchaseCompleted: self.purchaseOrRestoreCompleted,
restoreCompleted: self.purchaseOrRestoreCompleted,
onDismiss: self.paywallDismissed)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", offering: self.offering, fonts: self.fonts,
purchaseCompleted: self.purchaseOrRestoreCompleted,
restoreCompleted: self.purchaseOrRestoreCompleted,
onDismiss: self.paywallDismissed)
.presentPaywallIfNeeded(offering: nil) { (_: CustomerInfo) in false }
.presentPaywallIfNeeded(offering: self.offering) { (_: CustomerInfo) in false }
.presentPaywallIfNeeded(fonts: self.fonts) { (_: CustomerInfo) in false }
.presentPaywallIfNeeded(offering: self.offering, fonts: self.fonts) { (_: CustomerInfo) in false }
.presentPaywallIfNeeded(fonts: self.fonts) { (_: CustomerInfo) in
false
} purchaseCompleted: {
Expand All @@ -79,6 +89,15 @@ struct App: View {
} onDismiss: {
self.paywallDismissed()
}
.presentPaywallIfNeeded(offering: self.offering, fonts: self.fonts) { (_: CustomerInfo) in
false
} purchaseCompleted: {
self.purchaseOrRestoreCompleted($0)
} restoreCompleted: {
self.purchaseOrRestoreCompleted($0)
} onDismiss: {
self.paywallDismissed()
}
}

@ViewBuilder
Expand Down

0 comments on commit dd03843

Please sign in to comment.