-
Notifications
You must be signed in to change notification settings - Fork 339
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] Add support for gradient backgrounds #4522
Merged
+402
−11
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc995e9
[Paywalls] Add support for gradient backgrounds
MarkVillacampa 1c69759
Rename gradientType -> gradientStyle
MarkVillacampa 37512dc
simplify case patterns
MarkVillacampa 0f22164
simplify case
MarkVillacampa 7cd855e
simplify case
MarkVillacampa 94f41f1
cleaning
MarkVillacampa 571c847
[Paywalls] Add support for gradients as text foreground style, and su…
MarkVillacampa 09528c6
Add GradientView preview
MarkVillacampa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -20,19 +20,21 @@ enum BackgroundStyle { | |
|
||
case color(PaywallComponent.ColorScheme) | ||
case image(PaywallComponent.ThemeImageUrls) | ||
case gradient | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following the spec, gradient is not a different type of background but is encoded as part of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad 😅 Thanks for fixing! |
||
|
||
} | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
struct BackgroundStyleModifier: ViewModifier { | ||
|
||
@Environment(\.colorScheme) | ||
var colorScheme | ||
|
||
var backgroundStyle: BackgroundStyle? | ||
|
||
func body(content: Content) -> some View { | ||
if let backgroundStyle { | ||
content | ||
.apply(backgroundStyle: backgroundStyle) | ||
.apply(backgroundStyle: backgroundStyle, colorScheme: colorScheme) | ||
} else { | ||
content | ||
} | ||
|
@@ -44,10 +46,29 @@ struct BackgroundStyleModifier: ViewModifier { | |
fileprivate extension View { | ||
|
||
@ViewBuilder | ||
func apply(backgroundStyle: BackgroundStyle) -> some View { | ||
func apply(backgroundStyle: BackgroundStyle, colorScheme: ColorScheme) -> some View { | ||
switch backgroundStyle { | ||
case .color(let color): | ||
self.background(color.toDynamicColor()) | ||
switch color.effectiveColor(for: colorScheme) { | ||
case .hex, .alias: | ||
self.background(color.toDynamicColor()) | ||
case .linear(let degrees, _): | ||
self.background { | ||
GradientView( | ||
lightGradient: color.light.toGradient(), | ||
darkGradient: color.dark?.toGradient(), | ||
gradientStyle: .linear(degrees) | ||
) | ||
} | ||
case .radial: | ||
self.background { | ||
GradientView( | ||
lightGradient: color.light.toGradient(), | ||
darkGradient: color.dark?.toGradient(), | ||
gradientStyle: .radial | ||
) | ||
} | ||
} | ||
case .image(let imageInfo): | ||
self.background { | ||
RemoteImage( | ||
|
@@ -62,10 +83,6 @@ fileprivate extension View { | |
.ignoresSafeArea() | ||
} | ||
} | ||
case .gradient: | ||
self.background { | ||
// WIP: Add you gradient | ||
} | ||
} | ||
} | ||
|
||
|
@@ -183,6 +200,81 @@ struct BackgrounDStyle_Previews: PreviewProvider { | |
.preferredColorScheme(.dark) | ||
.previewLayout(.sizeThatFits) | ||
.previewDisplayName("Image - Dark (should be japan cats)") | ||
|
||
testContent | ||
.backgroundStyle( | ||
BackgroundStyle.color( | ||
PaywallComponent.ColorScheme.init( | ||
light: .linear(30, [ | ||
.init(color: "#000000", percent: 0), | ||
.init(color: "#ffffff", percent: 100) | ||
]), | ||
dark: .linear(30, [ | ||
.init(color: "#ff0000", percent: 0), | ||
.init(color: "#E58984", percent: 100) | ||
]) | ||
) | ||
) | ||
) | ||
.preferredColorScheme(.dark) | ||
.previewLayout(.sizeThatFits) | ||
.previewDisplayName("Linear Gradient - Dark (should be red)") | ||
|
||
testContent | ||
.backgroundStyle( | ||
BackgroundStyle.color( | ||
PaywallComponent.ColorScheme.init( | ||
light: .linear(30, [ | ||
.init(color: "#000000", percent: 0), | ||
.init(color: "#ffffff", percent: 100) | ||
]), | ||
dark: .linear(30, [ | ||
.init(color: "#00E519", percent: 0), | ||
.init(color: "#9DEAD3", percent: 100) | ||
]) | ||
) | ||
) | ||
) | ||
.previewLayout(.sizeThatFits) | ||
.previewDisplayName("Linear Gradient - Light (should be green") | ||
|
||
testContent | ||
.backgroundStyle( | ||
BackgroundStyle.color( | ||
PaywallComponent.ColorScheme.init( | ||
light: .radial([ | ||
.init(color: "#000000", percent: 0), | ||
.init(color: "#ffffff", percent: 100) | ||
]), | ||
dark: .radial([ | ||
.init(color: "#ff0000", percent: 0), | ||
.init(color: "#E58984", percent: 100) | ||
]) | ||
) | ||
) | ||
) | ||
.preferredColorScheme(.dark) | ||
.previewLayout(.sizeThatFits) | ||
.previewDisplayName("Radial Gradient - Dark (should be red)") | ||
|
||
testContent | ||
.backgroundStyle( | ||
BackgroundStyle.color( | ||
PaywallComponent.ColorScheme.init( | ||
light: .radial([ | ||
|
||
.init(color: "#00E519", percent: 0), | ||
.init(color: "#9DEAD3", percent: 100) | ||
]), | ||
dark: .radial([ | ||
.init(color: "#000000", percent: 0), | ||
.init(color: "#ffffff", percent: 100) | ||
]) | ||
) | ||
) | ||
) | ||
.previewLayout(.sizeThatFits) | ||
.previewDisplayName("Radial Gradient - Light (should be green") | ||
} | ||
} | ||
|
||
|
73 changes: 73 additions & 0 deletions
73
RevenueCatUI/Templates/V2/ViewHelpers/ForegroundColorScheme.swift
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,73 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// ForegroundColorScheme.swift | ||
// | ||
// Created by MarkVillacampa on 27/11/24. | ||
|
||
import RevenueCat | ||
import SwiftUI | ||
|
||
#if PAYWALL_COMPONENTS | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
struct ForegroundColorSchemeModifier: ViewModifier { | ||
|
||
@Environment(\.colorScheme) | ||
var colorScheme | ||
|
||
var foregroundColorScheme: PaywallComponent.ColorScheme | ||
|
||
func body(content: Content) -> some View { | ||
content.foregroundColorScheme(foregroundColorScheme, colorScheme: colorScheme) | ||
} | ||
|
||
} | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
extension View { | ||
func foregroundColorScheme(_ colorScheme: PaywallComponent.ColorScheme) -> some View { | ||
self.modifier(ForegroundColorSchemeModifier(foregroundColorScheme: colorScheme)) | ||
} | ||
} | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
fileprivate extension View { | ||
@ViewBuilder | ||
func foregroundColorScheme(_ color: PaywallComponent.ColorScheme, colorScheme: ColorScheme) -> some View { | ||
switch color.effectiveColor(for: colorScheme) { | ||
case .hex, .alias: | ||
self.foregroundColor(color.toDynamicColor()) | ||
case .linear(let degrees, _): | ||
self.overlay { | ||
GradientView( | ||
lightGradient: color.light.toGradient(), | ||
darkGradient: color.dark?.toGradient(), | ||
gradientStyle: .linear(degrees) | ||
) | ||
.mask( | ||
self | ||
) | ||
} | ||
case .radial: | ||
self.overlay { | ||
GradientView( | ||
lightGradient: color.light.toGradient(), | ||
darkGradient: color.dark?.toGradient(), | ||
gradientStyle: .radial | ||
) | ||
.mask( | ||
self | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Storing the
ColorScheme
instead of the dynamic color now because it can potentially be a gradient.