From 93e96c388cb113faf8868c93f6747ab7a441e6c5 Mon Sep 17 00:00:00 2001 From: DianQK Date: Wed, 1 Feb 2023 22:05:35 +0800 Subject: [PATCH] Show keyboard only on the first load of the search page --- .../java/org/dianqk/ruslin/ui/page/search/SearchPage.kt | 9 +++++++-- .../org/dianqk/ruslin/ui/page/search/SearchViewModel.kt | 9 ++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/dianqk/ruslin/ui/page/search/SearchPage.kt b/app/src/main/java/org/dianqk/ruslin/ui/page/search/SearchPage.kt index ce627aa..e07e858 100644 --- a/app/src/main/java/org/dianqk/ruslin/ui/page/search/SearchPage.kt +++ b/app/src/main/java/org/dianqk/ruslin/ui/page/search/SearchPage.kt @@ -47,6 +47,7 @@ fun SearchPage( val focusRequest = remember { FocusRequester() } val lazyListState = rememberLazyListState() val keyboardController = LocalSoftwareKeyboardController.current + Scaffold( modifier = Modifier, topBar = { @@ -74,6 +75,7 @@ fun SearchPage( keyboardActions = KeyboardActions( onSearch = { viewModel.search() + focusRequest.freeFocus() } ) ) @@ -114,8 +116,11 @@ fun SearchPage( keyboardController?.hide() } - LaunchedEffect(Unit) { - focusRequest.requestFocus() + if (uiState.showKeyboardOnFirstLoad) { + LaunchedEffect(Unit) { + focusRequest.requestFocus() + viewModel.hasShownKeyboardOnFirstLoad() + } } } diff --git a/app/src/main/java/org/dianqk/ruslin/ui/page/search/SearchViewModel.kt b/app/src/main/java/org/dianqk/ruslin/ui/page/search/SearchViewModel.kt index 972d4db..75e5f4b 100644 --- a/app/src/main/java/org/dianqk/ruslin/ui/page/search/SearchViewModel.kt +++ b/app/src/main/java/org/dianqk/ruslin/ui/page/search/SearchViewModel.kt @@ -18,7 +18,8 @@ data class SearchUiState( val searchingTerm: String = "", val isSearching: Boolean = false, val searchedNotes: List = emptyList(), - val notFound: Boolean = false + val notFound: Boolean = false, + val showKeyboardOnFirstLoad: Boolean = true, ) @HiltViewModel @@ -40,6 +41,12 @@ class SearchViewModel @Inject constructor( } } + fun hasShownKeyboardOnFirstLoad() { + _uiState.update { + it.copy(showKeyboardOnFirstLoad = false) + } + } + fun search() { if (_uiState.value.isSearching) { return