Fix(TopAppBar): Fix the bug that the TopAppBar can be scrolled and hidden when the search box is opened

This commit is contained in:
2024-08-20 16:54:36 +08:00
parent da4c58b631
commit b115d3d598
3 changed files with 28 additions and 15 deletions

View File

@@ -9,11 +9,11 @@ import top.fatweb.oxygen.toolbox.ui.OxygenAppState
fun OxygenNavHost( fun OxygenNavHost(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
appState: OxygenAppState, appState: OxygenAppState,
onShowSnackbar: suspend (message: String, action: String?) -> Boolean,
startDestination: String, startDestination: String,
isVertical: Boolean, isVertical: Boolean,
searchValue: String, searchValue: String,
searchCount: Int searchCount: Int,
onShowSnackbar: suspend (message: String, action: String?) -> Boolean
) { ) {
val navController = appState.navController val navController = appState.navController
NavHost( NavHost(
@@ -41,13 +41,13 @@ fun OxygenNavHost(
onNavigateToToolView = navController::navigateToToolView, onNavigateToToolView = navController::navigateToToolView,
onNavigateToToolStore = { appState.navigateToTopLevelDestination(TopLevelDestination.ToolStore) } onNavigateToToolStore = { appState.navigateToTopLevelDestination(TopLevelDestination.ToolStore) }
) )
toolViewScreen(
onBackClick = navController::popBackStack
)
starScreen( starScreen(
isVertical = isVertical, isVertical = isVertical,
searchValue = searchValue, searchValue = searchValue,
onNavigateToToolView = navController::navigateToToolView onNavigateToToolView = navController::navigateToToolView
) )
toolViewScreen(
onBackClick = navController::popBackStack
)
} }
} }

View File

@@ -80,12 +80,18 @@ fun OxygenApp(appState: OxygenAppState) {
val noConnectMessage = stringResource(R.string.core_no_connect) val noConnectMessage = stringResource(R.string.core_no_connect)
val topAppBarScrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior() var canScroll by remember { mutableStateOf(true) }
val topAppBarScrollBehavior =
if (canScroll) TopAppBarDefaults.enterAlwaysScrollBehavior() else TopAppBarDefaults.pinnedScrollBehavior()
var activeSearch by remember { mutableStateOf(false) } var activeSearch by remember { mutableStateOf(false) }
var searchValue by remember { mutableStateOf("") } var searchValue by remember { mutableStateOf("") }
var searchCount by remember { mutableIntStateOf(0) } var searchCount by remember { mutableIntStateOf(0) }
LaunchedEffect(activeSearch) {
canScroll = !activeSearch
}
LaunchedEffect(destination) { LaunchedEffect(destination) {
activeSearch = false activeSearch = false
searchValue = "" searchValue = ""
@@ -194,20 +200,20 @@ fun OxygenApp(appState: OxygenAppState) {
OxygenNavHost( OxygenNavHost(
appState = appState, appState = appState,
onShowSnackbar = { message, action ->
snackbarHostState.showSnackbar(
message = message,
actionLabel = action,
duration = SnackbarDuration.Short
) == SnackbarResult.ActionPerformed
},
startDestination = when (appState.launchPageConfig) { startDestination = when (appState.launchPageConfig) {
LaunchPageConfig.Tools -> TOOLS_ROUTE LaunchPageConfig.Tools -> TOOLS_ROUTE
LaunchPageConfig.Star -> STAR_ROUTE LaunchPageConfig.Star -> STAR_ROUTE
}, },
isVertical = appState.shouldShowBottomBar, isVertical = appState.shouldShowBottomBar,
searchValue = searchValue, searchValue = searchValue,
searchCount = searchCount searchCount = searchCount,
onShowSnackbar = { message, action ->
snackbarHostState.showSnackbar(
message = message,
actionLabel = action,
duration = SnackbarDuration.Short
) == SnackbarResult.ActionPerformed
}
) )
} }
} }

View File

@@ -41,6 +41,7 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBarDefaults import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@@ -107,13 +108,19 @@ internal fun LibrariesScreen(
var dialogContent by remember { mutableStateOf("") } var dialogContent by remember { mutableStateOf("") }
var dialogUrl by remember { mutableStateOf("") } var dialogUrl by remember { mutableStateOf("") }
val topAppBarScrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior() var canScroll by remember { mutableStateOf(true) }
val topAppBarScrollBehavior =
if (canScroll) TopAppBarDefaults.enterAlwaysScrollBehavior() else TopAppBarDefaults.pinnedScrollBehavior()
val infiniteTransition = rememberInfiniteTransition(label = "infiniteTransition") val infiniteTransition = rememberInfiniteTransition(label = "infiniteTransition")
var activeSearch by remember { mutableStateOf(false) } var activeSearch by remember { mutableStateOf(false) }
var searchValue by remember { mutableStateOf("") } var searchValue by remember { mutableStateOf("") }
LaunchedEffect(activeSearch) {
canScroll = !activeSearch
}
Scaffold( Scaffold(
modifier = Modifier modifier = Modifier
.nestedScroll(topAppBarScrollBehavior.nestedScrollConnection), .nestedScroll(topAppBarScrollBehavior.nestedScrollConnection),