Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paywalls: using new color information in template #2823

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions RevenueCatUI/Helpers/ColorInformation+MultiScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,20 @@ extension PaywallData.Configuration.ColorInformation {
}

#endif

#if canImport(SwiftUI)

import SwiftUI

// Helpful acessors
@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
extension PaywallData.Configuration.Colors {

var backgroundColor: Color { self.background.underlyingColor }
var foregroundColor: Color { self.foreground.underlyingColor }
var callToActionBackgroundColor: Color { self.callToActionBackground.underlyingColor }
var callToActionForegroundColor: Color { self.callToActionForeground.underlyingColor }

}

#endif
12 changes: 9 additions & 3 deletions RevenueCatUI/Templates/Example1Template.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ struct Example1Template: TemplateViewType {
init(
packages: [Package],
localization: PaywallData.LocalizedConfiguration,
paywall: PaywallData
paywall: PaywallData,
colors: PaywallData.Configuration.Colors
) {
// Fix-me: move this logic out to be used by all templates
if packages.isEmpty {
Expand All @@ -29,7 +30,8 @@ struct Example1Template: TemplateViewType {
package: package,
localization: localization.processVariables(with: package),
configuration: paywall.config,
headerImageURL: paywall.headerImageURL
headerImageURL: paywall.headerImageURL,
colors: colors
))
} else {
self.data = .failure(.couldNotFindAnyPackages(expectedTypes: allPackages))
Expand Down Expand Up @@ -128,9 +130,11 @@ private struct Example1TemplateContent: View {

self.button
}
.foregroundColor(self.data.colors.foregroundColor)
.multilineTextAlignment(.center)
.padding(.horizontal)
}
.background(self.data.colors.backgroundColor)
}

private var offerDetails: some View {
Expand Down Expand Up @@ -186,11 +190,12 @@ private struct Example1TemplateContent: View {
}
} label: {
self.ctaText
.foregroundColor(self.data.colors.callToActionForegroundColor)
.frame(maxWidth: .infinity)
}
.font(.title2)
.fontWeight(.semibold)
.tint(Color.green.gradient.opacity(0.8))
.tint(self.data.colors.callToActionBackgroundColor.gradient)
.buttonStyle(.borderedProminent)
.buttonBorderShape(.capsule)
.controlSize(.large)
Expand All @@ -210,6 +215,7 @@ private extension Example1TemplateContent {
let localization: ProcessedLocalizedConfiguration
let configuration: PaywallData.Configuration
let headerImageURL: URL
let colors: PaywallData.Configuration.Colors
}

enum Error: Swift.Error {
Expand Down
8 changes: 6 additions & 2 deletions RevenueCatUI/Templates/TemplateViewType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ protocol TemplateViewType: SwiftUI.View {
init(
packages: [Package],
localization: PaywallData.LocalizedConfiguration,
paywall: PaywallData
paywall: PaywallData,
colors: PaywallData.Configuration.Colors
)

}
Expand All @@ -17,12 +18,15 @@ extension PaywallData {

@ViewBuilder
func createView(for offering: Offering) -> some View {
let colors = self.config.colors.multiScheme
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Computing dynamic colors


switch self.template {
case .example1:
Example1Template(
packages: offering.availablePackages,
localization: self.localizedConfiguration,
paywall: self
paywall: self,
colors: colors
)
}
}
Expand Down
16 changes: 11 additions & 5 deletions RevenueCatUI/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal enum TestData {
config: .init(
packages: [.monthly],
headerImageName: Self.paywallHeaderImageName,
colors: .init(light: Self.lightColors)
colors: .init(light: Self.lightColors, dark: Self.darkColors)
),
localization: Self.localization,
assetBaseURL: Self.paywallAssetBaseURL
Expand All @@ -77,7 +77,7 @@ internal enum TestData {
config: .init(
packages: [.annual],
headerImageName: Self.paywallHeaderImageName,
colors: .init(light: Self.lightColors)
colors: .init(light: Self.lightColors, dark: Self.darkColors)
),
localization: Self.localization,
assetBaseURL: Self.paywallAssetBaseURL
Expand All @@ -100,10 +100,16 @@ internal enum TestData {
)

static let lightColors: PaywallData.Configuration.Colors = .init(
background: "#FFFFFF",
foreground: "#000000",
callToActionBackground: "#5CD27A",
callToActionForeground: "#FFFFFF"
)
static let darkColors: PaywallData.Configuration.Colors = .init(
background: "#000000",
foreground: "#FF0000",
callToActionBackground: "#FF0AB1",
callToActionForeground: "#FF0000"
foreground: "#FFFFFF",
callToActionBackground: "#ACD27A",
callToActionForeground: "#000000"
)

static let customerInfo: CustomerInfo = {
Expand Down
13 changes: 13 additions & 0 deletions Tests/RevenueCatUITests/PaywallViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ class PaywallViewTests: TestCase {
view.snapshot(size: Self.size)
}

func testDarkMode() {
let offering = TestData.offeringWithIntroOffer

let view = PaywallView(offering: offering,
paywall: offering.paywallWithLocalImage,
introEligibility: Self.ineligibleChecker,
purchaseHandler: Self.purchaseHandler)

view
.environment(\.colorScheme, .dark)
.snapshot(size: Self.size)
}

private static let eligibleChecker: TrialOrIntroEligibilityChecker = .producing(eligibility: .eligible)
private static let ineligibleChecker: TrialOrIntroEligibilityChecker = .producing(eligibility: .ineligible)
private static let purchaseHandler: PurchaseHandler = .mock()
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.