Skip to content

Commit 4a7a52f

Browse files
committed
feat: Hide BottomAppBar when scrollable list is not scrollable #256
This commit introduces the ability to hide the BottomAppBar when a scrollable list (like in LogScreen and ListManagerScreen) is not scrollable. It also refactors the code to achieve this: - Adds `LocalBottomBarBehavior` to provide the `BottomAppBarScrollBehavior` for child composables. - Adds a check in `LogScreen` and `ListManagerScreen` to monitor the list's scroll state. - When the list is not scrollable (neither can scroll backward nor forward), the BottomAppBar's height offset is reset to zero, effectively hiding it. - The code related to `LazyListState` has been refactored to use the `listState` variable consistently.
1 parent 1f386ca commit 4a7a52f

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

app/src/main/java/com/github/jing332/tts_server_android/compose/MainPager.kt

+15-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import androidx.compose.foundation.pager.HorizontalPager
99
import androidx.compose.foundation.pager.rememberPagerState
1010
import androidx.compose.material3.BottomAppBar
1111
import androidx.compose.material3.BottomAppBarDefaults
12+
import androidx.compose.material3.BottomAppBarScrollBehavior
1213
import androidx.compose.material3.ExperimentalMaterial3Api
1314
import androidx.compose.material3.NavigationBarDefaults
1415
import androidx.compose.material3.NavigationBarItem
1516
import androidx.compose.material3.Scaffold
1617
import androidx.compose.material3.Text
1718
import androidx.compose.runtime.Composable
19+
import androidx.compose.runtime.CompositionLocalProvider
20+
import androidx.compose.runtime.compositionLocalOf
1821
import androidx.compose.runtime.rememberCoroutineScope
1922
import androidx.compose.ui.Modifier
2023
import androidx.compose.ui.input.nestedscroll.nestedScroll
@@ -28,6 +31,11 @@ import com.github.jing332.tts_server_android.compose.systts.TtsLogScreen
2831
import com.github.jing332.tts_server_android.compose.systts.list.ListManagerScreen
2932
import kotlinx.coroutines.launch
3033

