From f7c6330910e9e5a1b54f8b66ca937262ce8d9cd0 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 2 Mar 2021 09:08:17 +0530 Subject: [PATCH] LobstersApp: lower coroutine scope further in the view tree Signed-off-by: Harsh Shandilya --- .../dev/msfjarvis/lobsters/ui/main/LobstersApp.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/dev/msfjarvis/lobsters/ui/main/LobstersApp.kt b/app/src/main/java/dev/msfjarvis/lobsters/ui/main/LobstersApp.kt index 958108ab..0febc3df 100644 --- a/app/src/main/java/dev/msfjarvis/lobsters/ui/main/LobstersApp.kt +++ b/app/src/main/java/dev/msfjarvis/lobsters/ui/main/LobstersApp.kt @@ -31,7 +31,6 @@ import kotlinx.coroutines.launch @Composable fun LobstersApp() { val viewModel: LobstersViewModel = viewModel() - val coroutineScope = rememberCoroutineScope() val navController = rememberNavController() val hottestPosts = viewModel.posts.collectAsLazyPagingItems() val savedPosts by viewModel.savedPosts.collectAsState() @@ -47,10 +46,8 @@ fun LobstersApp() { popUpTo(navController.graph.startDestination) { inclusive = false } } } - val jumpToIndex: (Int) -> Unit = { - coroutineScope.launch { - hottestPostsListState.animateScrollToItem(it) - } + val jumpToIndex: suspend (Int) -> Unit = { + hottestPostsListState.animateScrollToItem(it) } Scaffold( @@ -87,8 +84,9 @@ fun LobstersApp() { fun LobstersBottomNav( currentDestination: Destination, navigateToDestination: (destination: Destination) -> Unit, - jumpToIndex: (index: Int) -> Unit, + jumpToIndex: suspend (index: Int) -> Unit, ) { + val coroutineScope = rememberCoroutineScope() BottomNavigation(modifier = Modifier.testTag("LobstersBottomNav")) { Destination.values().forEach { screen -> BottomNavigationItem( @@ -106,7 +104,7 @@ fun LobstersBottomNav( if (screen != currentDestination) { navigateToDestination(screen) } else if (screen.route == Destination.Hottest.route) { - jumpToIndex(0) + coroutineScope.launch { jumpToIndex(0) } } } )