Skip to content

Commit

Permalink
Paywalls: added new defaultPackage configuration (#2871)
Browse files Browse the repository at this point in the history
It's optional, so it defaults to the first package if not set.
  • Loading branch information
NachoSoto committed Aug 9, 2023
1 parent 110fa6b commit cbd3073
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 7 deletions.
27 changes: 22 additions & 5 deletions RevenueCatUI/Data/TemplateViewConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension TemplateViewConfiguration {
enum PackageConfiguration: Equatable {

case single(Package)
case multiple(first: Package, all: [Package])
case multiple(first: Package, default: Package, all: [Package])

}

Expand All @@ -62,21 +62,31 @@ extension TemplateViewConfiguration.PackageConfiguration {
switch self {
case let .single(package):
return package
case let .multiple(first, _):
case let .multiple(first, _, _):
return first
}
}

/// Returns all packages, useful for templates that expect multiple packages
/// Returns all packages, useful for templates that expect multiple packages.
var all: [TemplateViewConfiguration.Package] {
switch self {
case let .single(package):
return [package]
case let .multiple(_, packages):
case let .multiple(_, _, packages):
return packages
}
}

/// Returns the package to be selected by default.
var `default`: TemplateViewConfiguration.Package {
switch self {
case let .single(package):
return package
case let .multiple(_, defaultPackage, _):
return defaultPackage
}
}

}

// MARK: - Creation
Expand All @@ -89,6 +99,7 @@ extension TemplateViewConfiguration.PackageConfiguration {
static func create(
with packages: [RevenueCat.Package],
filter: [PackageType],
default: PackageType?,
localization: PaywallData.LocalizedConfiguration,
setting: TemplateViewConfiguration.PackageSetting,
locale: Locale = .current
Expand All @@ -113,7 +124,13 @@ extension TemplateViewConfiguration.PackageConfiguration {
case .single:
return .single(firstPackage)
case .multiple:
return .multiple(first: firstPackage, all: filtered)
let defaultPackage = filtered
.first { $0.content.packageType == `default` }
?? firstPackage

return .multiple(first: firstPackage,
default: defaultPackage,
all: filtered)
}
}

Expand Down
2 changes: 1 addition & 1 deletion RevenueCatUI/Templates/MultiPackageTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private struct MultiPackageTemplateContent: View {
private var dismiss

init(configuration: TemplateViewConfiguration, introEligibility: [Package: IntroEligibilityStatus]) {
self._selectedPackage = .init(initialValue: configuration.packages.single.content)
self._selectedPackage = .init(initialValue: configuration.packages.default.content)

self.configuration = configuration
self.introEligibility = introEligibility
Expand Down
1 change: 1 addition & 0 deletions RevenueCatUI/Templates/TemplateViewType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extension PaywallData {
mode: mode,
packages: try .create(with: offering.availablePackages,
filter: self.config.packages,
default: self.config.defaultPackage,
localization: self.localizedConfiguration,
setting: self.template.packageSetting,
locale: locale),
Expand Down
8 changes: 8 additions & 0 deletions Sources/Paywalls/PaywallData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//
// Created by Nacho Soto on 7/10/23.

// swiftlint:disable file_length

import Foundation

/// The data necessary to display a paywall using the `RevenueCatUI` library.
Expand Down Expand Up @@ -141,6 +143,9 @@ extension PaywallData {
/// The list of package types this paywall will display
public var packages: [PackageType]

/// The package to be selected by default.
public var defaultPackage: PackageType?

/// The names for image assets.
public var imageNames: [String] {
get { self._imageNames }
Expand Down Expand Up @@ -177,6 +182,7 @@ extension PaywallData {
// swiftlint:disable:next missing_docs
public init(
packages: [PackageType],
defaultPackage: PackageType? = nil,
imageNames: [String],
colors: ColorInformation,
blurredBackgroundImage: Bool = false,
Expand All @@ -187,6 +193,7 @@ extension PaywallData {
assert(!imageNames.isEmpty)

self.packages = packages
self.defaultPackage = defaultPackage
self._imageNames = imageNames
self.colors = colors
self._blurredBackgroundImage = blurredBackgroundImage
Expand Down Expand Up @@ -334,6 +341,7 @@ extension PaywallData.Configuration: Codable {

private enum CodingKeys: String, CodingKey {
case packages
case defaultPackage
case _imageNames = "images"
case _blurredBackgroundImage = "blurredBackgroundImage"
case _displayRestorePurchases = "displayRestorePurchases"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ func checkPaywallConfiguration(_ config: PaywallData.Configuration,
imageNames: [""],
colors: colors)
let _: PaywallData.Configuration = .init(packages: [.monthly, .annual],
defaultPackage: .monthly,
imageNames: [""],
colors: colors,
blurredBackgroundImage: true,
displayRestorePurchases: true,
termsOfServiceURL: URL(string: ""),
privacyURL: URL(string: ""))
let _: [PackageType] = config.packages
let _: PackageType? = config.defaultPackage
let _: [String] = config.imageNames
let _: PaywallData.Configuration.ColorInformation = config.colors
let _: Bool = config.blurredBackgroundImage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TemplateViewConfigurationCreationTests: BaseTemplateViewConfigurationTests
try Config.create(
with: [],
filter: [.monthly],
default: nil,
localization: TestData.paywallWithIntroOffer.localizedConfiguration,
setting: .single
)
Expand All @@ -34,6 +35,7 @@ class TemplateViewConfigurationCreationTests: BaseTemplateViewConfigurationTests
try Config.create(
with: [TestData.monthlyPackage],
filter: [],
default: nil,
localization: TestData.paywallWithIntroOffer.localizedConfiguration,
setting: .single
)
Expand All @@ -44,6 +46,7 @@ class TemplateViewConfigurationCreationTests: BaseTemplateViewConfigurationTests
let result = try Config.create(
with: [TestData.monthlyPackage],
filter: [.monthly],
default: nil,
localization: Self.localization,
setting: .single
)
Expand All @@ -61,15 +64,18 @@ class TemplateViewConfigurationCreationTests: BaseTemplateViewConfigurationTests
let result = try Config.create(
with: [TestData.monthlyPackage, TestData.annualPackage, TestData.weeklyPackage],
filter: [.annual, .monthly],
default: .monthly,
localization: Self.localization,
setting: .multiple
)

switch result {
case .single:
fail("Invalid result: \(result)")
case let .multiple(first, packages):
case let .multiple(first, defaultPackage, packages):
expect(first.content) === TestData.annualPackage
expect(defaultPackage.content) === TestData.monthlyPackage

expect(packages).to(haveCount(2))

let annual = packages[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"default_locale": "en_US",
"config": {
"packages": ["$rc_monthly", "$rc_annual"],
"default_package": "$rc_annual",
"images": ["asset_name.png"],
"blurredBackgroundImage": true,
"display_restore_purchases": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class OfferingsDecodingTests: BaseHTTPResponseTest {
try expect(paywall.assetBaseURL) == XCTUnwrap(URL(string: "https://rc-paywalls.s3.amazonaws.com"))

expect(paywall.config.packages) == [.monthly, .annual]
expect(paywall.config.defaultPackage).to(beNil())
expect(paywall.config.imageNames) == ["asset_1", "asset_2"]

let enConfig = try XCTUnwrap(paywall.config(for: Locale(identifier: "en_US")))
Expand Down
1 change: 1 addition & 0 deletions Tests/UnitTests/Paywalls/PaywallDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PaywallDataTests: BaseHTTPResponseTest {
expect(paywall.defaultLocale) == Locale(identifier: Self.defaultLocale)
expect(paywall.assetBaseURL) == URL(string: "https://rc-paywalls.s3.amazonaws.com")!
expect(paywall.config.packages) == [.monthly, .annual]
expect(paywall.config.defaultPackage) == .annual
expect(paywall.config.imageNames) == ["asset_name.png"]
expect(paywall.config.blurredBackgroundImage) == true
expect(paywall.config.displayRestorePurchases) == false
Expand Down

0 comments on commit cbd3073

Please sign in to comment.