Skip to content

Commit

Permalink
Paywalls: convert empty images into nil (#3195)
Browse files Browse the repository at this point in the history
Ideally the backend would never send `""` for images, but better to be
defensive.
  • Loading branch information
NachoSoto committed Sep 15, 2023
1 parent 66ef49d commit 0bcc509
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 5 deletions.
33 changes: 29 additions & 4 deletions Sources/Paywalls/PaywallData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,29 @@ extension PaywallData.Configuration {
public struct Images {

/// Image displayed as a header in a template.
public var header: String?
public var header: String? {
get { self._header }
set { self._header = newValue }
}

/// Image displayed as a background in a template.
public var background: String?
public var background: String? {
get { self._background }
set { self._background = newValue }
}

/// Image displayed as an app icon in a template.
public var icon: String?
public var icon: String? {
get { self._icon }
set { self._icon = newValue }
}

@NonEmptyStringDecodable
var _header: String?
@NonEmptyStringDecodable
var _background: String?
@NonEmptyStringDecodable
var _icon: String?

// swiftlint:disable:next missing_docs
public init(header: String? = nil, background: String? = nil, icon: String? = nil) {
Expand Down Expand Up @@ -418,7 +434,16 @@ extension PaywallData.LocalizedConfiguration: Codable {

extension PaywallData.Configuration.ColorInformation: Codable {}
extension PaywallData.Configuration.Colors: Codable {}
extension PaywallData.Configuration.Images: Codable {}

extension PaywallData.Configuration.Images: Codable {

private enum CodingKeys: String, CodingKey {
case _header = "header"
case _background = "background"
case _icon = "icon"
}

}

extension PaywallData.Configuration: Codable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
},
"display_restore_purchases" : true,
"images" : {
"header" : "903907_1693958145.jpg"
"background" : null,
"header" : "903907_1693958145.jpg",
"icon" : null
},
"packages" : [
"$rc_monthly"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"display_restore_purchases" : true,
"images" : {
"background" : "300883_1691437592.jpg",
"header" : null,
"icon" : "300883_1691437591.jpg"
},
"packages" : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"display_restore_purchases" : true,
"images" : {
"background" : "background.jpg",
"header" : null,
"icon" : "revenuecatui_default_paywall_app_icon"
},
"packages" : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"display_restore_purchases" : true,
"images" : {
"background" : "background.jpg",
"header" : null,
"icon" : "revenuecatui_default_paywall_app_icon"
},
"packages" : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"display_restore_purchases" : true,
"images" : {
"background" : "background.jpg",
"header" : null,
"icon" : "revenuecatui_default_paywall_app_icon"
},
"packages" : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"display_restore_purchases" : true,
"images" : {
"background" : "background.jpg",
"header" : null,
"icon" : "revenuecatui_default_paywall_app_icon"
},
"packages" : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"display_restore_purchases" : true,
"images" : {
"background" : "background.jpg",
"header" : null,
"icon" : "revenuecatui_default_paywall_app_icon"
},
"packages" : [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"template_name": "1",
"localized_strings": {
"en_US": {
"title": "Paywall",
"subtitle": "Description",
"call_to_action": "Purchase now",
"call_to_action_with_intro_offer": "Purchase now",
"offer_details": "{{ sub_price_per_month }} per month",
"offer_details_with_intro_offer": "Start your {{ sub_offer_duration }} trial, then {{ sub_price_per_month }} per month"
}
},
"config": {
"packages": ["$rc_monthly", "$rc_annual"],
"images": {
"header": "",
"background": "",
"icon": ""
},
"colors": {
"light": {
"background": "#FFFFFF",
"text_1": "#FFFFFF",
"call_to_action_background": "#FFFFFF",
"call_to_action_foreground": "#FFFFFF",
"accent_1": "#FFFFFF"
},
"dark": null
}
},
"asset_base_url": "https://rc-paywalls.s3.amazonaws.com"
}
9 changes: 9 additions & 0 deletions Tests/UnitTests/Paywalls/PaywallDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class PaywallDataTests: BaseHTTPResponseTest {
expect(localization.title) == "Tienda"
}

func testEmptyImageNamesAreParsedAsNil() throws {
let paywall: PaywallData = try self.decodeFixture("PaywallData-empty_images")

let images = paywall.config.images
expect(images.header).to(beNil())
expect(images.background).to(beNil())
expect(images.icon).to(beNil())
}

func testEncodePaywallViewMode() throws {
// iOS 12 does not allow decoding fragments (top-level objects)
try AvailabilityChecks.iOS13APIAvailableOrSkipTest()
Expand Down

0 comments on commit 0bcc509

Please sign in to comment.