refactor(android): use in-built navigation backstack State

This commit is contained in:
Harsh Shandilya 2022-09-04 20:11:27 +05:30 committed by GitHub
parent 7a86842ef3
commit ed8402034e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 11 deletions

View file

@ -64,7 +64,7 @@ fun LobstersApp(
val navController = rememberMaterialMotionNavController() val navController = rememberMaterialMotionNavController()
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val postActions = rememberPostActions(urlLauncher, navController, viewModel) val postActions = rememberPostActions(urlLauncher, navController, viewModel)
val currentDestination by currentNavigationDestination(navController) val currentDestination = currentNavigationDestination(navController)
val context = LocalContext.current val context = LocalContext.current
val hottestPosts = viewModel.hottestPosts.collectAsLazyPagingItems() val hottestPosts = viewModel.hottestPosts.collectAsLazyPagingItems()

View file

@ -9,10 +9,10 @@ import androidx.compose.material3.ColorScheme
import androidx.compose.material3.dynamicDarkColorScheme import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.State import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import dev.msfjarvis.claw.android.ui.navigation.Destinations import dev.msfjarvis.claw.android.ui.navigation.Destinations
import dev.msfjarvis.claw.android.viewmodel.ClawViewModel import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
import dev.msfjarvis.claw.common.posts.PostActions import dev.msfjarvis.claw.common.posts.PostActions
@ -41,15 +41,10 @@ fun String.toLocalDateTime(): LocalDateTime {
return Instant.parse(this).toLocalDateTime(TimeZone.currentSystemDefault()) return Instant.parse(this).toLocalDateTime(TimeZone.currentSystemDefault())
} }
// The destination needs to be tracked like this rather than used directly since
// `NavController#currentDestination` is not a Composable state.
@Composable @Composable
fun currentNavigationDestination(navController: NavController): State<String?> { fun currentNavigationDestination(navController: NavController): String? {
val currentDestination = remember { mutableStateOf<String?>(null) } val backStackEntry by navController.currentBackStackEntryAsState()
navController.addOnDestinationChangedListener { _, destination, _ -> return backStackEntry?.destination?.route
currentDestination.value = destination.route
}
return currentDestination
} }
@Composable @Composable