34+
35+
@OptIn(ExperimentalMaterial3Api::class)
36+
val LocalBottomBarBehavior =
37+
compositionLocalOf<BottomAppBarScrollBehavior>() { error("LocalBottomBarBehavior not initialized") }
38+
3139
@OptIn(
3240
ExperimentalFoundationApi::class, ExperimentalLayoutApi::class,
3341
ExperimentalMaterial3Api::class
@@ -83,11 +91,13 @@ fun MainPager(sharedVM: SharedViewModel) {
8391
state = pagerState,
8492
userScrollEnabled = false
8593
) { index ->
86-
when (index) {
87-
PagerDestination.SystemTts.index -> ListManagerScreen(sharedVM)
88-
PagerDestination.SystemTtsLog.index -> TtsLogScreen()
89-
PagerDestination.Settings.index -> SettingsScreen()
90-
PagerDestination.SystemTtsForwarder.index -> SystemTtsForwarderScreen()
94+
CompositionLocalProvider(LocalBottomBarBehavior provides scrollBehavior) {
95+
when (index) {
96+
PagerDestination.SystemTts.index -> ListManagerScreen(sharedVM)
97+
PagerDestination.SystemTtsLog.index -> TtsLogScreen()
98+
PagerDestination.Settings.index -> SettingsScreen()
99+
PagerDestination.SystemTtsForwarder.index -> SystemTtsForwarderScreen()
100+
}
91101
}
92102
}
93103
}

app/src/main/java/com/github/jing332/tts_server_android/compose/systts/LogScreen.kt

+15-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.compose.foundation.shape.CircleShape
2020
import androidx.compose.foundation.text.selection.SelectionContainer
2121
import androidx.compose.material.icons.Icons
2222
import androidx.compose.material.icons.filled.KeyboardDoubleArrowDown
23+
import androidx.compose.material3.ExperimentalMaterial3Api
2324
import androidx.compose.material3.FloatingActionButton
2425
import androidx.compose.material3.HorizontalDivider
2526
import androidx.compose.material3.Icon
@@ -44,22 +45,30 @@ import com.github.jing332.common.toArgb
4445
import com.github.jing332.common.toLogLevelChar
4546
import com.github.jing332.compose.ComposeExtensions.toAnnotatedString
4647
import com.github.jing332.tts_server_android.R
48+
import com.github.jing332.tts_server_android.compose.LocalBottomBarBehavior
4749
import kotlinx.coroutines.launch
4850

49-
@OptIn(ExperimentalFoundationApi::class)
51+
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)
5052
@Composable
5153
fun LogScreen(
5254
modifier: Modifier,
5355
list: List<LogEntry>,
54-
lazyListState: LazyListState = rememberLazyListState(),
56+
listState: LazyListState = rememberLazyListState(),
5557
) {
58+
val bottomBarBehavior = LocalBottomBarBehavior.current
59+
LaunchedEffect(listState.canScrollBackward || listState.canScrollForward) {
60+
if (!(listState.canScrollBackward || listState.canScrollForward)) {
61+
bottomBarBehavior.state.heightOffset = 0f
62+
}
63+
}
64+
5665
val scope = rememberCoroutineScope()
5766
val view = LocalView.current
5867
val context = LocalContext.current
5968
Box(modifier) {
6069
val isAtBottom by remember {
6170
derivedStateOf {
62-
val layoutInfo = lazyListState.layoutInfo
71+
val layoutInfo = listState.layoutInfo
6372
val visibleItemsInfo = layoutInfo.visibleItemsInfo
6473
if (layoutInfo.totalItemsCount <= 0) {
6574
true
@@ -73,7 +82,7 @@ fun LogScreen(
7382
LaunchedEffect(list.size) {
7483
if (isAtBottom && list.isNotEmpty())
7584
scope.launch {
76-
lazyListState.animateScrollToItem(list.size - 1)
85+
listState.animateScrollToItem(list.size - 1)
7786
}
7887
}
7988

@@ -87,7 +96,7 @@ fun LogScreen(
8796

8897
val darkTheme = isSystemInDarkTheme()
8998
SelectionContainer {
90-
LazyColumn(Modifier.fillMaxSize(), state = lazyListState) {
99+
LazyColumn(Modifier.fillMaxSize(), state = listState) {
91100
itemsIndexed(list, key = { index, _ -> index }) { index, log ->
92101
val style = MaterialTheme.typography.bodyMedium
93102
val spanned = remember {
@@ -130,7 +139,7 @@ fun LogScreen(
130139
onClick = {
131140
scope.launch {
132141
kotlin.runCatching {
133-
lazyListState.scrollToItem(list.size - 1)
142+
listState.scrollToItem(list.size - 1)
134143
}
135144
}
136145
}) {

app/src/main/java/com/github/jing332/tts_server_android/compose/systts/list/ListManagerScreen.kt

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import com.github.jing332.database.entities.systts.source.LocalTtsSource
4848
import com.github.jing332.database.entities.systts.source.PluginTtsSource
4949
import com.github.jing332.tts_server_android.AppLocale
5050
import com.github.jing332.tts_server_android.R
51+
import com.github.jing332.tts_server_android.compose.LocalBottomBarBehavior
5152
import com.github.jing332.tts_server_android.compose.LocalDrawerState
5253
import com.github.jing332.tts_server_android.compose.LocalNavController
5354
import com.github.jing332.tts_server_android.compose.SharedViewModel
@@ -271,6 +272,13 @@ internal fun ListManagerScreen(
271272
},
272273
) { paddingValues ->
273274
Box(Modifier.padding(top = paddingValues.calculateTopPadding())) {
275+
val bottomBarBehavior = LocalBottomBarBehavior.current
276+
LaunchedEffect(listState.canScrollBackward || listState.canScrollForward) {
277+
if (!(listState.canScrollBackward || listState.canScrollForward)) {
278+
bottomBarBehavior.state.heightOffset = 0f
279+
}
280+
}
281+
274282
LazyColumn(
275283
Modifier
276284
.fillMaxSize()

0 commit comments

Comments
 (0)