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

Paywall Tester: improve template 5 dark colors #3358

Merged
merged 5 commits into from
Oct 30, 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
21 changes: 11 additions & 10 deletions RevenueCatUI/Data/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

import Foundation
import RevenueCat
import SwiftUI

// swiftlint:disable type_body_length file_length

#if DEBUG

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *)
internal enum TestData {

static let weeklyProduct = TestStoreProduct(
Expand Down Expand Up @@ -380,15 +381,15 @@ internal enum TestData {
accent3: "#D1D1D1"
),
dark: .init(
background: "#000000",
text1: "#ffffff",
text2: "#adf5c5",
text3: "#b15d5d",
callToActionBackground: "#41E194",
callToActionForeground: "#000000",
accent1: "#41E194",
accent2: "#DFDFDF",
accent3: "#D1D1D1"
background: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).asPaywallColor,
text1: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1).asPaywallColor,
text2: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1).asPaywallColor,
text3: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).asPaywallColor,
callToActionBackground: #colorLiteral(red: 0.1960784346, green: 0.3411764801, blue: 0.1019607857, alpha: 1).asPaywallColor,
callToActionForeground: #colorLiteral(red: 0.5315951397, green: 1, blue: 0.4162791786, alpha: 1).asPaywallColor,
accent1: #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1).asPaywallColor,
accent2: #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1).asPaywallColor,
accent3: #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1).asPaywallColor
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm I know this is only for test data but should we try to use the same style as the light mode colors? Seems weird to declare the colors differently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, but it's a fair amount of work to update all definitions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW this is what it looks like on Xcode:
Screenshot 2023-10-30 at 09 08 14

)
),
termsOfServiceURL: URL(string: "https://revenuecat.com/tos")!
Expand Down
78 changes: 69 additions & 9 deletions Sources/Paywalls/PaywallColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ private extension PaywallColor {
hexNumber |= 0xff
}

red = CGFloat((hexNumber & 0xff000000) >> 24) / 256
green = CGFloat((hexNumber & 0x00ff0000) >> 16) / 256
blue = CGFloat((hexNumber & 0x0000ff00) >> 8) / 256
alpha = CGFloat(hexNumber & 0x000000ff) / 256
red = CGFloat((hexNumber & 0xff000000) >> 24) / 255
green = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
blue = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
alpha = CGFloat(hexNumber & 0x000000ff) / 255

