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:
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.SearchBarPosition
|
||||
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.ui.navigation.AppNavigation
|
||||
import com.example.research.ui.settings.SettingsViewModel
|
||||
@@ -144,7 +143,7 @@ class MainActivity : ComponentActivity() {
|
||||
val text = intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT)
|
||||
?: intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT_READONLY)
|
||||
text?.toString()?.let {
|
||||
searchViewModel.onAction(SearchAction.QueryChanged(it))
|
||||
searchViewModel.injectExternalQuery(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,9 @@ class SearchViewModel(
|
||||
)
|
||||
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)
|
||||
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() {
|
||||
indexWarmupJob?.cancel()
|
||||
indexWarmupJob = viewModelScope.launch(Dispatchers.Default) {
|
||||
|
||||
@@ -43,7 +43,6 @@ fun MainRoute(
|
||||
val searchHistory by searchViewModel.searchHistory.collectAsStateWithLifecycle()
|
||||
|
||||
val settingsState by settingsViewModel.uiState.collectAsStateWithLifecycle()
|
||||
val searchQuery by searchViewModel.searchQuery.collectAsStateWithLifecycle()
|
||||
val isSearchBarAtBottom = settingsState.searchBarPosition == SearchBarPosition.BOTTOM
|
||||
|
||||
val scrollPosition = remember(searchViewModel) {
|
||||
@@ -53,9 +52,9 @@ fun MainRoute(
|
||||
TextFieldState(initialText = searchViewModel.searchQuery.value)
|
||||
}
|
||||
|
||||
LaunchedEffect(searchState, searchQuery) {
|
||||
if (searchState.text.toString() != searchQuery) {
|
||||
searchState.edit { replace(0, length, searchQuery) }
|
||||
LaunchedEffect(searchViewModel) {
|
||||
searchViewModel.queryInjection.collect { query ->
|
||||
searchState.edit { replace(0, length, query) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,12 +94,11 @@ fun MainRoute(
|
||||
MainAction.ClearError -> searchViewModel.clearError()
|
||||
is MainAction.SaveScrollPosition ->
|
||||
searchViewModel.saveScrollPosition(action.index, action.offset)
|
||||
is MainAction.HistoryEntryClicked ->
|
||||
is MainAction.HistoryEntryClicked -> {
|
||||
searchState.edit { replace(0, length, action.query) }
|
||||
if (isWide) {
|
||||
searchViewModel.onSearchQueryChanged(action.query)
|
||||
searchViewModel.onAction(SearchAction.SearchAndLoadArticle(action.query))
|
||||
} else {
|
||||
searchViewModel.onSearchQueryChanged(action.query)
|
||||
}
|
||||
}
|
||||
is MainAction.RemoveHistoryEntry ->
|
||||
searchViewModel.removeSearchHistoryEntry(action.query)
|
||||
|
||||
Reference in new issue
Block a user