Commit be3bcf1e by Aleksandr

Drop current route when navigating with navigateSingleTopTo()

parent b988457c
......@@ -50,6 +50,7 @@ class HomeUseCase @Inject constructor(
// los gatos
lat = 37.2451678,
lng = -121.9752617,
uris = uris
)
uris != null -> HomeContract.Effect.Navigation.ToPreview(uris)
......
......@@ -54,11 +54,17 @@ fun AppNavHost(
)
composable(
route = routeMap(lat = "{${Argument.LATITUDE}}", lng = "{${Argument.LONGITUDE}}", spotCode = "{${Argument.SPOT_CODE}}"),
route = routeMap(
lat = "{${Argument.LATITUDE}}",
lng = "{${Argument.LONGITUDE}}",
spotCode = "{${Argument.SPOT_CODE}}",
uris = "{${Argument.URI}}"
),
arguments = listOf(
makeNavArgument(Argument.LATITUDE, NavType.StringType, nullable = true),
makeNavArgument(Argument.LONGITUDE, NavType.StringType, nullable = true),
makeNavArgument(Argument.SPOT_CODE, NavType.StringType, nullable = true),
makeNavArgument(Argument.LATITUDE, NavType.StringType),
makeNavArgument(Argument.LONGITUDE, NavType.StringType),
makeNavArgument(Argument.SPOT_CODE, NavType.StringType),
makeNavArgument(Argument.URI, NavType.StringType)
),
content = { MapScreenDestination(navController) }
)
......
......@@ -14,10 +14,16 @@ internal fun routeRenderPreview(uris: String = Argument.URI) = RenderPreview.rou
.appendQueryParameter(Argument.URI, uris)
.toString()
internal fun routeMap(lat: String? = null, lng: String? = null, spotCode: String? = null) = Map.route.toUri().buildUpon()
internal fun routeMap(
lat: String? = null,
lng: String? = null,
spotCode: String? = null,
uris: String? = null
) = Map.route.toUri().buildUpon()
.appendQueryParameter(Argument.LATITUDE, lat)
.appendQueryParameter(Argument.LONGITUDE, lng)
.appendQueryParameter(Argument.SPOT_CODE, spotCode)
.appendQueryParameter(Argument.URI, uris)
.toString()
internal fun routeJobDetails(id: String = Argument.ID) = JobDetails.route.toUri().buildUpon()
......
......@@ -24,7 +24,7 @@ fun HomeScreenDestination(navController: NavHostController) {
val route = when (effect) {
HomeContract.Effect.Navigation.ToLogin -> TODO("Session is not implemented")
HomeContract.Effect.Navigation.ToSelectContent -> routeSelectContent()
is HomeContract.Effect.Navigation.ToSelectSpot -> routeMap(lat = effect.lat?.toString(), lng = effect.lng?.toString(), spotCode = effect.spotCode)
is HomeContract.Effect.Navigation.ToSelectSpot -> routeMap(lat = effect.lat?.toString(), lng = effect.lng?.toString(), spotCode = effect.spotCode, uris = effect.uris)
is HomeContract.Effect.Navigation.ToPreview -> routeRenderPreview(uris = effect.uris)
}
......
......@@ -3,6 +3,8 @@ package com.isidroid.c23.ui.navigation.destinations
import androidx.compose.runtime.Composable
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.isidroid.c23.ui.navigation.Home
import com.isidroid.c23.ui.navigation.routeRenderPreview
import com.isidroid.c23.ui.navigation.routeSelectContent
import com.isidroid.c23.ui.screen.map.MapContract
import com.isidroid.c23.ui.screen.map.MapScreen
......@@ -23,6 +25,9 @@ fun MapScreenDestination(navController: NavHostController) {
when (effect) {
MapContract.Effect.Navigation.ToBack -> navController.popBackStack()
MapContract.Effect.Navigation.ToSelectContent -> navController.navigateSingleTopTo(routeSelectContent())
is MapContract.Effect.Navigation.ToPreview -> navController.navigateSingleTopTo(
routeRenderPreview(uris = effect.uris),
)
}
},
)
......
......@@ -17,7 +17,7 @@ class HomeContract {
sealed interface Navigation : Effect {
data object ToLogin : Navigation
data object ToSelectContent : Navigation
data class ToSelectSpot(val lat: Double? = null, val lng: Double? = null, val spotCode: String? = null) : Navigation
data class ToSelectSpot(val lat: Double? = null, val lng: Double? = null, val spotCode: String? = null, val uris: String?) : Navigation
data class ToPreview(val uris: String) : Navigation
}
}
......
package com.isidroid.c23.ui.screen.map
import com.isidroid.c23.ui.screen.home.HomeContract
import com.isidroid.core.vm.ViewEvent
import com.isidroid.core.vm.ViewSideEffect
import com.isidroid.core.vm.ViewState
......@@ -18,6 +19,7 @@ class MapContract {
sealed interface Navigation : Effect {
data object ToBack: Navigation
data object ToSelectContent: Navigation
data class ToPreview(val uris: String) : Navigation
}
}
......
......@@ -32,6 +32,7 @@ class MapViewModel @Inject constructor(
private var _findSpotsJob: Job? = null
private val _data = hashMapOf<String, RichSpot>()
private var _markers = hashMapOf<String, List<RichSpot>>()
private var _uris: String? = null
private val _spotResultStateFlow = MutableStateFlow<List<MapMarker>>(emptyList())
val spotResultStateFlow = _spotResultStateFlow.asStateFlow()
......@@ -58,9 +59,15 @@ class MapViewModel @Inject constructor(
val flowLat = savedStateHandle.getStateFlow<String?>(Argument.LATITUDE, null)
val flowLng = savedStateHandle.getStateFlow<String?>(Argument.LONGITUDE, null)
val flowSpotCode = savedStateHandle.getStateFlow<String?>(Argument.SPOT_CODE, null)
val flowUris = savedStateHandle.getStateFlow<String?>(Argument.URI, null)
combine(flowLat, flowLng, flowSpotCode) { lat, lng, spotCode ->
prepareData(lat?.toDoubleOrNull(), lng?.toDoubleOrNull(), spotCode)
combine(flowLat, flowLng, flowSpotCode, flowUris) { lat, lng, spotCode, uris ->
_uris = uris
prepareData(
lat = lat?.toDoubleOrNull(),
lng = lng?.toDoubleOrNull(),
spotCode = spotCode
)
}.collect()
}
......@@ -98,7 +105,7 @@ class MapViewModel @Inject constructor(
private suspend fun selectSpot(id: String) {
useCase.selectSpot(_data[id])
.flowOn(Dispatchers.IO)
.collect { setEffect { MapContract.Effect.Navigation.ToSelectContent } }
.collect { onSpotSelected() }
}
private suspend fun moveToMyLocation() {
......@@ -123,4 +130,11 @@ class MapViewModel @Inject constructor(
_spotResultStateFlow.emit(markers)
}
private fun onSpotSelected() {
val effect = _uris?.let { MapContract.Effect.Navigation.ToPreview(uris = it) } ?: MapContract.Effect.Navigation.ToSelectContent
_uris = null
setEffect { effect }
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ import timber.log.Timber
fun NavController.navigateSingleTopTo(
route: String,
dropCurrent: Boolean = true,
isSaveState: Boolean? = null,
isLaunchSingleTop: Boolean = true,
isRestoreState: Boolean? = null,
......@@ -16,7 +17,9 @@ fun NavController.navigateSingleTopTo(
popupToRoute: String? = null
) {
navigate(route) {
val node = popupToRoute?.let { graph.findNode(popupToRoute) } ?: graph.findStartDestination()
val dropNode = if (dropCurrent) currentDestination?.route else popupToRoute
val node = dropNode?.let { graph.findNode(popupToRoute) } ?: graph.findStartDestination()
popUpTo(node.id) {
inclusive = isInclusive
if (isSaveState != null)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment