-
Notifications
You must be signed in to change notification settings - Fork 432
feat(paywalls): internal web_view JSON value type #7227
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
Merged
+185
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5cf1dfd
feat(paywalls): public API surface for web_view messages (1/7)
cursoragent e671378
refactor(paywalls): keep web view bridge internal
cursoragent ca80c8f
Merge branch 'main' into webview/1-public-api
ajpallares 0612595
refactor(paywalls): remove unused web view handler API
cursoragent d14d63c
fix(paywalls): retain internal bridge dependencies
cursoragent 19cbfe6
refactor(paywalls): remove web view message handler concept
JZDesign d829317
Merge branch 'main' into webview/1-public-api
ajpallares 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 hidden or 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
112 changes: 112 additions & 0 deletions
112
RevenueCatUI/Templates/V2/Components/WebView/PaywallWebViewAPI.swift
This file contains hidden or 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,112 @@ | ||
| import Foundation | ||
|
|
||
| #if !os(tvOS) // For Paywalls V2 | ||
|
|
||
| /// A JSON-compatible value exchanged between a Paywalls V2 `web_view` component and your app. | ||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| struct PaywallWebViewValue: Sendable, Equatable, Hashable, Codable { | ||
|
|
||
| private enum Storage: Sendable, Equatable, Hashable { | ||
| case string(String) | ||
| // The web peer uses JavaScript `Number` values, so there is no integer precision to preserve. | ||
| case number(Double) | ||
| case bool(Bool) | ||
| case array([PaywallWebViewValue]) | ||
| case object([String: PaywallWebViewValue]) | ||
| case null | ||
| } | ||
|
|
||
| private let storage: Storage | ||
|
|
||
| private init(_ storage: Storage) { | ||
| self.storage = storage | ||
| } | ||
|
|
||
| /// Creates a string value. | ||
| static func string(_ value: String) -> Self { Self(.string(value)) } | ||
|
|
||
| /// Creates a numeric value. Non-finite values normalize to ``null`` because JSON cannot encode them. | ||
| static func number(_ value: Double) -> Self { value.isFinite ? Self(.number(value)) : .null } | ||
|
|
||
| /// Creates a boolean value. | ||
| static func bool(_ value: Bool) -> Self { Self(.bool(value)) } | ||
|
|
||
| /// Creates an array value. | ||
| static func array(_ value: [PaywallWebViewValue]) -> Self { Self(.array(value)) } | ||
|
|
||
| /// Creates an object (dictionary) value. | ||
| static func object(_ value: [String: PaywallWebViewValue]) -> Self { Self(.object(value)) } | ||
|
|
||
| /// A null value. | ||
| static var null: Self { Self(.null) } | ||
|
|
||
| var stringValue: String? { | ||
| if case .string(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| var numberValue: Double? { | ||
| if case .number(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| var boolValue: Bool? { | ||
| if case .bool(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| var arrayValue: [PaywallWebViewValue]? { | ||
| if case .array(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| var objectValue: [String: PaywallWebViewValue]? { | ||
| if case .object(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| var isNull: Bool { | ||
| if case .null = self.storage { return true } | ||
| return false | ||
| } | ||
|
|
||
| init(from decoder: Decoder) throws { | ||
| let container = try decoder.singleValueContainer() | ||
|
|
||
| if container.decodeNil() { | ||
| self = .null | ||
| } else if let value = try? container.decode(Bool.self) { | ||
| self = .bool(value) | ||
| } else if let value = try? container.decode(Double.self) { | ||
| self = .number(value) | ||
| } else if let value = try? container.decode(String.self) { | ||
| self = .string(value) | ||
| } else if let value = try? container.decode([PaywallWebViewValue].self) { | ||
| self = .array(value) | ||
| } else { | ||
| self = .object(try container.decode([String: PaywallWebViewValue].self)) | ||
| } | ||
| } | ||
|
|
||
| func encode(to encoder: Encoder) throws { | ||
| var container = encoder.singleValueContainer() | ||
|
|
||
| switch self.storage { | ||
| case .string(let value): | ||
| try container.encode(value) | ||
| case .number(let value): | ||
| try container.encode(value) | ||
| case .bool(let value): | ||
| try container.encode(value) | ||
| case .array(let value): | ||
| try container.encode(value) | ||
| case .object(let value): | ||
| try container.encode(value) | ||
| case .null: | ||
| try container.encodeNil() | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
59 changes: 59 additions & 0 deletions
59
Tests/RevenueCatUITests/PaywallsV2/PaywallWebViewValueTests.swift
This file contains hidden or 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,59 @@ | ||
| // | ||
| // Copyright RevenueCat Inc. All Rights Reserved. | ||
| // | ||
|
|
||
| @testable import RevenueCatUI | ||
| import XCTest | ||
|
|
||
| #if !os(tvOS) | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| final class PaywallWebViewValueTests: TestCase { | ||
|
|
||
| func testFactoryAccessorsAndCodableRoundTrip() throws { | ||
| let value: PaywallWebViewValue = .object([ | ||
| "string": .string("value"), | ||
| "number": .number(1.25), | ||
| "bool": .bool(true), | ||
| "array": .array([.null, .string("x")]), | ||
| "object": .object(["nested": .bool(false)]), | ||
| "null": .null | ||
| ]) | ||
|
|
||
| let decoded = try JSONDecoder().decode( | ||
| PaywallWebViewValue.self, | ||
| from: try JSONEncoder().encode(value) | ||
| ) | ||
|
|
||
| XCTAssertEqual(decoded, value) | ||
| XCTAssertEqual(decoded.objectValue?["string"]?.stringValue, "value") | ||
| XCTAssertEqual(decoded.objectValue?["number"]?.numberValue, 1.25) | ||
| XCTAssertEqual(decoded.objectValue?["bool"]?.boolValue, true) | ||
| XCTAssertTrue(decoded.objectValue?["null"]?.isNull == true) | ||
| } | ||
|
|
||
| func testNumberBoolDisambiguation() throws { | ||
| let decoded = try JSONDecoder().decode( | ||
| PaywallWebViewValue.self, | ||
| from: Data(#"{"bool":true,"number":1}"#.utf8) | ||
| ) | ||
|
|
||
| XCTAssertEqual(decoded.objectValue?["bool"]?.boolValue, true) | ||
| XCTAssertNil(decoded.objectValue?["bool"]?.numberValue) | ||
| XCTAssertEqual(decoded.objectValue?["number"]?.numberValue, 1) | ||
| } | ||
|
|
||
| func testNonFiniteNumbersNormalizeToNullAndEncode() throws { | ||
| XCTAssertTrue(PaywallWebViewValue.number(.nan).isNull) | ||
| XCTAssertTrue(PaywallWebViewValue.number(.infinity).isNull) | ||
| XCTAssertTrue(PaywallWebViewValue.number(-.infinity).isNull) | ||
| XCTAssertNil(PaywallWebViewValue.number(.nan).numberValue) | ||
|
|
||
| let value: PaywallWebViewValue = .object(["bad": .number(.nan)]) | ||
| XCTAssertNoThrow(try JSONEncoder().encode(value)) | ||
| XCTAssertEqual(PaywallWebViewValue.number(-0.0), .number(0.0)) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif |
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.
Uh oh!
There was an error while loading. Please reload this page.