Skip to content
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

Public extension to convert Destination into a deeplink Uri #35

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/src/main/kotlin/com/kiwi/navigationcompose/typed/Routing.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.kiwi.navigationcompose.typed

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import androidx.annotation.MainThread
import androidx.core.net.toUri
import androidx.navigation.NavDestination
import com.kiwi.navigationcompose.typed.internal.addPolymorphicType
import com.kiwi.navigationcompose.typed.internal.createRouteSlug
import com.kiwi.navigationcompose.typed.internal.isNavTypeOptional
import com.kiwi.navigationcompose.typed.internal.toRoute
import kotlin.reflect.KClass
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
Expand Down Expand Up @@ -74,3 +80,26 @@ public fun <T : Destination> createRoutePattern(serializer: KSerializer<T>): Str

return destination + path.toString() + query.toString()
}

/**
* Converts the destination into a deeplink [Uri].
*
* The most typical usage would be in construction of an [Intent].
* Such intent can then be used to open the destination from anywhere, e.g. notification.
*
* ```
* fun onArticleClick(id: Int) {
* context.startActivity(
* Intent(
* Intent.ACTION_VIEW,
* Destinations.Article(id).toDeepLinkUri()
* )
* )
* )
* ```
*/
@SuppressLint("RestrictedApi")
@ExperimentalSerializationApi
@MainThread
public fun <T : Destination> T.toDeepLinkUri(): Uri =
NavDestination.createRoute(this.toRoute()).toUri()