refactor(search): deliver external queries to the field as one-shot events

The screen synced the view model query back into the text field on
every query change. The text field state now owns the text. Typed
edits are pulled up to the view model; history clicks edit the field
state directly, and process-text queries arrive as one-shot events
delivered to the field.
This commit is contained in:
OneWay committed 2026-11-02 16:39:11 +08:00
1 parent 98b23bac48
commit dcae63056c
3 files changed
+16 -10

No files matched your search

@@ -28,7 +28,6 @@ import com.example.research.common.ui.theme.ReSearchTheme
import com.example.research.core.domain.model.AppTheme import com.example.research.core.domain.model.AppTheme
import com.example.research.core.domain.model.SearchBarPosition import com.example.research.core.domain.model.SearchBarPosition
import com.example.research.data.local.preferences.SectionExpansionStates import com.example.research.data.local.preferences.SectionExpansionStates
import com.example.research.feature.search.SearchAction
import com.example.research.feature.search.SearchViewModel import com.example.research.feature.search.SearchViewModel
import com.example.research.ui.navigation.AppNavigation import com.example.research.ui.navigation.AppNavigation
import com.example.research.ui.settings.SettingsViewModel import com.example.research.ui.settings.SettingsViewModel
@@ -144,7 +143,7 @@ class MainActivity : ComponentActivity() {
val text = intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT) val text = intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT)
?: intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT_READONLY) ?: intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT_READONLY)
text?.toString()?.let { text?.toString()?.let {
searchViewModel.onAction(SearchAction.QueryChanged(it)) searchViewModel.injectExternalQuery(it)
} }
} }
} }
@@ -44,6 +44,9 @@ class SearchViewModel(
) )
val searchQuery: StateFlow<String> = mutableSearchQuery.asStateFlow() val searchQuery: StateFlow<String> = mutableSearchQuery.asStateFlow()
private val mutableQueryInjection = MutableSharedFlow<String>(extraBufferCapacity = 1)
val queryInjection: SharedFlow<String> = mutableQueryInjection.asSharedFlow()
private val mutableSelectedArticle = MutableStateFlow<ArticleState?>(null) private val mutableSelectedArticle = MutableStateFlow<ArticleState?>(null)
val selectedArticle: StateFlow<ArticleState?> = mutableSelectedArticle.asStateFlow() val selectedArticle: StateFlow<ArticleState?> = mutableSelectedArticle.asStateFlow()
@@ -157,6 +160,12 @@ class SearchViewModel(
} }
} }
fun injectExternalQuery(query: String) {
val sanitized = query.sanitizeQuery().trim()
mutableQueryInjection.tryEmit(sanitized)
onSearchQueryChanged(sanitized)
}
private fun warmupIndexesInBackground() { private fun warmupIndexesInBackground() {
indexWarmupJob?.cancel() indexWarmupJob?.cancel()
indexWarmupJob = viewModelScope.launch(Dispatchers.Default) { indexWarmupJob = viewModelScope.launch(Dispatchers.Default) {
@@ -43,7 +43,6 @@ fun MainRoute(
val searchHistory by searchViewModel.searchHistory.collectAsStateWithLifecycle() val searchHistory by searchViewModel.searchHistory.collectAsStateWithLifecycle()
val settingsState by settingsViewModel.uiState.collectAsStateWithLifecycle() val settingsState by settingsViewModel.uiState.collectAsStateWithLifecycle()
val searchQuery by searchViewModel.searchQuery.collectAsStateWithLifecycle()
val isSearchBarAtBottom = settingsState.searchBarPosition == SearchBarPosition.BOTTOM val isSearchBarAtBottom = settingsState.searchBarPosition == SearchBarPosition.BOTTOM
val scrollPosition = remember(searchViewModel) { val scrollPosition = remember(searchViewModel) {
@@ -53,9 +52,9 @@ fun MainRoute(
TextFieldState(initialText = searchViewModel.searchQuery.value) TextFieldState(initialText = searchViewModel.searchQuery.value)
} }
LaunchedEffect(searchState, searchQuery) { LaunchedEffect(searchViewModel) {
if (searchState.text.toString() != searchQuery) { searchViewModel.queryInjection.collect { query ->
searchState.edit { replace(0, length, searchQuery) } searchState.edit { replace(0, length, query) }
} }
} }
@@ -95,13 +94,12 @@ fun MainRoute(
MainAction.ClearError -> searchViewModel.clearError() MainAction.ClearError -> searchViewModel.clearError()
is MainAction.SaveScrollPosition -> is MainAction.SaveScrollPosition ->
searchViewModel.saveScrollPosition(action.index, action.offset) searchViewModel.saveScrollPosition(action.index, action.offset)
is MainAction.HistoryEntryClicked -> is MainAction.HistoryEntryClicked -> {
searchState.edit { replace(0, length, action.query) }
if (isWide) { if (isWide) {
searchViewModel.onSearchQueryChanged(action.query)
searchViewModel.onAction(SearchAction.SearchAndLoadArticle(action.query)) searchViewModel.onAction(SearchAction.SearchAndLoadArticle(action.query))
} else {
searchViewModel.onSearchQueryChanged(action.query)
} }
}
is MainAction.RemoveHistoryEntry -> is MainAction.RemoveHistoryEntry ->
searchViewModel.removeSearchHistoryEntry(action.query) searchViewModel.removeSearchHistoryEntry(action.query)
MainAction.ClearHistory -> searchViewModel.clearSearchHistory() MainAction.ClearHistory -> searchViewModel.clearSearchHistory()