Skip to content

Commit

Permalink
Validate icons
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Aug 16, 2023
1 parent faab280 commit ef96c85
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
31 changes: 31 additions & 0 deletions RevenueCatUI/Data/PaywallData+Validation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extension Offering {
case missingPaywall
case invalidTemplate(String)
case invalidVariables(Set<String>)
case invalidIcons(Set<String>)

}

Expand Down Expand Up @@ -72,6 +73,11 @@ private extension PaywallData {
return .failure(.invalidTemplate(self.templateName))
}

let invalidIcons = self.localizedConfiguration.validateIcons()
guard invalidIcons.isEmpty else {
return .failure(.invalidIcons(invalidIcons))
}

return .success(template)
}

Expand All @@ -92,6 +98,28 @@ private extension PaywallData {

}

private extension PaywallData.LocalizedConfiguration {

/// - Returns: the set of invalid icons
func validateIcons() -> Set<String> {
return Set(self.features.compactMap { $0.validateIcon() })
}

}

private extension PaywallData.LocalizedConfiguration.Feature {

/// - Returns: the icon ID if it's not recognized
func validateIcon() -> String? {
guard let iconID = self.iconID else { return nil }

return PaywallIcon(rawValue: iconID) == nil
? iconID
: nil
}

}

// MARK: - Errors

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
Expand All @@ -107,6 +135,9 @@ extension Offering.PaywallValidationError: CustomStringConvertible {

case let .invalidVariables(names):
return "Found unrecognized variables: \(names.joined(separator: ", "))."

case let .invalidIcons(names):
return "Found unrecognized icons: \(names.joined(separator: ", "))."
}
}

Expand Down
20 changes: 19 additions & 1 deletion Tests/RevenueCatUITests/Data/PaywallDataValidationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class PaywallDataValidationTests: TestCase {
var localization = try XCTUnwrap(originalOffering.paywall?.localizedConfiguration)
localization.features = [
.init(title: "{{ future_variable }}", content: "{{ new_variable }}"),
.init(title: "{{ another_one }}"),
.init(title: "{{ another_one }}")
]

let offering = originalOffering.with(localization: localization)
Expand All @@ -77,6 +77,24 @@ class PaywallDataValidationTests: TestCase {

expect(result.error) == .invalidVariables(["future_variable", "new_variable", "another_one"])
}

func testUnrecognizedIconsGeneratesDefaultPaywall() throws {
let originalOffering = TestData.offeringWithMultiPackagePaywall
var localization = try XCTUnwrap(originalOffering.paywall?.localizedConfiguration)
localization.features = [
.init(title: "Title 1", content: "Content 1", iconID: "unrecognized_icon_1"),
.init(title: "Title 2", content: "Content 2", iconID: "unrecognized_icon_2")
]

let offering = originalOffering.with(localization: localization)
let result = offering.validatedPaywall()

Self.verifyPackages(in: result.displayablePaywall, match: originalOffering.paywall)
Self.snapshot(result.displayablePaywall)

expect(result.error) == .invalidIcons(["unrecognized_icon_1", "unrecognized_icon_2"])
}

}

// MARK: -
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"asset_base_url" : "https://assets.pawwalls.com",
"config" : {
"blurred_background_image" : true,
"colors" : {
"light" : {
"accent1" : "#FFFFFF",
"accent2" : "#FFFFFF",
"background" : "#FFFFFF",
"call_to_action_background" : "#FFFFFF",
"call_to_action_foreground" : "#FFFFFF",
"text1" : "#FFFFFF"
}
},
"display_restore_purchases" : true,
"images" : {
"background" : "background.jpg",
"icon" : "revenuecatui_default_paywall_app_icon"
},
"packages" : [
"$rc_annual",
"$rc_monthly"
],
"privacy_url" : null,
"tos_url" : null
},
"localized_strings" : {
"en_US" : {
"call_to_action" : "Continue",
"call_to_action_with_intro_offer" : null,
"features" : [

],
"offer_details" : "{{ total_price_and_per_month }}",
"offer_details_with_intro_offer" : "Start your {{ sub_offer_duration }} trial, then {{ total_price_and_per_month }}.",
"title" : "{{ app_name }}"
}
},
"template_name" : "2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"asset_base_url" : "https://assets.pawwalls.com",
"config" : {
"blurred_background_image" : true,
"colors" : {
"light" : {
"accent1" : "#FFFFFF",
"accent2" : "#FFFFFF",
"background" : "#FFFFFF",
"call_to_action_background" : "#FFFFFF",
"call_to_action_foreground" : "#FFFFFF",
"text1" : "#FFFFFF"
}
},
"display_restore_purchases" : true,
"images" : {
"background" : "background.jpg",
"icon" : "revenuecatui_default_paywall_app_icon"
},
"packages" : [
"$rc_annual",
"$rc_monthly"
],
"privacy_url" : null,
"tos_url" : null
},
"localized_strings" : {
"en_US" : {
"call_to_action" : "Continue",
"call_to_action_with_intro_offer" : null,
"features" : [

],
"offer_details" : "{{ total_price_and_per_month }}",
"offer_details_with_intro_offer" : "Start your {{ sub_offer_duration }} trial, then {{ total_price_and_per_month }}.",
"title" : "{{ app_name }}"
}
},
"template_name" : "2"
}

0 comments on commit ef96c85

Please sign in to comment.