-
Notifications
You must be signed in to change notification settings - Fork 47
Add callback requests and callback handlers #422
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
Open
ianrumac
wants to merge
1
commit into
develop
Choose a base branch
from
ir/feat/custom-callbacks
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
60 changes: 60 additions & 0 deletions
60
Sources/SuperwallKit/Paywall/Presentation/CustomCallback.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,60 @@ | ||
| // | ||
| // CustomCallback.swift | ||
| // SuperwallKit | ||
| // | ||
| // Created by Ian Rumac on 2025. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// The behavior of a custom callback request. | ||
| public enum CustomCallbackBehavior: String, Decodable { | ||
| /// The paywall waits for the callback to complete before continuing. | ||
| case blocking | ||
| /// The paywall continues immediately; callback triggers onSuccess/onFailure. | ||
| case nonBlocking = "non-blocking" | ||
| } | ||
|
|
||
| /// A custom callback request from the paywall. | ||
| public struct CustomCallback { | ||
| /// The name of the callback. | ||
| public let name: String | ||
|
|
||
| /// Optional variables passed with the callback. | ||
| public let variables: [String: Any]? | ||
|
|
||
| public init(name: String, variables: [String: Any]? = nil) { | ||
| self.name = name | ||
| self.variables = variables | ||
| } | ||
| } | ||
|
|
||
| /// The status of a custom callback result. | ||
| public enum CustomCallbackResultStatus: String { | ||
| case success | ||
| case failure | ||
| } | ||
|
|
||
| /// The result of handling a custom callback. | ||
| public struct CustomCallbackResult { | ||
| /// The status of the callback result. | ||
| public let status: CustomCallbackResultStatus | ||
|
|
||
| /// Optional data to send back to the paywall. | ||
| public let data: [String: Any]? | ||
|
|
||
| public init(status: CustomCallbackResultStatus, data: [String: Any]? = nil) { | ||
| self.status = status | ||
| self.data = data | ||
| } | ||
|
|
||
| /// Creates a success result with optional data. | ||
| public static func success(data: [String: Any]? = nil) -> CustomCallbackResult { | ||
| return CustomCallbackResult(status: .success, data: data) | ||
| } | ||
|
|
||
| /// Creates a failure result with optional data. | ||
| public static func failure(data: [String: Any]? = nil) -> CustomCallbackResult { | ||
| return CustomCallbackResult(status: .failure, data: data) | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
Sources/SuperwallKit/Paywall/Presentation/CustomCallbackRegistry.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,47 @@ | ||
| // | ||
| // CustomCallbackRegistry.swift | ||
| // SuperwallKit | ||
| // | ||
| // Created by Ian Rumac on 2025. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Thread-safe registry for custom callback handlers, keyed by paywall identifier. | ||
| final class CustomCallbackRegistry: @unchecked Sendable { | ||
| private var handlers: [String: (CustomCallback) async -> CustomCallbackResult] = [:] | ||
| private let lock = NSLock() | ||
|
|
||
| /// Registers a callback handler for a specific paywall identifier. | ||
| func register( | ||
| paywallIdentifier: String, | ||
| handler: @escaping (CustomCallback) async -> CustomCallbackResult | ||
| ) { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| handlers[paywallIdentifier] = handler | ||
| } | ||
|
|
||
| /// Unregisters the callback handler for a specific paywall identifier. | ||
| func unregister(paywallIdentifier: String) { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| handlers.removeValue(forKey: paywallIdentifier) | ||
| } | ||
|
|
||
| /// Gets the callback handler for a specific paywall identifier. | ||
| func getHandler( | ||
| paywallIdentifier: String | ||
| ) -> ((CustomCallback) async -> CustomCallbackResult)? { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| return handlers[paywallIdentifier] | ||
| } | ||
|
|
||
| /// Clears all registered handlers. | ||
| func clear() { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| handlers.removeAll() | ||
| } | ||
| } |
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
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
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CHANGELOG.md needs updating for this new API addition per the style guide (
CLAUDE.md:97-98)The guide states: "Update CHANGELOG.md for customer-facing changes: Include new API additions... Focus on what the change does for developers."
Suggest adding an entry like:
Context Used: Context from
dashboard- CLAUDE.md (source)Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI