Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paywalls: new presentationMode parameter (by @Lascorbe) #3638

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 96 additions & 45 deletions RevenueCatUI/View+PresentPaywall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ import SwiftUI

#if !os(macOS) && !os(tvOS)

/// Presentation options to use with the [presentPaywallIfNeeded](x-source-tag://presentPaywallIfNeeded) View modifiers.
///
/// ### Related Articles
/// [Documentation](https://rev.cat/paywalls)
public enum PaywallPresentationMode {

/// Paywall presented using SwiftUI's `.sheet`.
case sheet

/// Paywall presented using SwiftUI's `.fullScreenCover`.
case fullScreen

}

extension PaywallPresentationMode {

// swiftlint:disable:next missing_docs
public static let `default`: Self = .sheet

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@available(macOS, unavailable, message: "RevenueCatUI does not support macOS yet")
@available(tvOS, unavailable, message: "RevenueCatUI does not support tvOS yet")
Expand All @@ -37,14 +58,18 @@ extension View {
/// - 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`.
/// - Parameter fonts: An optional ``PaywallFontProvider``.
/// - Parameter presentationMode: The desired presentation mode of the paywall. Defaults to `.sheet`.
///
/// ### Related Articles
/// [Documentation](https://rev.cat/paywalls)
///
/// - Tag: presentPaywallIfNeeded
public func presentPaywallIfNeeded(
requiredEntitlementIdentifier: String,
offering: Offering? = nil,
fonts: PaywallFontProvider = DefaultPaywallFontProvider(),
presentationMode: PaywallPresentationMode = .default,
purchaseStarted: PurchaseStartedHandler? = nil,
purchaseCompleted: PurchaseOrRestoreCompletedHandler? = nil,
purchaseCancelled: PurchaseCancelledHandler? = nil,
Expand All @@ -56,6 +81,7 @@ extension View {
return self.presentPaywallIfNeeded(
offering: offering,
fonts: fonts,
presentationMode: presentationMode,
shouldDisplay: { info in
!info.entitlements
.activeInCurrentEnvironment
Expand Down Expand Up @@ -103,10 +129,15 @@ extension View {
/// - 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`.
/// - Parameter fonts: An optional ``PaywallFontProvider``.
/// - Parameter presentationMode: The desired presentation mode of the paywall. Defaults to `.sheet`.
///
/// ### Related Articles
/// [Documentation](https://rev.cat/paywalls)
public func presentPaywallIfNeeded(
offering: Offering? = nil,
fonts: PaywallFontProvider = DefaultPaywallFontProvider(),
presentationMode: PaywallPresentationMode = .default,
shouldDisplay: @escaping @Sendable (CustomerInfo) -> Bool,
purchaseStarted: PurchaseStartedHandler? = nil,
purchaseCompleted: PurchaseOrRestoreCompletedHandler? = nil,
Expand All @@ -119,6 +150,7 @@ extension View {
return self.presentPaywallIfNeeded(
offering: offering,
fonts: fonts,
presentationMode: presentationMode,
shouldDisplay: shouldDisplay,
purchaseStarted: purchaseStarted,
purchaseCompleted: purchaseCompleted,
Expand All @@ -143,6 +175,7 @@ extension View {
fonts: PaywallFontProvider = DefaultPaywallFontProvider(),
introEligibility: TrialOrIntroEligibilityChecker? = nil,
purchaseHandler: PurchaseHandler? = nil,
presentationMode: PaywallPresentationMode = .default,
shouldDisplay: @escaping @Sendable (CustomerInfo) -> Bool,
purchaseStarted: PurchaseStartedHandler? = nil,
purchaseCompleted: PurchaseOrRestoreCompletedHandler? = nil,
Expand All @@ -156,6 +189,7 @@ extension View {
return self
.modifier(PresentingPaywallModifier(
shouldDisplay: shouldDisplay,
presentationMode: presentationMode,
purchaseStarted: purchaseStarted,
purchaseCompleted: purchaseCompleted,
purchaseCancelled: purchaseCancelled,
Expand Down Expand Up @@ -184,6 +218,7 @@ private struct PresentingPaywallModifier: ViewModifier {
}

var shouldDisplay: @Sendable (CustomerInfo) -> Bool
var presentationMode: PaywallPresentationMode
var purchaseStarted: PurchaseStartedHandler?
var purchaseCompleted: PurchaseOrRestoreCompletedHandler?
var purchaseCancelled: PurchaseCancelledHandler?
Expand All @@ -200,6 +235,7 @@ private struct PresentingPaywallModifier: ViewModifier {

init(
shouldDisplay: @escaping @Sendable (CustomerInfo) -> Bool,
presentationMode: PaywallPresentationMode,
purchaseStarted: PurchaseStartedHandler?,
purchaseCompleted: PurchaseOrRestoreCompletedHandler?,
purchaseCancelled: PurchaseCancelledHandler?,
Expand All @@ -214,6 +250,7 @@ private struct PresentingPaywallModifier: ViewModifier {
purchaseHandler: PurchaseHandler?
) {
self.shouldDisplay = shouldDisplay
self.presentationMode = presentationMode
self.purchaseStarted = purchaseStarted
self.purchaseCompleted = purchaseCompleted
self.purchaseCancelled = purchaseCancelled
Expand All @@ -235,55 +272,69 @@ private struct PresentingPaywallModifier: ViewModifier {
private var data: Data?

func body(content: Content) -> some View {
content
.sheet(item: self.$data, onDismiss: self.onDismiss) { data in
PaywallView(
configuration: .init(
content: self.content,
customerInfo: data.customerInfo,
fonts: self.fontProvider,
displayCloseButton: true,
introEligibility: self.introEligibility,
purchaseHandler: self.purchaseHandler
)
)
.onPurchaseStarted {
self.purchaseStarted?()
}
.onPurchaseCompleted {
self.purchaseCompleted?($0)
}
.onPurchaseCancelled {
self.purchaseCancelled?()
}
.onRestoreCompleted { customerInfo in
self.restoreCompleted?(customerInfo)

if !self.shouldDisplay(customerInfo) {
self.close()
Group {
switch presentationMode {
case .sheet:
content
.sheet(item: self.$data, onDismiss: self.onDismiss) { data in
self.paywallView(data)
}
case .fullScreen:
content
.fullScreenCover(item: self.$data, onDismiss: self.onDismiss) { data in
self.paywallView(data)
}
}
.onPurchaseFailure {
self.purchaseFailure?($0)
}
.onRestoreFailure {
self.restoreFailure?($0)
}
.interactiveDismissDisabled(self.purchaseHandler.actionInProgress)
}
.task {
guard let info = try? await self.customerInfoFetcher() else { return }
}
.task {
guard let info = try? await self.customerInfoFetcher() else { return }

Logger.debug(Strings.determining_whether_to_display_paywall)
Logger.debug(Strings.determining_whether_to_display_paywall)

if self.shouldDisplay(info) {
Logger.debug(Strings.displaying_paywall)
if self.shouldDisplay(info) {
Logger.debug(Strings.displaying_paywall)

self.data = .init(customerInfo: info)
} else {
Logger.debug(Strings.not_displaying_paywall)
}
self.data = .init(customerInfo: info)
} else {
Logger.debug(Strings.not_displaying_paywall)
}
}
}

private func paywallView(_ data: Data) -> some View {
PaywallView(
configuration: .init(
content: self.content,
customerInfo: data.customerInfo,
fonts: self.fontProvider,
displayCloseButton: true,
introEligibility: self.introEligibility,
purchaseHandler: self.purchaseHandler
)
)
.onPurchaseStarted {
self.purchaseStarted?()
}
.onPurchaseCompleted {
self.purchaseCompleted?($0)
}
.onPurchaseCancelled {
self.purchaseCancelled?()
}
.onRestoreCompleted { customerInfo in
self.restoreCompleted?(customerInfo)

if !self.shouldDisplay(customerInfo) {
self.close()
}
}
.onPurchaseFailure {
self.purchaseFailure?($0)
}
.onRestoreFailure {
self.restoreFailure?($0)
}
.interactiveDismissDisabled(self.purchaseHandler.actionInProgress)
}

private func close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ struct App: View {
var checkPresentPaywallIfNeeded: some View {
Text("")
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "")
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", presentationMode: .sheet)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", presentationMode: .fullScreen)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", onDismiss: self.paywallDismissed)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", offering: nil)
.presentPaywallIfNeeded(requiredEntitlementIdentifier: "", offering: self.offering)
Expand Down Expand Up @@ -82,6 +84,9 @@ struct App: View {
} purchaseCompleted: {
self.purchaseOrRestoreCompleted($0)
}
.presentPaywallIfNeeded(presentationMode: .sheet) { (_: CustomerInfo) in
false
}
.presentPaywallIfNeeded(fonts: self.fonts) { (_: CustomerInfo) in
false
} purchaseCompleted: {
Expand Down Expand Up @@ -200,6 +205,13 @@ struct App: View {
let _: PaywallFontProvider = CustomPaywallFontProvider(fontName: "Papyrus")
}

private func presentationMode(_ mode: PaywallPresentationMode) {
switch mode {
case .sheet: break
case .fullScreen: break
}
}

}

private struct CustomFontProvider: PaywallFontProvider {
Expand Down