Coverage Summary for Class: DeeplinkNavHostKt (io.telereso.kmp.core.ui.widgets)

Class Method, % Branch, % Line, % Instruction, %
DeeplinkNavHostKt 0% (0/4) 0% (0/96) 0% (0/39) 0% (0/599)
DeeplinkNavHostKt$DeeplinkNavHost$3$1 0% (0/1) 0% (0/1) 0% (0/8)
Total 0% (0/5) 0% (0/96) 0% (0/40) 0% (0/607)


 /*
  * 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.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.browserSetCurrentPath
 
 @Composable
 fun DeeplinkNavHost(
     navController: NavHostController,
     startDestination: String,
     modifier: Modifier = Modifier,
     contentAlignment: Alignment = Alignment.Center,
     route: String? = null,
     enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition) =
         { fadeIn(animationSpec = tween(700)) },
     exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition) =
         { fadeOut(animationSpec = tween(700)) },
     popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition) =
         enterTransition,
     popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition) =
         exitTransition,
     builder: NavGraphBuilder.() -> Unit
 ) {
 
     LaunchedEffect(Unit) {
         browserSetCurrentPath(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())
     browserSetCurrentPath(route.lowercase())
 }