android: migrate away from Accompanist Insets

This commit is contained in:
Harsh Shandilya 2022-03-28 14:26:20 +05:30
parent edd537b351
commit 9f7a5446f8
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
3 changed files with 102 additions and 109 deletions

View file

@ -55,7 +55,6 @@ dependencies {
implementation(projects.common)
implementation(projects.database)
implementation(compose.material3)
implementation(libs.accompanist.insets)
implementation(libs.accompanist.swiperefresh)
implementation(libs.accompanist.sysuicontroller)
implementation(libs.androidx.activity.compose)

View file

@ -1,5 +1,6 @@
package dev.msfjarvis.claw.android.ui
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
@ -31,9 +32,6 @@ import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import androidx.navigation.navDeepLink
import androidx.paging.compose.collectAsLazyPagingItems
import com.google.accompanist.insets.ProvideWindowInsets
import com.google.accompanist.insets.navigationBarsPadding
import com.google.accompanist.insets.statusBarsPadding
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import dev.msfjarvis.claw.android.R
import dev.msfjarvis.claw.android.ui.decorations.ClawAppBar
@ -76,115 +74,112 @@ fun LobstersApp(
LocalHTMLConverter provides htmlConverter,
colorScheme = decideColorScheme(LocalContext.current),
) {
ProvideWindowInsets {
val currentUiMode = LocalConfiguration.current.uiMode
val systemBarsColor = MaterialTheme.colorScheme.surfaceColorAtNavigationBarElevation()
val backgroundColor = MaterialTheme.colorScheme.background
val navItems =
listOf(
NavigationItem(
label = "Hottest",
route = Destinations.Hottest.getRoute(),
icon = painterResource(R.drawable.ic_whatshot_24dp),
) { coroutineScope.launch { networkListState.animateScrollToItem(index = 0) } },
NavigationItem(
label = "Saved",
route = Destinations.Saved.getRoute(),
icon = painterResource(commonR.drawable.ic_favorite_24dp),
) { coroutineScope.launch { savedListState.animateScrollToItem(index = 0) } },
)
val currentUiMode = LocalConfiguration.current.uiMode
val systemBarsColor = MaterialTheme.colorScheme.surfaceColorAtNavigationBarElevation()
val backgroundColor = MaterialTheme.colorScheme.background
val navItems =
listOf(
NavigationItem(
label = "Hottest",
route = Destinations.Hottest.getRoute(),
icon = painterResource(R.drawable.ic_whatshot_24dp),
) { coroutineScope.launch { networkListState.animateScrollToItem(index = 0) } },
NavigationItem(
label = "Saved",
route = Destinations.Saved.getRoute(),
icon = painterResource(commonR.drawable.ic_favorite_24dp),
) { coroutineScope.launch { savedListState.animateScrollToItem(index = 0) } },
)
SideEffect { systemUiController.setStatusBarColor(color = systemBarsColor) }
SideEffect { systemUiController.setStatusBarColor(color = systemBarsColor) }
// Track UI mode as a key to force a navbar color update when dark theme is toggled
LaunchedEffect(currentDestination, currentUiMode) {
val color =
if (navItems.none { item -> item.route == currentDestination }) {
backgroundColor
} else {
systemBarsColor
}
systemUiController.setNavigationBarColor(color = color)
}
// Track UI mode as a key to force a navbar color update when dark theme is toggled
LaunchedEffect(currentDestination, currentUiMode) {
val color =
if (navItems.none { item -> item.route == currentDestination }) {
backgroundColor
} else {
systemBarsColor
}
systemUiController.setNavigationBarColor(color = color)
}
Scaffold(
topBar = {
ClawAppBar(
modifier = Modifier.statusBarsPadding(),
backgroundColor = systemBarsColor,
navigationIcon = {
if (navItems.none { it.route == currentDestination }) {
IconButton(onClick = { navController.popBackStack() }) {
Icon(
imageVector = Icons.Filled.ArrowBack,
contentDescription = "Go back to previous screen",
)
}
}
},
title = {
if (navItems.any { it.route == currentDestination }) {
Text(text = stringResource(commonR.string.app_name), fontWeight = FontWeight.Bold)
Scaffold(
topBar = {
ClawAppBar(
backgroundColor = systemBarsColor,
navigationIcon = {
if (navItems.none { it.route == currentDestination }) {
IconButton(onClick = { navController.popBackStack() }) {
Icon(
imageVector = Icons.Filled.ArrowBack,
contentDescription = "Go back to previous screen",
)
}
},
)
},
bottomBar = {
ClawNavigationBar(
navController = navController,
items = navItems,
isVisible = navItems.any { it.route == currentDestination },
)
},
) { paddingValues ->
NavHost(
navController,
startDestination = Destinations.startDestination.getRoute(),
}
},
title = {
if (navItems.any { it.route == currentDestination }) {
Text(text = stringResource(commonR.string.app_name), fontWeight = FontWeight.Bold)
}
},
)
},
bottomBar = {
ClawNavigationBar(
navController = navController,
items = navItems,
isVisible = navItems.any { it.route == currentDestination },
)
},
) { paddingValues ->
NavHost(
navController,
startDestination = Destinations.startDestination.getRoute(),
) {
val uri = LobstersApi.BASE_URL
composable(
route = Destinations.Hottest.getRoute(),
deepLinks =
listOf(navDeepLink { uriPattern = uri }, navDeepLink { uriPattern = "$uri/" }),
) {
val uri = LobstersApi.BASE_URL
composable(
route = Destinations.Hottest.getRoute(),
deepLinks =
listOf(navDeepLink { uriPattern = uri }, navDeepLink { uriPattern = "$uri/" }),
) {
setWebUri("https://lobste.rs/")
HottestPosts(
items = networkPosts,
listState = networkListState,
isPostSaved = viewModel::isPostSaved,
reloadPosts = viewModel::reloadPosts,
postActions = postActions,
modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()),
)
}
composable(Destinations.Saved.getRoute()) {
setWebUri(null)
DatabasePosts(
items = savedPosts,
listState = savedListState,
isSaved = viewModel::isPostSaved,
postActions = postActions,
modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()),
)
}
composable(
route = Destinations.Comments.getRoute("{postId}"),
arguments = listOf(navArgument("postId") { type = NavType.StringType }),
deepLinks =
listOf(
navDeepLink { uriPattern = "$uri/s/{postId}/.*" },
navDeepLink { uriPattern = "$uri/s/{postId}" },
),
) { backStackEntry ->
val postId = requireNotNull(backStackEntry.arguments?.getString("postId"))
setWebUri("https://lobste.rs/s/$postId")
CommentsPage(
postId = postId,
getDetails = viewModel::getPostComments,
modifier = Modifier.navigationBarsPadding(),
postActions = postActions,
)
}
setWebUri("https://lobste.rs/")
HottestPosts(
items = networkPosts,
listState = networkListState,
isPostSaved = viewModel::isPostSaved,
reloadPosts = viewModel::reloadPosts,
postActions = postActions,
modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()),
)
}
composable(Destinations.Saved.getRoute()) {
setWebUri(null)
DatabasePosts(
items = savedPosts,
listState = savedListState,
isSaved = viewModel::isPostSaved,
postActions = postActions,
modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()),
)
}
composable(
route = Destinations.Comments.getRoute("{postId}"),
arguments = listOf(navArgument("postId") { type = NavType.StringType }),
deepLinks =
listOf(
navDeepLink { uriPattern = "$uri/s/{postId}/.*" },
navDeepLink { uriPattern = "$uri/s/{postId}" },
),
) { backStackEntry ->
val postId = requireNotNull(backStackEntry.arguments?.getString("postId"))
setWebUri("https://lobste.rs/s/$postId")
CommentsPage(
postId = postId,
getDetails = viewModel::getPostComments,
modifier = Modifier.navigationBarsPadding(),
postActions = postActions,
)
}
}
}