Skip to content

Commit

Permalink
Paywalls: support fuzzy-Locale search in iOS 15 (#3162)
Browse files Browse the repository at this point in the history
See #2847.

Thanks to @vegaro for pointing this out.

Also removed all checks for Xcode 13 (see #3161 and #3147).
  • Loading branch information
NachoSoto committed Sep 14, 2023
1 parent 3600211 commit c7008c1
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 44 deletions.
2 changes: 1 addition & 1 deletion RevenueCatUI/Helpers/PaywallData+Default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Foundation
import RevenueCat
import SwiftUI

#if canImport(SwiftUI) && swift(>=5.7)
#if canImport(SwiftUI)

@available(iOS 15.0, macOS 12.0, tvOS 15.0, *)
extension PaywallData {
Expand Down
26 changes: 6 additions & 20 deletions Sources/Paywalls/PaywallColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public struct PaywallColor {
/// The original Hex representation for this color.
public var stringRepresentation: String

// `Color` is not `Sendable` in Xcode 13.
#if canImport(SwiftUI) && swift(>=5.7)
#if canImport(SwiftUI)
/// The underlying SwiftUI `Color`.
@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
public var underlyingColor: Color {
Expand All @@ -53,7 +52,7 @@ public struct PaywallColor {

extension PaywallColor {

#if canImport(SwiftUI) && swift(>=5.7)
#if canImport(SwiftUI)

/// Creates a color from a Hex string: `#RRGGBB` or `#RRGGBBAA`.
public init(stringRepresentation: String) throws {
Expand All @@ -77,13 +76,6 @@ extension PaywallColor {

#endif

#else

/// Creates a color from a Hex string: `#RRGGBB` or `#RRGGBBAA`.
public init(stringRepresentation: String) throws {
self.init(stringRepresentation: stringRepresentation, underlyingColor: nil)
}

#endif

}
Expand All @@ -94,12 +86,10 @@ private extension PaywallColor {

#if canImport(SwiftUI)

#if swift(>=5.7)
@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
init(stringRepresentation: String, color: Color) {
self.init(stringRepresentation: stringRepresentation, underlyingColor: color)
}
#endif
@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
init(stringRepresentation: String, color: Color) {
self.init(stringRepresentation: stringRepresentation, underlyingColor: color)
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *)
static func parseColor(_ input: String) throws -> Color {
Expand Down Expand Up @@ -201,17 +191,13 @@ public extension Color {
self.init(light: UIColor(light), dark: UIColor(dark))
}

#if swift(>=5.7)

/// 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)
}

#endif

}
#endif

Expand Down
12 changes: 1 addition & 11 deletions Sources/Paywalls/PaywallData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -451,28 +451,18 @@ extension PaywallData.Configuration.Colors: Sendable {}
extension PaywallData.Configuration.Images: Sendable {}
extension PaywallData.Configuration: Sendable {}

#if swift(>=5.7)
extension PaywallData: Sendable {}
#else
// `@unchecked` because:
// - `URL` is not `Sendable` until Swift 5.7
extension PaywallData: @unchecked Sendable {}
#endif

// MARK: - Extensions

private extension Locale {

func sharesLanguageCode(with other: Locale) -> Bool {
#if swift(>=5.7)
if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) {
return self.language.languageCode == other.language.languageCode
} else {
return false
return self.languageCode == other.languageCode
}
#else
return false
#endif
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,3 @@ extension TestStoreProduct {
}

}

#if swift(<5.7)
// `Locale` isn't `Sendable` in Xcode 13
extension TestStoreProduct: @unchecked Sendable {}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ func checkPaywallColorInformation(_ config: PaywallData.Configuration.ColorInfor
func checkPaywallColor(_ color: PaywallColor) throws {
_ = try PaywallColor(stringRepresentation: "")

#if canImport(UIKit) && !os(watchOS) && swift(>=5.7)
#if canImport(UIKit) && !os(watchOS)
if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) {
_ = PaywallColor(light: color, dark: color)
}
#endif

let _: String = color.debugDescription
let _: String = color.stringRepresentation
#if canImport(SwiftUI) && swift(>=5.7)
#if canImport(SwiftUI)
if #available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6.2, *) {
let _: Color = color.underlyingColor
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTests/Paywalls/PaywallColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//
// Created by Nacho Soto on 7/14/23.

#if canImport(UIKit) && canImport(SwiftUI) && swift(>=5.7)
#if canImport(UIKit) && canImport(SwiftUI)

import Nimble
@testable import RevenueCat
Expand Down
4 changes: 0 additions & 4 deletions Tests/UnitTests/Paywalls/PaywallDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ class PaywallDataTests: BaseHTTPResponseTest {
}

func testFindsLocaleWithOnlyLanguage() throws {
// `Locale.language.languageCode` is iOS 16 only
// and so is RevenueCatUI anyway.
try AvailabilityChecks.iOS16APIAvailableOrSkipTest()

let paywall: PaywallData = try self.decodeFixture("PaywallData-Sample1")

let enConfig = try XCTUnwrap(paywall.config(for: Locale(identifier: "en")))
Expand Down

0 comments on commit c7008c1

Please sign in to comment.