All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.telereso.kmp.core.ui.widgets.DeeplinkNavHost.kt Maven / Gradle / Ivy

The newest version!
/*
 * MIT License
 *
 * Copyright (c) 2023 Telereso
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package io.telereso.kmp.core.ui.widgets

import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import io.ktor.http.URLBuilder
import io.ktor.http.Url
import io.ktor.http.path
import io.telereso.kmp.core.ui._currentDeeplink
import io.telereso.kmp.core.ui.getCurrentDeeplink
import io.telereso.kmp.core.ui.setCurrentPath

@Composable
fun DeeplinkNavHost(
    navController: NavHostController,
    startDestination: String,
    modifier: Modifier = Modifier,
    contentAlignment: Alignment = Alignment.Center,
    route: String? = null,
    enterTransition: (AnimatedContentTransitionScope.() -> EnterTransition) =
        { fadeIn(animationSpec = tween(700)) },
    exitTransition: (AnimatedContentTransitionScope.() -> ExitTransition) =
        { fadeOut(animationSpec = tween(700)) },
    popEnterTransition: (AnimatedContentTransitionScope.() -> EnterTransition) =
        enterTransition,
    popExitTransition: (AnimatedContentTransitionScope.() -> ExitTransition) =
        exitTransition,
    builder: NavGraphBuilder.() -> Unit
) {

    LaunchedEffect(Unit) {
        setCurrentPath(startDestination.lowercase())
    }

    NavHost(
        navController = navController,
        startDestination = _currentDeeplink.value.route(startDestination),
        modifier = modifier,
        contentAlignment = contentAlignment,
        route = route,
        enterTransition = enterTransition,
        exitTransition = exitTransition,
        popEnterTransition = popEnterTransition,
        popExitTransition = popExitTransition,
        builder = builder,
    )
}

fun Url.route(path: String): String {
    val builder = URLBuilder(this)
    builder.path(path.lowercase())
    return builder.build().toString()
}

fun Url.base(): Url {
    val builder = URLBuilder()
    builder.host = host
    builder.protocol = protocol
    builder.port = port
    return builder.build()
}

fun NavHostController.deeplink(route: String) {
    val current = _currentDeeplink.value
    val newSegments = current.segments.toMutableList()
    if (newSegments.isNotEmpty())
        newSegments.removeLast()
    newSegments.add(route.lowercase())
    val builder = URLBuilder(current)
    builder.path(*newSegments.toTypedArray())
    _currentDeeplink.value = builder.build()
    navigate(_currentDeeplink.value.toString())
    setCurrentPath(route.lowercase())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy