diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/extensions/SavedStateHandleExt.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/extensions/SavedStateHandleExt.kt new file mode 100644 index 000000000..61fe3a7d5 --- /dev/null +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/extensions/SavedStateHandleExt.kt @@ -0,0 +1,11 @@ +package co.nimblehq.sample.compose.extensions + +import androidx.lifecycle.SavedStateHandle + +fun SavedStateHandle.getThenRemove(key: String): T? { + return if (contains(key)) { + val value = get(key) + remove(key) + value + } else null +} diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/AppNavGraph.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/AppNavGraph.kt index fe41f58a7..b9a9048a6 100644 --- a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/AppNavGraph.kt +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/AppNavGraph.kt @@ -11,7 +11,7 @@ import co.nimblehq.sample.compose.ui.base.BaseDestination import co.nimblehq.sample.compose.ui.screens.main.mainNavGraph @Composable -fun AppNavigation( +fun AppNavGraph( navController: NavHostController, ) { NavHost( @@ -44,7 +44,12 @@ fun NavGraphBuilder.composable( */ fun NavHostController.navigate(destination: BaseDestination, parcel: Pair? = null) { when (destination) { - is BaseDestination.Up -> navigateUp() + is BaseDestination.Up -> { + destination.results.forEach { (key, value) -> + previousBackStackEntry?.savedStateHandle?.set(key, value) + } + navigateUp() + } else -> { parcel?.let { (key, value) -> currentBackStackEntry?.savedStateHandle?.set(key, value) diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/base/BaseDestination.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/base/BaseDestination.kt index e7f3fe7ff..b8eae02ba 100644 --- a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/base/BaseDestination.kt +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/base/BaseDestination.kt @@ -2,6 +2,8 @@ package co.nimblehq.sample.compose.ui.base import androidx.navigation.NamedNavArgument +const val KeyResultOk = "keyResultOk" + abstract class BaseDestination(val route: String = "") { open val arguments: List = emptyList() @@ -10,5 +12,10 @@ abstract class BaseDestination(val route: String = "") { open var parcelableArgument: Pair = "" to null - object Up : BaseDestination() + data class Up(val results: HashMap = hashMapOf()) : BaseDestination() { + + fun addResult(key: String, value: Any) = apply { + results[key] = value + } + } } diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/MainActivity.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/MainActivity.kt index 28a6ea41a..5c262262b 100644 --- a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/MainActivity.kt +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/MainActivity.kt @@ -4,7 +4,7 @@ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.navigation.compose.rememberNavController -import co.nimblehq.sample.compose.ui.AppNavigation +import co.nimblehq.sample.compose.ui.AppNavGraph import co.nimblehq.sample.compose.ui.theme.ComposeTheme import dagger.hilt.android.AndroidEntryPoint @@ -15,7 +15,7 @@ class MainActivity : ComponentActivity() { super.onCreate(savedInstanceState) setContent { ComposeTheme { - AppNavigation(navController = rememberNavController()) + AppNavGraph(navController = rememberNavController()) } } } diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/MainNavGraph.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/MainNavGraph.kt index 0ff9380f7..1632ceaad 100644 --- a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/MainNavGraph.kt +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/MainNavGraph.kt @@ -3,7 +3,9 @@ package co.nimblehq.sample.compose.ui.screens.main import androidx.navigation.NavGraphBuilder import androidx.navigation.NavHostController import androidx.navigation.navigation +import co.nimblehq.sample.compose.extensions.getThenRemove import co.nimblehq.sample.compose.ui.AppDestination +import co.nimblehq.sample.compose.ui.base.KeyResultOk import co.nimblehq.sample.compose.ui.composable import co.nimblehq.sample.compose.ui.models.UiModel import co.nimblehq.sample.compose.ui.navigate @@ -18,11 +20,14 @@ fun NavGraphBuilder.mainNavGraph( route = AppDestination.MainNavGraph.route, startDestination = MainDestination.Home.destination ) { - composable(destination = MainDestination.Home) { + composable(destination = MainDestination.Home) { backStackEntry -> + val isResultOk = backStackEntry.savedStateHandle + .getThenRemove(KeyResultOk) ?: false HomeScreen( navigator = { destination -> navController.navigate(destination, destination.parcelableArgument) - } + }, + isResultOk = isResultOk, ) } diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/home/HomeScreen.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/home/HomeScreen.kt index fe2c70f22..b63436bcc 100644 --- a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/home/HomeScreen.kt +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/home/HomeScreen.kt @@ -27,6 +27,7 @@ import kotlinx.coroutines.flow.* fun HomeScreen( viewModel: HomeViewModel = hiltViewModel(), navigator: (destination: BaseDestination) -> Unit, + isResultOk: Boolean = false, ) { val context = LocalContext.current viewModel.error.collectAsEffect { e -> e.showToast(context) } @@ -43,6 +44,12 @@ fun HomeScreen( } } + LaunchedEffect(Unit) { + if (isResultOk) { + context.showToast(context.getString(R.string.message_updated)) + } + } + CameraPermission() HomeScreenContent( diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/second/SecondScreen.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/second/SecondScreen.kt index 7fc436a88..605d093db 100644 --- a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/second/SecondScreen.kt +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/second/SecondScreen.kt @@ -1,8 +1,10 @@ package co.nimblehq.sample.compose.ui.screens.main.second import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding +import androidx.compose.material.Button import androidx.compose.material.Scaffold import androidx.compose.material.Text import androidx.compose.runtime.Composable @@ -13,7 +15,9 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.hilt.navigation.compose.hiltViewModel import co.nimblehq.sample.compose.R import co.nimblehq.sample.compose.ui.base.BaseDestination +import co.nimblehq.sample.compose.ui.base.KeyResultOk import co.nimblehq.sample.compose.ui.common.AppBar +import co.nimblehq.sample.compose.ui.theme.AppTheme.dimensions import co.nimblehq.sample.compose.ui.theme.ComposeTheme @Composable @@ -22,11 +26,19 @@ fun SecondScreen( navigator: (destination: BaseDestination) -> Unit, id: String, ) { - SecondScreenContent(id) + SecondScreenContent( + id = id, + onUpdateClick = { + navigator(BaseDestination.Up().addResult(KeyResultOk, true)) + }, + ) } @Composable -private fun SecondScreenContent(id: String) { +private fun SecondScreenContent( + id: String, + onUpdateClick: () -> Unit, +) { Scaffold(topBar = { AppBar(R.string.second_title_bar) }) { paddingValues -> @@ -35,10 +47,20 @@ private fun SecondScreenContent(id: String) { .fillMaxSize() .padding(paddingValues) ) { - Text( - text = stringResource(R.string.second_id_title, id), - modifier = Modifier.align(Alignment.Center) - ) + Column(modifier = Modifier.align(Alignment.Center)) { + Text( + text = stringResource(R.string.second_id_title, id), + ) + + Button( + onClick = { onUpdateClick() }, + modifier = Modifier.padding(dimensions.spacingMedium) + ) { + Text( + text = stringResource(R.string.second_update) + ) + } + } } } } @@ -47,6 +69,9 @@ private fun SecondScreenContent(id: String) { @Composable private fun SecondScreenPreview() { ComposeTheme { - SecondScreenContent("1") + SecondScreenContent( + id = "1", + onUpdateClick = {}, + ) } } diff --git a/sample-compose/app/src/main/res/values/strings.xml b/sample-compose/app/src/main/res/values/strings.xml index 06743934e..d21fee171 100644 --- a/sample-compose/app/src/main/res/values/strings.xml +++ b/sample-compose/app/src/main/res/values/strings.xml @@ -7,10 +7,12 @@ Second ID: %1$s + Update Third Data: %s Edit This is the first time launch + Updated! diff --git a/template-compose/app/src/main/java/co/nimblehq/template/compose/extensions/SavedStateHandleExt.kt b/template-compose/app/src/main/java/co/nimblehq/template/compose/extensions/SavedStateHandleExt.kt new file mode 100644 index 000000000..c041c5545 --- /dev/null +++ b/template-compose/app/src/main/java/co/nimblehq/template/compose/extensions/SavedStateHandleExt.kt @@ -0,0 +1,11 @@ +package co.nimblehq.template.compose.extensions + +import androidx.lifecycle.SavedStateHandle + +fun SavedStateHandle.getThenRemove(key: String): T? { + return if (contains(key)) { + val value = get(key) + remove(key) + value + } else null +} diff --git a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/AppNavGraph.kt b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/AppNavGraph.kt index f2b975f97..6dd922c48 100644 --- a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/AppNavGraph.kt +++ b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/AppNavGraph.kt @@ -11,7 +11,7 @@ import co.nimblehq.template.compose.ui.base.BaseDestination import co.nimblehq.template.compose.ui.screens.main.mainNavGraph @Composable -fun AppNavigation( +fun AppNavGraph( navController: NavHostController, ) { NavHost( @@ -38,7 +38,12 @@ fun NavGraphBuilder.composable( fun NavHostController.navigate(destination: BaseDestination) { when (destination) { - is BaseDestination.Up -> navigateUp() + is BaseDestination.Up -> { + destination.results.forEach { (key, value) -> + previousBackStackEntry?.savedStateHandle?.set(key, value) + } + navigateUp() + } else -> navigate(route = destination.destination) } } diff --git a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/base/BaseDestination.kt b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/base/BaseDestination.kt index 3247e7561..a56152cfa 100644 --- a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/base/BaseDestination.kt +++ b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/base/BaseDestination.kt @@ -8,5 +8,10 @@ abstract class BaseDestination(val route: String = "") { open var destination: String = route - object Up : BaseDestination() + data class Up(val results: HashMap = hashMapOf()) : BaseDestination() { + + fun addResult(key: String, value: Any) = apply { + results[key] = value + } + } } diff --git a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/MainActivity.kt b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/MainActivity.kt index 87759c1be..0b9a609ef 100644 --- a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/MainActivity.kt +++ b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/MainActivity.kt @@ -4,7 +4,7 @@ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.navigation.compose.rememberNavController -import co.nimblehq.template.compose.ui.AppNavigation +import co.nimblehq.template.compose.ui.AppNavGraph import co.nimblehq.template.compose.ui.theme.ComposeTheme import dagger.hilt.android.AndroidEntryPoint @@ -15,7 +15,7 @@ class MainActivity : ComponentActivity() { super.onCreate(savedInstanceState) setContent { ComposeTheme { - AppNavigation(navController = rememberNavController()) + AppNavGraph(navController = rememberNavController()) } } }