Skip to content

Commit

Permalink
Renamed withDeepLink to handleDeepLink
Browse files Browse the repository at this point in the history
  • Loading branch information
arkivanov committed Apr 27, 2024
1 parent 0b2bfc2 commit 7e4612f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion decompose/api/android/decompose.api
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public abstract interface class com/arkivanov/decompose/ComponentContextFactoryO
}

public final class com/arkivanov/decompose/DeeplinkUtilsKt {
public static final fun withDeepLink (Landroid/app/Activity;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun handleDeepLink (Landroid/app/Activity;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
}

public final class com/arkivanov/decompose/DefaultComponentContext : com/arkivanov/decompose/ComponentContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import androidx.savedstate.SavedStateRegistryOwner
* Extracts a deep link URL from this [Activity] [Intent], calls [block]
* function with the extracted deep link URL (if any) and returns the result.
*
* This function must be called from [Activity.onCreate] method.
*
* It is strongly recommended to always use the `standard` (default)
* `launchMode` for the [Activity] when handling deep links. This function takes
* care of restarting the [Activity] task if needed, in which case the returned
* value is `null`.
* care of restarting the [Activity] task and finishing this [Activity] if needed,
* in which case the returned value is `null`.
*
* Example of creating a root component with deep link support.
*
Expand All @@ -25,7 +27,7 @@ import androidx.savedstate.SavedStateRegistryOwner
* super.onCreate(savedInstanceState)
*
* val root =
* withDeepLink { uri ->
* handleDeepLink { uri ->
* val itemId = uri?.extractItemId() // Parse the deep link
* DefaultRootComponent(
* componentContext = defaultComponentContext(discardSavedState = itemId != null),
Expand All @@ -42,28 +44,25 @@ import androidx.savedstate.SavedStateRegistryOwner
* ```
*/
@ExperimentalDecomposeApi
fun <A, T : Any> A.withDeepLink(
block: (deepLink: Uri?) -> T,
fun <A, T : Any> A.handleDeepLink(
block: (Uri?) -> T,
): T? where A : Activity, A : SavedStateRegistryOwner {
if (restartIfNeeded()) {
return null
}

val savedState: Bundle? = savedStateRegistry.consumeRestoredStateForKey(key = KEY_SAVED_DEEP_LINK_STATE)

@Suppress("DEPRECATION")
val lastDeepLink = savedState?.getParcelable(KEY_LAST_DEEP_LINK) as Uri?

val deepLink = intent.data
val isDeepLinkHandled = savedState?.getBoolean(KEY_DEEP_LINK_HANDLED) ?: false
val deepLink = intent.data.takeUnless { isDeepLinkHandled }

savedStateRegistry.registerSavedStateProvider(key = KEY_SAVED_DEEP_LINK_STATE) {
bundleOf(KEY_LAST_DEEP_LINK to deepLink)
bundleOf(KEY_DEEP_LINK_HANDLED to (isDeepLinkHandled || (deepLink != null)))
}

return block(deepLink.takeUnless { it == lastDeepLink })
return block(deepLink)
}

// Derived from https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigation/navigation-runtime/src/main/java/androidx/navigation/NavController.kt;l=1487-1504?q=Intent.flags&ss=androidx%2Fplatform%2Fframeworks%2Fsupport:navigation%2F
// Derived from https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigation/navigation-runtime/src/main/java/androidx/navigation/NavController.kt;l=1486;drc=fd7d0dc4a56c2aef65424db7986aa057f9717661
private fun Activity.restartIfNeeded(): Boolean {
if ((intent.flags and Intent.FLAG_ACTIVITY_NEW_TASK == 0) || (intent.flags and Intent.FLAG_ACTIVITY_CLEAR_TASK != 0)) {
return false
Expand All @@ -84,5 +83,5 @@ private fun Activity.restartIfNeeded(): Boolean {
}

private const val KEY_SAVED_DEEP_LINK_STATE = "SAVED_DEEP_LINK_STATE"
private const val KEY_LAST_DEEP_LINK = "LAST_DEEP_LINK"
private const val KEY_DEEP_LINK_HANDLED = "DEEP_LINK_HANDLED"

0 comments on commit 7e4612f

Please sign in to comment.