Skip to content

Commit

Permalink
Paywalls: add displayCloseButton to PaywallViewController
Browse files Browse the repository at this point in the history
See #3359.
This brings support for that parameter to the `UIKit` type.
  • Loading branch information
NachoSoto committed Nov 7, 2023
1 parent 1fc7dc9 commit e9e5229
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
18 changes: 14 additions & 4 deletions RevenueCatUI/UIKit/PaywallViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ public final class PaywallViewController: UIViewController {
public weak var delegate: PaywallViewControllerDelegate?

private let offering: Offering?

// swiftlint:disable:next missing_docs
public init(offering: Offering? = nil) {
private let displayCloseButton: Bool

/// Initialize a `PaywallViewController` with an optional `Offering`.
/// - Parameter offering: The `Offering` containing the desired `PaywallData` to display.
/// `Offerings.current` will be used by default.
/// - Parameter displayCloseButton: Set this to `true` to automatically include a close button.
public init(
offering: Offering? = nil,
displayCloseButton: Bool = false
) {
self.offering = offering
self.displayCloseButton = displayCloseButton

super.init(nibName: nil, bundle: nil)
}
Expand All @@ -42,7 +50,9 @@ public final class PaywallViewController: UIViewController {
}

private lazy var hostingController: UIHostingController<AnyView> = {
let paywallView = self.offering.map { PaywallView(offering: $0) } ?? PaywallView()
let paywallView = self.offering
.map { PaywallView(offering: $0, displayCloseButton: self.displayCloseButton) }
?? PaywallView(displayCloseButton: self.displayCloseButton)

let view = paywallView
.onPurchaseCompleted { [weak self] transaction, customerInfo in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func paywallViewControllerAPI(_ delegate: Delegate, _ offering: Offering?) {
controller.delegate = delegate

let _: UIViewController = PaywallViewController(offering: offering)
let _: UIViewController = PaywallViewController(displayCloseButton: true)
let _: UIViewController = PaywallViewController(offering: offering, displayCloseButton: true)
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ struct AppContentView: View {
ProminentButton(title: "Present default paywall") {
self.showingDefaultPaywall.toggle()
}

ProminentButton(title: "Present PaywallViewController") {
self.presentPaywallViewController()
}
}
.padding(.horizontal)
.padding(.bottom, 80)
Expand Down Expand Up @@ -150,6 +154,17 @@ struct AppContentView: View {
}
}

private func presentPaywallViewController() {
let paywall = PaywallViewController(displayCloseButton: true)
paywall.modalPresentationStyle = .pageSheet

UIApplication.shared
.currentWindowScene?
.keyWindow?
.rootViewController?
.present(paywall, animated: true)
}

}

private struct ProminentButton: View {
Expand Down Expand Up @@ -198,6 +213,23 @@ extension CustomerInfo {

}

private extension UIApplication {

@available(iOS 13.0, macCatalyst 13.1, *)
@available(macOS, unavailable)
@available(watchOS, unavailable)
@available(tvOS, unavailable)
@MainActor
var currentWindowScene: UIWindowScene? {
return self
.connectedScenes
.filter { $0.activationState == .foregroundActive }
.first as? UIWindowScene
}

}


#if DEBUG

@testable import RevenueCatUI
Expand Down

0 comments on commit e9e5229

Please sign in to comment.