return .init(red: red, green: green, blue: blue, opacity: alpha)
} else {
Expand Down Expand Up @@ -172,7 +172,9 @@ private extension UIColor {
}
#endif

#if canImport(SwiftUI) && canImport(UIKit) && !os(watchOS)
#if canImport(SwiftUI) && canImport(UIKit)

#if !os(watchOS)
@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
private extension Color {

Expand All @@ -186,18 +188,29 @@ private extension Color {
}

}
#endif

@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
public extension Color {

/// Converts a `Color` into a `PaywallColor`.
/// - Warning: This `PaywallColor` won't be able to be encoded,
/// its ``PaywallColor/stringRepresentation`` will be undefined.
var asPaywallColor: PaywallColor {
return .init(stringRepresentation: "#FFFFFF", color: self)
return .init(stringRepresentation: self.stringRepresentation,
color: self)
}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public extension UIColor {

/// Converts a `UIColor` into a `PaywallColor`.
var asPaywallColor: PaywallColor {
return Color(uiColor: self).asPaywallColor
}

}

#endif

// MARK: - Conformances
Expand Down Expand Up @@ -247,3 +260,50 @@ extension PaywallColor: Codable {
}

// swiftlint:enable missing_docs

// MARK: -

#if canImport(SwiftUI) && canImport(UIKit)

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
internal extension Color {

var rgba: (red: Int, green: Int, blue: Int, alpha: Int) {
let color = UIColor(self)

var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
assert(color.getRed(&red, green: &green, blue: &blue, alpha: &alpha))

return (red.rounded, green.rounded, blue.rounded, alpha.rounded)
}

}

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
private extension Color {

/// - Returns: the color converted to `#RRGGBBAA` or `#RRGGBB`.`
var stringRepresentation: String {
let (red, green, blue, alpha) = self.rgba

if alpha < 255 {
return String(format: "#%02X%02X%02X%02X", red, green, blue, alpha)
} else {
return String(format: "#%02X%02X%02X", red, green, blue)
}
}

}

#endif

private extension CGFloat {

var rounded: Int {
return Int((self * 255).rounded())
}

}
13 changes: 13 additions & 0 deletions Tests/APITesters/SwiftAPITester/SwiftAPITester/PaywallAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ func checkPaywallColor(_ color: PaywallColor) throws {
#endif
}

#if canImport(SwiftUI) && canImport(UIKit)

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
func checkConversions(color: Color, uiColor: UIColor) {
let _: PaywallColor = color.asPaywallColor

if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) {
let _: PaywallColor = uiColor.asPaywallColor
}
}

#endif

func checkPaywallViewMode(_ mode: PaywallViewMode) {
switch mode {
case .fullScreen:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"blurred_background_image" : true,
"colors" : {
"light" : {
"accent1" : "#FFFFFF",
"accent2" : "#FFFFFF",
"accent1" : "#007AFF",
"accent2" : "#000000",
"background" : "#FFFFFF",
"call_to_action_background" : "#FFFFFF",
"call_to_action_background" : "#007AFF",
"call_to_action_foreground" : "#FFFFFF",
"text1" : "#FFFFFF"
"text1" : "#000000"
}
},
"display_restore_purchases" : true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"blurred_background_image" : true,
"colors" : {
"light" : {
"accent1" : "#FFFFFF",
"accent2" : "#FFFFFF",
"accent1" : "#007AFF",
"accent2" : "#000000",
"background" : "#FFFFFF",
"call_to_action_background" : "#FFFFFF",
"call_to_action_background" : "#007AFF",
"call_to_action_foreground" : "#FFFFFF",
"text1" : "#FFFFFF"
"text1" : "#000000"
}
},
"display_restore_purchases" : true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"blurred_background_image" : true,
"colors" : {
"light" : {
"accent1" : "#FFFFFF",
"accent2" : "#FFFFFF",
"accent1" : "#007AFF",
"accent2" : "#000000",
"background" : "#FFFFFF",
"call_to_action_background" : "#FFFFFF",
"call_to_action_background" : "#007AFF",
"call_to_action_foreground" : "#FFFFFF",
"text1" : "#FFFFFF"
"text1" : "#000000"
}
},
"display_restore_purchases" : true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"blurred_background_image" : true,
"colors" : {
"light" : {
"accent1" : "#FFFFFF",
"accent2" : "#FFFFFF",
"accent1" : "#007AFF",
"accent2" : "#000000",
"background" : "#FFFFFF",
"call_to_action_background" : "#FFFFFF",
"call_to_action_background" : "#007AFF",
"call_to_action_foreground" : "#FFFFFF",
"text1" : "#FFFFFF"
"text1" : "#000000"
}
},
"display_restore_purchases" : true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"blurred_background_image" : true,
"colors" : {
"light" : {
"accent1" : "#FFFFFF",
"accent2" : "#FFFFFF",
"accent1" : "#007AFF",
"accent2" : "#000000",
"background" : "#FFFFFF",
"call_to_action_background" : "#FFFFFF",
"call_to_action_background" : "#007AFF",
"call_to_action_foreground" : "#FFFFFF",
"text1" : "#FFFFFF"
"text1" : "#000000"
}
},
"display_restore_purchases" : true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"blurred_background_image" : true,
"colors" : {
"light" : {
"accent1" : "#FFFFFF",
"accent2" : "#FFFFFF",
"accent1" : "#007AFF",
"accent2" : "#000000",
"background" : "#FFFFFF",
"call_to_action_background" : "#FFFFFF",
"call_to_action_background" : "#007AFF",
"call_to_action_foreground" : "#FFFFFF",
"text1" : "#FFFFFF"
"text1" : "#000000"
}
},
"display_restore_purchases" : true,
Expand Down
2 changes: 1 addition & 1 deletion Tests/RevenueCatUITests/Helpers/DataExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension PaywallData {
}

/// For snapshot tests to be able to produce a consistent `assetBaseURL`
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *)
var withTestAssetBaseURL: Self {
var copy = self
copy.assetBaseURL = TestData.paywallAssetBaseURL
Expand Down
47 changes: 22 additions & 25 deletions Tests/UnitTests/Paywalls/PaywallColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ final class PaywallColorTests: TestCase {
expect(try JSONDecoder.default.decode(PaywallColor.self, jsonData: "\"ABBCC22\"".asData)).to(throwError())
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
func testOpaqueColorAsPaywallColor() throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

let color = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1).asPaywallColor
let expected = try PaywallColor(stringRepresentation: "#FF0000")

expect(color) == expected
color.verifyComponents(255, 0, 0, 255)
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
func testTranslucentColorAsPaywallColor() throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

let color = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 0.4).asPaywallColor
let expected = try PaywallColor(stringRepresentation: "#FF000066")

expect(color) == expected
color.verifyComponents(255, 0, 0, 102)
}

}

// MARK: -
Expand Down Expand Up @@ -105,29 +127,4 @@ private extension PaywallColor {

}

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
private extension Color {

var rgba: (red: Int, green: Int, blue: Int, alpha: Int) {
let color = UIColor(self)

var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
assert(color.getRed(&red, green: &green, blue: &blue, alpha: &alpha))

return (red.rounded, green.rounded, blue.rounded, alpha.rounded)
}

}

private extension CGFloat {

var rounded: Int {
return Int((self * 256).rounded())
}

}

#endif