-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paywalls
added individual previews for templates (#2924)
This makes it easier to preview and iterate on each individual template instead of doing that through `PaywallView`. I extracted a lot of the common code into a new `PreviewHelpers` for this.
- Loading branch information
Showing
9 changed files
with
179 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// PreviewHelpers.swift | ||
// | ||
// | ||
// Created by Nacho Soto on 7/29/23. | ||
// | ||
|
||
import RevenueCat | ||
import SwiftUI | ||
|
||
#if DEBUG | ||
|
||
@available(iOS 16.0, macOS 13.0, tvOS 16.0, *) | ||
@available(watchOS, unavailable) | ||
@available(macOS, unavailable) | ||
@available(macCatalyst, unavailable) | ||
@MainActor | ||
enum PreviewHelpers { | ||
|
||
static let introEligibilityChecker: TrialOrIntroEligibilityChecker = | ||
.producing(eligibility: .eligible) | ||
.with(delay: .seconds(0.5)) | ||
static let purchaseHandler: PurchaseHandler = | ||
.mock() | ||
.with(delay: .seconds(0.5)) | ||
|
||
} | ||
|
||
/// Creates an easily previewable `TemplateViewType`. | ||
/// Usage: | ||
/// ```swift | ||
/// PreviewableTemplate( | ||
/// offering: TestData.testOffering | ||
/// ) { | ||
/// PaywallTemplate($0) | ||
/// } | ||
/// ``` | ||
@available(iOS 16.0, macOS 13.0, tvOS 16.0, *) | ||
@available(macOS, unavailable) | ||
struct PreviewableTemplate<T: TemplateViewType>: View { | ||
|
||
typealias Creator = @Sendable @MainActor (TemplateViewConfiguration) -> T | ||
|
||
private let creator: Creator | ||
private let configuration: Result<TemplateViewConfiguration, Error> | ||
|
||
@StateObject | ||
private var introEligibilityViewModel = IntroEligibilityViewModel( | ||
introEligibilityChecker: PreviewHelpers.introEligibilityChecker | ||
) | ||
|
||
init( | ||
offering: Offering, | ||
creator: @escaping Creator | ||
) { | ||
self.configuration = offering.paywall!.configuration( | ||
for: offering, | ||
mode: .fullScreen, | ||
locale: .current | ||
) | ||
self.creator = creator | ||
} | ||
|
||
var body: some View { | ||
switch self.configuration { | ||
case let .success(configuration): | ||
self.creator(configuration) | ||
.environmentObject(self.introEligibilityViewModel) | ||
.environmentObject(PreviewHelpers.purchaseHandler) | ||
.task { | ||
await self.introEligibilityViewModel.computeEligibility( | ||
for: configuration.packages | ||
) | ||
} | ||
|
||
case let .failure(error): | ||
DebugErrorView("Invalid configuration: \(error)", | ||
releaseBehavior: .fatalError) | ||
} | ||
} | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters