Skip to content

Commit

Permalink
Paywalls: changed SamplePaywallsList to work offline
Browse files Browse the repository at this point in the history
It contains fake paywalls, so it makes sense that it also works offline by using `TestStoreProduct` instead of production products.
I also changed `SimpleApp` to a tab bar so the 3 pages are more prominent.
  • Loading branch information
NachoSoto committed Aug 2, 2023
1 parent c845031 commit 93ba145
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 65 deletions.
96 changes: 82 additions & 14 deletions Tests/TestingApps/SimpleApp/SimpleApp/SamplePaywalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@ final class SamplePaywallLoader {

private let packages: [Package]

init(packages: [Package]) {
self.packages = packages
}

static func create() async throws -> Self {
struct PurchasesNotConfigured: Error {}

guard Purchases.isConfigured else {
throw PurchasesNotConfigured()
}

let packages = try await Purchases.shared.offerings().current?.availablePackages

return .init(packages: packages ?? [])
init() {
self.packages = [
Self.weeklyPackage,
Self.monthlyPackage,
Self.annualPackage
]
}

func offering(for template: PaywallTemplate) -> Offering {
Expand Down Expand Up @@ -61,6 +53,82 @@ final class SamplePaywallLoader {

}

// MARK: - Packages

private extension SamplePaywallLoader {

static let weeklyPackage = Package(
identifier: "weekly",
packageType: .weekly,
storeProduct: weeklyProduct.toStoreProduct(),
offeringIdentifier: offeringIdentifier
)
static let monthlyPackage = Package(
identifier: "monthly",
packageType: .monthly,
storeProduct: monthlyProduct.toStoreProduct(),
offeringIdentifier: offeringIdentifier
)
static let annualPackage = Package(
identifier: "annual",
packageType: .annual,
storeProduct: annualProduct.toStoreProduct(),
offeringIdentifier: offeringIdentifier
)

static let weeklyProduct = TestStoreProduct(
localizedTitle: "Weekly",
price: 1.99,
localizedPriceString: "$1.99",
productIdentifier: "com.revenuecat.product_1",
productType: .autoRenewableSubscription,
localizedDescription: "PRO weekly",
subscriptionGroupIdentifier: "group",
subscriptionPeriod: .init(value: 1, unit: .week)
)
static let monthlyProduct = TestStoreProduct(
localizedTitle: "Monthly",
price: 12.99,
localizedPriceString: "$12.99",
productIdentifier: "com.revenuecat.product_2",
productType: .autoRenewableSubscription,
localizedDescription: "PRO monthly",
subscriptionGroupIdentifier: "group",
subscriptionPeriod: .init(value: 1, unit: .month),
introductoryDiscount: .init(
identifier: "intro",
price: 0,
localizedPriceString: "$0.00",
paymentMode: .freeTrial,
subscriptionPeriod: .init(value: 1, unit: .week),
numberOfPeriods: 1,
type: .introductory
)
)
static let annualProduct = TestStoreProduct(
localizedTitle: "Annual",
price: 69.49,
localizedPriceString: "$69.49",
productIdentifier: "com.revenuecat.product_3",
productType: .autoRenewableSubscription,
localizedDescription: "PRO annual",
subscriptionGroupIdentifier: "group",
subscriptionPeriod: .init(value: 1, unit: .year),
introductoryDiscount: .init(
identifier: "intro",
price: 0,
localizedPriceString: "$0.00",
paymentMode: .freeTrial,
subscriptionPeriod: .init(value: 14, unit: .day),
numberOfPeriods: 1,
type: .introductory
)
)

}

// MARK: - Paywalls

private extension SamplePaywallLoader {

static func onePackageStandardTemplate() -> PaywallData {
Expand Down
1 change: 1 addition & 0 deletions Tests/TestingApps/SimpleApp/SimpleApp/SimpleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct SimpleApp: App {
.overlay {
DebugView()
.frame(maxHeight: .infinity, alignment: .bottom)
.offset(y: -50)
}
#endif
}
Expand Down
36 changes: 17 additions & 19 deletions Tests/TestingApps/SimpleApp/SimpleApp/Views/AppContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,24 @@ struct AppContentView: View {
private var didPurchase: Bool = false

var body: some View {
ZStack {
self.background
TabView {
ZStack {
self.background
self.content
}
.tabItem {
Label("App", systemImage: "iphone")
}

SamplePaywallsList()
.tabItem {
Label("Examples", systemImage: "pawprint")
}

self.content
OfferingsList()
.tabItem {
Label("All paywalls", systemImage: "network")
}
}
.presentPaywallIfNecessary {
!$0.hasPro
Expand All @@ -64,22 +78,6 @@ struct AppContentView: View {
if self.didPurchase {
Text("Thanks for purchasing!")
}

NavigationLink {
SamplePaywallsList()
} label: {
Text("Sample paywalls")
}
.buttonStyle(.borderedProminent)
.tint(.mint)

NavigationLink {
OfferingsList()
} label: {
Text("All offerings")
}
.buttonStyle(.borderedProminent)
.tint(.indigo)

Spacer()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,20 @@ import SwiftUI

struct SamplePaywallsList: View {

@State
private var loader: Result<SamplePaywallLoader, NSError>?

@State
private var display: Display?

var body: some View {
self.content
.navigationTitle("Paywalls")
.task {
do {
self.loader = .success(try await .create())
} catch let error as NSError {
self.loader = .failure(error)
self.list(with: Self.loader)
.sheet(item: self.$display) { display in
switch display {
case let .template(template):
PaywallView(offering: Self.loader.offering(for: template))
case .defaultTemplate:
PaywallView(offering: Self.loader.offeringWithDefaultPaywall())
}
}
}

@ViewBuilder
private var content: some View {
switch self.loader {
case let .success(loader):
self.list(with: loader)
.sheet(item: self.$display) { display in
switch display {
case let .template(template):
PaywallView(offering: loader.offering(for: template))
case .defaultTemplate:
PaywallView(offering: loader.offeringWithDefaultPaywall())
}
}

case let .failure(error):
Text(error.description)

case .none:
ProgressView()
}
.navigationTitle("Paywalls")
}

private func list(with loader: SamplePaywallLoader) -> some View {
Expand All @@ -75,6 +51,8 @@ struct SamplePaywallsList: View {
.buttonStyle(.plain)
}

private static let loader: SamplePaywallLoader = .init()

}

private struct TemplateLabel: View {
Expand Down

0 comments on commit 93ba145

Please sign in to comment.