-
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 1 commit
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
205 changes: 205 additions & 0 deletions
205
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,205 @@ | ||
| import Foundation | ||
| import SwiftUI | ||
| // swiftlint:disable missing_docs | ||
|
|
||
| #if !os(tvOS) // For Paywalls V2 | ||
|
|
||
| /// A validated message sent from a Paywalls V2 `web_view` component to your app. | ||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| public struct PaywallWebViewMessage: Sendable, Equatable { | ||
|
|
||
| /// The identifier of the `web_view` component that produced this message. | ||
| public let componentID: String | ||
|
|
||
| /// The message type, e.g. `"rc:step-complete"`. | ||
| public let type: String | ||
|
|
||
| /// The responses collected by the web flow. Only populated for `"rc:step-complete"` messages. | ||
| public let responses: [String: PaywallWebViewValue]? | ||
|
|
||
| /// The error reported by the web content. Only populated for `"rc:error"` messages. | ||
| public let error: String? | ||
|
|
||
| public init( | ||
| componentID: String, | ||
| type: String, | ||
| responses: [String: PaywallWebViewValue]? = nil, | ||
| error: String? = nil | ||
| ) { | ||
| self.componentID = componentID | ||
| self.type = type | ||
| self.responses = responses | ||
| self.error = error | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /// 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, *) | ||
| public struct PaywallWebViewValue: Sendable, Equatable, Hashable, Codable { | ||
|
|
||
| private indirect enum Storage: Sendable, Equatable, Hashable { | ||
|
JZDesign marked this conversation as resolved.
Outdated
|
||
| case string(String) | ||
| case number(Double) | ||
|
JZDesign marked this conversation as resolved.
|
||
| 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. | ||
| public static func string(_ value: String) -> Self { Self(.string(value)) } | ||
|
|
||
| /// Creates a numeric value. Non-finite values normalize to ``null`` because JSON cannot encode them. | ||
| public static func number(_ value: Double) -> Self { value.isFinite ? Self(.number(value)) : .null } | ||
|
|
||
| /// Creates a boolean value. | ||
| public static func bool(_ value: Bool) -> Self { Self(.bool(value)) } | ||
|
|
||
| /// Creates an array value. | ||
| public static func array(_ value: [PaywallWebViewValue]) -> Self { Self(.array(value)) } | ||
|
|
||
| /// Creates an object (dictionary) value. | ||
| public static func object(_ value: [String: PaywallWebViewValue]) -> Self { Self(.object(value)) } | ||
|
|
||
| /// A null value. | ||
| public static var null: Self { Self(.null) } | ||
|
|
||
| public var stringValue: String? { | ||
| if case .string(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| public var numberValue: Double? { | ||
| if case .number(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| public var boolValue: Bool? { | ||
| if case .bool(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| public var arrayValue: [PaywallWebViewValue]? { | ||
| if case .array(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| public var objectValue: [String: PaywallWebViewValue]? { | ||
| if case .object(let value) = self.storage { return value } | ||
| return nil | ||
| } | ||
|
|
||
| public var isNull: Bool { | ||
| if case .null = self.storage { return true } | ||
| return false | ||
| } | ||
|
|
||
| public 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)) | ||
| } | ||
| } | ||
|
|
||
| public 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() | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| @MainActor | ||
| public struct PaywallWebViewController { | ||
|
|
||
| init() {} | ||
|
|
||
| public func postVariables(componentID: String, variables: [String: PaywallWebViewValue]) { | ||
| // Session wiring lands in a later PR; public API is inert until then. | ||
| } | ||
|
|
||
| public func postMessage(componentID: String, type: String, variables: [String: PaywallWebViewValue]) { | ||
| // Session wiring lands in a later PR; public API is inert until then. | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /// A wrapper for the Paywalls V2 `web_view` message handler. | ||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| public struct PaywallWebViewMessageAction { | ||
|
|
||
| private let action: @MainActor (PaywallWebViewMessage, PaywallWebViewController) -> Void | ||
|
|
||
| public init( | ||
| _ action: @escaping @MainActor (PaywallWebViewMessage, PaywallWebViewController) -> Void | ||
| ) { | ||
| self.action = action | ||
| } | ||
|
|
||
| @MainActor | ||
| public func callAsFunction(_ message: PaywallWebViewMessage, _ controller: PaywallWebViewController) { | ||
| self.action(message, controller) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| struct PaywallWebViewMessageActionKey: EnvironmentKey { | ||
| static let defaultValue: PaywallWebViewMessageAction? = nil | ||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| extension EnvironmentValues { | ||
|
|
||
| var paywallWebViewMessageAction: PaywallWebViewMessageAction? { | ||
| get { self[PaywallWebViewMessageActionKey.self] } | ||
| set { self[PaywallWebViewMessageActionKey.self] = newValue } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| extension View { | ||
|
|
||
| /// Invokes the given closure when a Paywalls V2 `web_view` component sends a message. | ||
| public func onPaywallWebViewMessage( | ||
| _ action: @escaping @MainActor (PaywallWebViewMessage, PaywallWebViewController) -> Void | ||
| ) -> some View { | ||
| self.environment(\.paywallWebViewMessageAction, PaywallWebViewMessageAction(action)) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
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
77 changes: 77 additions & 0 deletions
77
Tests/APITesters/AllAPITests/RevenueCatUISwiftAPITester/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,77 @@ | ||
| // | ||
| // PaywallWebViewAPI.swift | ||
| // RevenueCatUISwiftAPITester | ||
| // | ||
| // Compile-time checks for the Paywalls V2 web_view public API surface. | ||
| // | ||
|
|
||
| import RevenueCat | ||
| import RevenueCatUI | ||
| import SwiftUI | ||
|
|
||
| #if !os(tvOS) // For Paywalls V2 | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| struct PaywallWebViewApp: View { | ||
|
|
||
| var body: some View { | ||
| Text("") | ||
| .onPaywallWebViewMessage { (message: PaywallWebViewMessage, controller: PaywallWebViewController) in | ||
| let componentID: String = message.componentID | ||
| let type: String = message.type | ||
| let responses: [String: PaywallWebViewValue]? = message.responses | ||
| let error: String? = message.error | ||
|
|
||
| controller.postVariables( | ||
| componentID: componentID, | ||
| variables: ["custom": .object(["plan": .string("annual")])] | ||
| ) | ||
| controller.postMessage(componentID: componentID, type: type, variables: [:]) | ||
|
|
||
| _ = (responses, error) | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| func checkPaywallWebViewMessageAPI() { | ||
| let message = PaywallWebViewMessage( | ||
| componentID: "", | ||
| type: "rc:step-complete", | ||
| responses: ["selected_plan": .string("annual")], | ||
| error: nil | ||
| ) | ||
| let sameMessage: Bool = message == message | ||
| _ = sameMessage | ||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| func checkPaywallWebViewValueAPI() { | ||
| let string: PaywallWebViewValue = .string("s") | ||
| let number: PaywallWebViewValue = .number(1.5) | ||
| let bool: PaywallWebViewValue = .bool(true) | ||
| let array: PaywallWebViewValue = .array([string, number]) | ||
| let object: PaywallWebViewValue = .object(["key": bool]) | ||
| let null: PaywallWebViewValue = .null | ||
|
|
||
| let _: String? = string.stringValue | ||
| let _: Double? = number.numberValue | ||
| let _: Bool? = bool.boolValue | ||
| let _: [PaywallWebViewValue]? = array.arrayValue | ||
| let _: [String: PaywallWebViewValue]? = object.objectValue | ||
| let _: Bool = null.isNull | ||
| let _: Set<PaywallWebViewValue> = [string, number] | ||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| @MainActor | ||
| func checkPaywallWebViewMessageActionAPI( | ||
| message: PaywallWebViewMessage, | ||
| controller: PaywallWebViewController | ||
| ) { | ||
| let action = PaywallWebViewMessageAction { (_: PaywallWebViewMessage, _: PaywallWebViewController) in } | ||
| action(message, controller) | ||
| } | ||
|
|
||
| #endif |
Oops, something went wrong.
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.