Skip to content

Commit

Permalink
Paywalls: add support for CTA button gradients (#3121)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto authored Aug 31, 2023
1 parent 40a59c2 commit 29755a2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
3 changes: 3 additions & 0 deletions RevenueCatUI/Helpers/ColorInformation+MultiScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ extension PaywallData.Configuration.Colors {
text2: .init(light: light.text2, dark: dark.text2),
callToActionBackground: .init(light: light.callToActionBackground, dark: dark.callToActionBackground),
callToActionForeground: .init(light: light.callToActionForeground, dark: dark.callToActionForeground),
callToActionSecondaryBackground: .init(light: light.callToActionSecondaryBackground,
dark: dark.callToActionSecondaryBackground),
accent1: .init(light: light.accent1, dark: dark.accent1),
accent2: .init(light: light.accent2, dark: dark.accent2)
)
Expand Down Expand Up @@ -88,6 +90,7 @@ extension PaywallData.Configuration.Colors {
var text2Color: Color { self.text2?.underlyingColor ?? self.text1.underlyingColor }
var callToActionBackgroundColor: Color { self.callToActionBackground.underlyingColor }
var callToActionForegroundColor: Color { self.callToActionForeground.underlyingColor }
var callToActionSecondaryBackgroundColor: Color? { self.callToActionSecondaryBackground?.underlyingColor }
var accent1Color: Color { self.accent1?.underlyingColor ?? self.callToActionForegroundColor }
var accent2Color: Color { self.accent2?.underlyingColor ?? self.accent1Color }

Expand Down
45 changes: 23 additions & 22 deletions RevenueCatUI/Views/PurchaseButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,37 @@ struct PurchaseButton: View {
? .infinity
: nil
)
.padding()
.padding(.vertical, self.userInterfaceIdiom == .pad ? 10 : 0)
}
.font(self.fonts.font(for: self.mode.buttonFont).weight(.semibold))
.tint(self.colors.callToActionBackgroundColor)
.buttonBorderShape(self.mode.buttonBorderShape)
.controlSize(self.mode.buttonSize)
.buttonStyle(.borderedProminent)
.background(self.backgroundView)
.tint(.clear)
.frame(maxWidth: .infinity)
.dynamicTypeSize(...Constants.maximumDynamicTypeSize)
.disabled(self.package.currentlySubscribed)
}

@ViewBuilder
private var backgroundView: some View {
Capsule(style: .continuous)
.foregroundStyle(self.backgroundColor)
}

private var backgroundColor: some ShapeStyle {
let primary = self.colors.callToActionBackgroundColor

if let secondary = self.colors.callToActionSecondaryBackgroundColor {
return AnyShapeStyle(
LinearGradient(colors: [primary, secondary],
startPoint: .top,
endPoint: .bottom)
)
} else {
return AnyShapeStyle(primary)
}
}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, *)
Expand All @@ -112,24 +131,6 @@ private extension PaywallViewMode {
}
}

@available(tvOS, unavailable)
var buttonSize: ControlSize {
switch self {
case .fullScreen, .footer, .condensedFooter: return .large
}
}

var buttonBorderShape: ButtonBorderShape {
switch self {
case .fullScreen, .footer, .condensedFooter:
#if os(macOS) || os(tvOS)
return .roundedRectangle
#else
return .capsule
#endif
}
}

}

// MARK: - Previews
Expand Down
4 changes: 4 additions & 0 deletions Sources/Paywalls/PaywallData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ extension PaywallData.Configuration {
public var callToActionBackground: PaywallColor
/// Foreground color of the main call to action button.
public var callToActionForeground: PaywallColor
/// If present, the CTA will create a vertical gradient from ``callToActionBackground`` to this color.
public var callToActionSecondaryBackground: PaywallColor?
/// Primary accent color.
public var accent1: PaywallColor?
/// Secondary accent color
Expand All @@ -309,6 +311,7 @@ extension PaywallData.Configuration {
text2: PaywallColor? = nil,
callToActionBackground: PaywallColor,
callToActionForeground: PaywallColor,
callToActionSecondaryBackground: PaywallColor? = nil,
accent1: PaywallColor? = nil,
accent2: PaywallColor? = nil
) {
Expand All @@ -317,6 +320,7 @@ extension PaywallData.Configuration {
self.text2 = text2
self.callToActionBackground = callToActionBackground
self.callToActionForeground = callToActionForeground
self.callToActionSecondaryBackground = callToActionSecondaryBackground
self.accent1 = accent1
self.accent2 = accent2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func checkPaywallColors(_ config: PaywallData.Configuration.Colors) {
let text2: PaywallColor? = config.text2
let callToActionBackground: PaywallColor = config.callToActionBackground
let callToActionForeground: PaywallColor = config.callToActionForeground
let callToActionSecondaryBackground: PaywallColor? = config.callToActionSecondaryBackground
let accent1: PaywallColor? = config.accent1
let accent2: PaywallColor? = config.accent2

Expand All @@ -110,6 +111,7 @@ func checkPaywallColors(_ config: PaywallData.Configuration.Colors) {
text2: text2,
callToActionBackground: callToActionBackground,
callToActionForeground: callToActionForeground,
callToActionSecondaryBackground: callToActionSecondaryBackground,
accent1: accent1,
accent2: accent2
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"call_to_action_background": "#FF00AACC",
"call_to_action_foreground": "#FF00AA",
"accent_1": "#FF0000",
"accent_2": "#00FF00"
"accent_2": "#00FF00",
"call_to_action_secondary_background": "#FF00BB",
},
"dark": {
"background": "#FF0000",
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 @@ -39,6 +39,7 @@ class PaywallDataTests: BaseHTTPResponseTest {
expect(paywall.config.colors.light.text2?.stringRepresentation) == "#FF00AA11"
expect(paywall.config.colors.light.callToActionBackground.stringRepresentation) == "#FF00AACC"
expect(paywall.config.colors.light.callToActionForeground.stringRepresentation) == "#FF00AA"
expect(paywall.config.colors.light.callToActionSecondaryBackground?.stringRepresentation) == "#FF00BB"
expect(paywall.config.colors.light.accent1?.stringRepresentation) == "#FF0000"
expect(paywall.config.colors.light.accent2?.stringRepresentation) == "#00FF00"

Expand Down

0 comments on commit 29755a2

Please sign in to comment.