-
Notifications
You must be signed in to change notification settings - Fork 6
MOBILE-114: Add public API unregisterInAppCallback #706
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,9 +14,6 @@ import androidx.lifecycle.Lifecycle.State.RESUMED | |
| import androidx.lifecycle.ProcessLifecycleOwner | ||
| import androidx.work.WorkerFactory | ||
| import cloud.mindbox.common.MindboxCommon | ||
| import cloud.mindbox.mobile_sdk.Mindbox.disposeDeviceUuidSubscription | ||
| import cloud.mindbox.mobile_sdk.Mindbox.disposePushTokenSubscription | ||
| import cloud.mindbox.mobile_sdk.Mindbox.handleRemoteMessage | ||
| import cloud.mindbox.mobile_sdk.di.MindboxDI | ||
| import cloud.mindbox.mobile_sdk.di.mindboxInject | ||
| import cloud.mindbox.mobile_sdk.inapp.data.managers.SessionStorageManager | ||
|
|
@@ -735,18 +732,72 @@ public object Mindbox : MindboxLog { | |
| } | ||
|
|
||
| /** | ||
| * Method to register callback for InApp Message | ||
| * Registers a callback for InApp messages. | ||
| * | ||
| * Call this method after you call [Mindbox.init] | ||
| * Call this method after [Mindbox.init]. The SDK holds a **strong reference** to | ||
| * [inAppCallback], so the callback persists until explicitly replaced or removed via | ||
| * [unregisterInAppCallback]. | ||
| * | ||
| * @param inAppCallback used to provide required callback implementation | ||
| * Calling this method again replaces the previously registered callback. | ||
| * | ||
| * **Application-level callback (recommended):** | ||
| * Register once in `Application.onCreate` with a callback that does not reference any | ||
| * Activity. No cleanup needed. | ||
| * ```kotlin | ||
| * class MyApp : Application() { | ||
| * override fun onCreate() { | ||
| * super.onCreate() | ||
| * Mindbox.init(...) | ||
| * Mindbox.registerInAppCallback(MyGlobalInAppCallback()) | ||
| * } | ||
| * } | ||
| * ``` | ||
| * | ||
| * **Per-screen callback:** | ||
| * If different screens require different callback behavior and the callback captures an | ||
| * Activity reference, use `onResume`/`onPause` — **not** `onCreate`/`onDestroy`. | ||
| * Android guarantees that `onPause` of the current Activity is called before `onResume` | ||
| * of the next, so callbacks never overlap and the Activity reference is always cleared | ||
| * before the Activity can be garbage-collected. | ||
| * ```kotlin | ||
| * override fun onResume() { | ||
| * super.onResume() | ||
| * Mindbox.registerInAppCallback(myScreenCallback) | ||
| * } | ||
| * override fun onPause() { | ||
| * super.onPause() | ||
| * Mindbox.unregisterInAppCallback() | ||
| * } | ||
| * ``` | ||
| * | ||
| * @param inAppCallback the callback implementation to register | ||
| **/ | ||
|
|
||
| public fun registerInAppCallback(inAppCallback: InAppCallback) { | ||
| MindboxLoggerImpl.d(this, "registerInAppCallback") | ||
| mindboxLogI("InApp callback registered: ${inAppCallback::class.simpleName}") | ||
| inAppMessageManager.registerInAppCallback(inAppCallback) | ||
| } | ||
|
|
||
| /** | ||
| * Unregisters the current InApp message callback and restores the default SDK behavior. | ||
| * | ||
| * The default behavior handles URL redirects, deep links, payload copying, and logging | ||
| * automatically — the same actions performed when no custom callback is registered. | ||
| * | ||
| * **When to call:** | ||
| * Only needed for per-screen callbacks registered in `onResume`. Call in the corresponding | ||
| * `onPause` to release the Activity reference and restore default behavior while another | ||
| * screen is in the foreground. | ||
|
Comment on lines
+786
to
+789
|
||
| * | ||
| * Not needed if the callback was registered at the Application level and does not | ||
| * reference any Activity. | ||
| * | ||
| * @see registerInAppCallback | ||
| **/ | ||
| public fun unregisterInAppCallback() { | ||
| mindboxLogI("InApp callback unregistered, default behavior restored") | ||
| inAppMessageManager.unregisterInAppCallback() | ||
| } | ||
|
|
||
| /** | ||
| * Method to initialise push services | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| package cloud.mindbox.mobile_sdk.inapp.presentation | ||
|
|
||
| internal class InAppCallbackWrapper( | ||
| private val callback: InAppCallback, | ||
| private val callbackProvider: () -> InAppCallback, | ||
| private val afterDismiss: () -> Unit = {}, | ||
| ) : InAppCallback { | ||
|
|
||
| override fun onInAppClick(id: String, redirectUrl: String, payload: String) { | ||
| callback.onInAppClick(id, redirectUrl, payload) | ||
| callbackProvider().onInAppClick(id, redirectUrl, payload) | ||
| } | ||
|
|
||
| override fun onInAppDismissed(id: String) { | ||
| callback.onInAppDismissed(id) | ||
| callbackProvider().onInAppDismissed(id) | ||
| afterDismiss.invoke() | ||
| } | ||
| } |
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.
The KDoc here claims that unregistering in
onPauseensures the Activity reference is cleared before the Activity can be garbage-collected. In the current implementation, the callback is captured intoInAppCallbackWrapper(callback: InAppCallback)when an in-app is shown (see InAppCallbackWrapper.kt), so any currently displayed/paused in-app can still hold the old callback (and its Activity reference) even afterunregisterInAppCallback()is called. Please adjust the documentation to reflect that unregistering affects only future in-apps (or change the callback wiring so the wrapper delegates to the current callback rather than capturing a snapshot).