Files
purchases-ios/RevenueCatUI/Data/PaywallViewConfiguration.swift
T
Antonio Pallares 24ada6079b Add Internal support for draft paywall previews (#4761)
* feat: add support for presenting draft paywalls under `@_spi(Internal)` attribute

* fix: initialize `useDraftPaywall` property of `PaywallView`

* feat: have `PaywallView` actually show draft paywall when `useDraftPaywall` is `true`

* remove lint warning

* silence linter warning

* tests: add unit tests for `paywallComponents` and `draftPaywallComponents` in `OfferingsResponse`

* tests: move test offerings with v2 paywalls into a new file not to affect existing decode + encode test

* remove unnecessary property wrapper

* silence lint warning
2025-02-13 18:37:04 +01:00

109 lines
3.0 KiB
Swift

//
// PaywallViewConfiguration.swift
//
//
// Created by Nacho Soto on 1/19/24.
//
import Foundation
import RevenueCat
/// Parameters needed to configure a ``PaywallView``.
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
// @PublicForExternalTesting
struct PaywallViewConfiguration {
var content: Content
var customerInfo: CustomerInfo?
var mode: PaywallViewMode
var fonts: PaywallFontProvider
var displayCloseButton: Bool
let useDraftPaywall: Bool
var introEligibility: TrialOrIntroEligibilityChecker?
var purchaseHandler: PurchaseHandler
var locale: Locale
init(
content: Content,
customerInfo: CustomerInfo? = nil,
mode: PaywallViewMode = .default,
fonts: PaywallFontProvider = DefaultPaywallFontProvider(),
displayCloseButton: Bool = false,
useDraftPaywall: Bool = false,
introEligibility: TrialOrIntroEligibilityChecker? = nil,
purchaseHandler: PurchaseHandler,
locale: Locale = .current
) {
self.content = content
self.customerInfo = customerInfo
self.mode = mode
self.fonts = fonts
self.displayCloseButton = displayCloseButton
self.useDraftPaywall = useDraftPaywall
self.introEligibility = introEligibility
self.purchaseHandler = purchaseHandler
self.locale = locale
}
}
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension PaywallViewConfiguration {
/// Offering selection for the paywall.
// @PublicForExternalTesting
enum Content {
case defaultOffering
case offering(Offering)
case offeringIdentifier(String)
}
}
// MARK: -
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension PaywallViewConfiguration {
// @PublicForExternalTesting
init(
offering: Offering? = nil,
customerInfo: CustomerInfo? = nil,
mode: PaywallViewMode = .default,
fonts: PaywallFontProvider = DefaultPaywallFontProvider(),
displayCloseButton: Bool = false,
useDraftPaywall: Bool = false,
introEligibility: TrialOrIntroEligibilityChecker? = nil,
purchaseHandler: PurchaseHandler = PurchaseHandler.default(),
locale: Locale = .current
) {
let handler = purchaseHandler
self.init(
content: .optionalOffering(offering),
customerInfo: customerInfo,
mode: mode,
fonts: fonts,
displayCloseButton: displayCloseButton,
useDraftPaywall: useDraftPaywall,
introEligibility: introEligibility,
purchaseHandler: handler,
locale: locale
)
}
}
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension PaywallViewConfiguration.Content {
/// - Returns: `Content.offering` or `Content.defaultOffering` if `nil`.
static func optionalOffering(_ offering: Offering?) -> Self {
return offering.map(Self.offering) ?? .defaultOffering
}
}