3 Commits
Author SHA1 Message Date
OneWay dcae63056c 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.
2026-11-02 16:39:11 +08:00
OneWay 98b23bac48 fix(search): expire stale results after an empty query
Stale rows from an earlier query flashed during the next load.
Clear the cache when a finished query has no results.
2026-11-02 01:33:24 +08:00
OneWay c440fc5024 fix(import): expire the sticky import error state
A failed local import left the error in the state flow forever, so the
dictionaries section replayed it as a snackbar on every recomposition
and screens created later picked it up long after the failure. Terminal
import errors now decay to idle after the same grace period as
downloads, unless a newer state has replaced them.
2026-11-01 21:01:43 +08:00
5 changed files with 34 additions and 12 deletions

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)
} }
} }
} }
@@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.*
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
import java.io.File import java.io.File
import java.util.Collections import java.util.Collections
import kotlin.time.Duration.Companion.seconds
class DictionaryImportManager( class DictionaryImportManager(
private val application: Application private val application: Application
@@ -19,6 +20,10 @@ class DictionaryImportManager(
private val mutableImportState = MutableStateFlow<ImportState>(ImportState.Idle) private val mutableImportState = MutableStateFlow<ImportState>(ImportState.Idle)
val importState: StateFlow<ImportState> = mutableImportState.asStateFlow() val importState: StateFlow<ImportState> = mutableImportState.asStateFlow()
companion object {
private val TERMINAL_STATE_DURATION = 2.seconds
}
private val managerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main) private val managerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
private var importJob: Job? = null private var importJob: Job? = null
private val importedFiles = Collections.synchronizedList(mutableListOf<File>()) private val importedFiles = Collections.synchronizedList(mutableListOf<File>())
@@ -130,6 +135,9 @@ class DictionaryImportManager(
) )
mutableImportState.value = terminalState mutableImportState.value = terminalState
onTerminal?.invoke(terminalState) onTerminal?.invoke(terminalState)
if (terminalState is ImportState.Error) {
expireErrorStateToIdle(terminalState)
}
} catch (e: CancellationException) { } catch (e: CancellationException) {
cleanupImportedFiles() cleanupImportedFiles()
@@ -141,9 +149,17 @@ class DictionaryImportManager(
) )
mutableImportState.value = errorState mutableImportState.value = errorState
onTerminal?.invoke(errorState) onTerminal?.invoke(errorState)
expireErrorStateToIdle(errorState)
} }
} }
private suspend fun expireErrorStateToIdle(errorState: ImportState.Error) {
delay(TERMINAL_STATE_DURATION)
if (mutableImportState.value === errorState) {
mutableImportState.value = ImportState.Idle
}
}
private fun getFileName(uri: Uri): String? { private fun getFileName(uri: Uri): String? {
var fileName: String? = null var fileName: String? = null
application.contentResolver.query(uri, null, null, null, null)?.use { cursor -> application.contentResolver.query(uri, null, null, null, null)?.use { cursor ->
@@ -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()
@@ -89,8 +89,8 @@ internal fun SearchResultsPane(
SideEffect { SideEffect {
val loaded = results.itemSnapshotList.items val loaded = results.itemSnapshotList.items
if (results.loadState.refresh is LoadState.NotLoading && loaded.isNotEmpty()) { if (results.loadState.refresh is LoadState.NotLoading) {
staleEntries = loaded staleEntries = if (loaded.isNotEmpty()) loaded else emptyList()
} }
} }
val pendingReload = results.itemCount == 0 && val pendingReload = results.itemCount == 0 &&