Commit be3bcf1e by Aleksandr

Drop current route when navigating with navigateSingleTopTo()

parent b988457c
...@@ -50,6 +50,7 @@ class HomeUseCase @Inject constructor( ...@@ -50,6 +50,7 @@ class HomeUseCase @Inject constructor(
// los gatos // los gatos
lat = 37.2451678, lat = 37.2451678,
lng = -121.9752617, lng = -121.9752617,
uris = uris
) )
uris != null -> HomeContract.Effect.Navigation.ToPreview(uris) uris != null -> HomeContract.Effect.Navigation.ToPreview(uris)
......
...@@ -54,11 +54,17 @@ fun AppNavHost( ...@@ -54,11 +54,17 @@ fun AppNavHost(
) )
composable( 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( arguments = listOf(
makeNavArgument(Argument.LATITUDE, NavType.StringType, nullable = true), makeNavArgument(Argument.LATITUDE, NavType.StringType),
makeNavArgument(Argument.LONGITUDE, NavType.StringType, nullable = true), makeNavArgument(Argument.LONGITUDE, NavType.StringType),
makeNavArgument(Argument.SPOT_CODE, NavType.StringType, nullable = true), makeNavArgument(Argument.SPOT_CODE, NavType.StringType),
makeNavArgument(Argument.URI, NavType.StringType)
), ),
content = { MapScreenDestination(navController) } content = { MapScreenDestination(navController) }
) )
......
...@@ -14,10 +14,16 @@ internal fun routeRenderPreview(uris: String = Argument.URI) = RenderPreview.rou ...@@ -14,10 +14,16 @@ internal fun routeRenderPreview(uris: String = Argument.URI) = RenderPreview.rou
.appendQueryParameter(Argument.URI, uris) .appendQueryParameter(Argument.URI, uris)
.toString() .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.LATITUDE, lat)
.appendQueryParameter(Argument.LONGITUDE, lng) .appendQueryParameter(Argument.LONGITUDE, lng)
.appendQueryParameter(Argument.SPOT_CODE, spotCode) .appendQueryParameter(Argument.SPOT_CODE, spotCode)
.appendQueryParameter(Argument.URI, uris)
.toString() .toString()
internal fun routeJobDetails(id: String = Argument.ID) = JobDetails.route.toUri().buildUpon() internal fun routeJobDetails(id: String = Argument.ID) = JobDetails.route.toUri().buildUpon()
......
...@@ -24,7 +24,7 @@ fun HomeScreenDestination(navController: NavHostController) { ...@@ -24,7 +24,7 @@ fun HomeScreenDestination(navController: NavHostController) {
val route = when (effect) { val route = when (effect) {
HomeContract.Effect.Navigation.ToLogin -> TODO("Session is not implemented") HomeContract.Effect.Navigation.ToLogin -> TODO("Session is not implemented")
HomeContract.Effect.Navigation.ToSelectContent -> routeSelectContent() 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) is HomeContract.Effect.Navigation.ToPreview -> routeRenderPreview(uris = effect.uris)
} }
......
...@@ -3,6 +3,8 @@ package com.isidroid.c23.ui.navigation.destinations ...@@ -3,6 +3,8 @@ package com.isidroid.c23.ui.navigation.destinations
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController 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.navigation.routeSelectContent
import com.isidroid.c23.ui.screen.map.MapContract import com.isidroid.c23.ui.screen.map.MapContract
import com.isidroid.c23.ui.screen.map.MapScreen import com.isidroid.c23.ui.screen.map.MapScreen
...@@ -23,6 +25,9 @@ fun MapScreenDestination(navController: NavHostController) { ...@@ -23,6 +25,9 @@ fun MapScreenDestination(navController: NavHostController) {
when (effect) { when (effect) {
MapContract.Effect.Navigation.ToBack -> navController.popBackStack() MapContract.Effect.Navigation.ToBack -> navController.popBackStack()
MapContract.Effect.Navigation.ToSelectContent -> navController.navigateSingleTopTo(routeSelectContent()) MapContract.Effect.Navigation.ToSelectContent -> navController.navigateSingleTopTo(routeSelectContent())
is MapContract.Effect.Navigation.ToPreview -> navController.navigateSingleTopTo(
routeRenderPreview(uris = effect.uris),
)
} }
}, },
) )
......
...@@ -17,7 +17,7 @@ class HomeContract { ...@@ -17,7 +17,7 @@ class HomeContract {
sealed interface Navigation : Effect { sealed interface Navigation : Effect {
data object ToLogin : Navigation data object ToLogin : Navigation
data object ToSelectContent : 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 data class ToPreview(val uris: String) : Navigation
} }
} }
......
package com.isidroid.c23.ui.screen.map 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.ViewEvent
import com.isidroid.core.vm.ViewSideEffect import com.isidroid.core.vm.ViewSideEffect
import com.isidroid.core.vm.ViewState import com.isidroid.core.vm.ViewState
...@@ -18,6 +19,7 @@ class MapContract { ...@@ -18,6 +19,7 @@ class MapContract {
sealed interface Navigation : Effect { sealed interface Navigation : Effect {
data object ToBack: Navigation data object ToBack: Navigation
data object ToSelectContent: Navigation data object ToSelectContent: Navigation
data class ToPreview(val uris: String) : Navigation
} }
} }
......
...@@ -32,6 +32,7 @@ class MapViewModel @Inject constructor( ...@@ -32,6 +32,7 @@ class MapViewModel @Inject constructor(
private var _findSpotsJob: Job? = null private var _findSpotsJob: Job? = null
private val _data = hashMapOf<String, RichSpot>() private val _data = hashMapOf<String, RichSpot>()
private var _markers = hashMapOf<String, List<RichSpot>>() private var _markers = hashMapOf<String, List<RichSpot>>()
private var _uris: String? = null
private val _spotResultStateFlow = MutableStateFlow<List<MapMarker>>(emptyList()) private val _spotResultStateFlow = MutableStateFlow<List<MapMarker>>(emptyList())
val spotResultStateFlow = _spotResultStateFlow.asStateFlow() val spotResultStateFlow = _spotResultStateFlow.asStateFlow()
...@@ -58,9 +59,15 @@ class MapViewModel @Inject constructor( ...@@ -58,9 +59,15 @@ class MapViewModel @Inject constructor(
val flowLat = savedStateHandle.getStateFlow<String?>(Argument.LATITUDE, null) val flowLat = savedStateHandle.getStateFlow<String?>(Argument.LATITUDE, null)
val flowLng = savedStateHandle.getStateFlow<String?>(Argument.LONGITUDE, null) val flowLng = savedStateHandle.getStateFlow<String?>(Argument.LONGITUDE, null)
val flowSpotCode = savedStateHandle.getStateFlow<String?>(Argument.SPOT_CODE, 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 -> combine(flowLat, flowLng, flowSpotCode, flowUris) { lat, lng, spotCode, uris ->
prepareData(lat?.toDoubleOrNull(), lng?.toDoubleOrNull(), spotCode) _uris = uris
prepareData(
lat = lat?.toDoubleOrNull(),
lng = lng?.toDoubleOrNull(),
spotCode = spotCode
)
}.collect() }.collect()
} }
...@@ -98,7 +105,7 @@ class MapViewModel @Inject constructor( ...@@ -98,7 +105,7 @@ class MapViewModel @Inject constructor(
private suspend fun selectSpot(id: String) { private suspend fun selectSpot(id: String) {
useCase.selectSpot(_data[id]) useCase.selectSpot(_data[id])
.flowOn(Dispatchers.IO) .flowOn(Dispatchers.IO)
.collect { setEffect { MapContract.Effect.Navigation.ToSelectContent } } .collect { onSpotSelected() }
} }
private suspend fun moveToMyLocation() { private suspend fun moveToMyLocation() {
...@@ -123,4 +130,11 @@ class MapViewModel @Inject constructor( ...@@ -123,4 +130,11 @@ class MapViewModel @Inject constructor(
_spotResultStateFlow.emit(markers) _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 ...@@ -9,6 +9,7 @@ import timber.log.Timber
fun NavController.navigateSingleTopTo( fun NavController.navigateSingleTopTo(
route: String, route: String,
dropCurrent: Boolean = true,
isSaveState: Boolean? = null, isSaveState: Boolean? = null,
isLaunchSingleTop: Boolean = true, isLaunchSingleTop: Boolean = true,
isRestoreState: Boolean? = null, isRestoreState: Boolean? = null,
...@@ -16,7 +17,9 @@ fun NavController.navigateSingleTopTo( ...@@ -16,7 +17,9 @@ fun NavController.navigateSingleTopTo(
popupToRoute: String? = null popupToRoute: String? = null
) { ) {
navigate(route) { 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) { popUpTo(node.id) {
inclusive = isInclusive inclusive = isInclusive
if (isSaveState != null) 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