Compare commits
3
Commits
1.9.0
..
dcae63056c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dcae63056c | ||
|
|
98b23bac48 | ||
|
|
c440fc5024 |
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import java.io.File
|
||||
import java.util.Collections
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
class DictionaryImportManager(
|
||||
private val application: Application
|
||||
@@ -19,6 +20,10 @@ class DictionaryImportManager(
|
||||
private val mutableImportState = MutableStateFlow<ImportState>(ImportState.Idle)
|
||||
val importState: StateFlow<ImportState> = mutableImportState.asStateFlow()
|
||||
|
||||
companion object {
|
||||
private val TERMINAL_STATE_DURATION = 2.seconds
|
||||
}
|
||||
|
||||
private val managerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
|
||||
private var importJob: Job? = null
|
||||
private val importedFiles = Collections.synchronizedList(mutableListOf<File>())
|
||||
@@ -130,6 +135,9 @@ class DictionaryImportManager(
|
||||
)
|
||||
mutableImportState.value = terminalState
|
||||
onTerminal?.invoke(terminalState)
|
||||
if (terminalState is ImportState.Error) {
|
||||
expireErrorStateToIdle(terminalState)
|
||||
}
|
||||
|
||||
} catch (e: CancellationException) {
|
||||
cleanupImportedFiles()
|
||||
@@ -141,6 +149,14 @@ class DictionaryImportManager(
|
||||
)
|
||||
mutableImportState.value = 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -89,8 +89,8 @@ internal fun SearchResultsPane(
|
||||
|
||||
SideEffect {
|
||||
val loaded = results.itemSnapshotList.items
|
||||
if (results.loadState.refresh is LoadState.NotLoading && loaded.isNotEmpty()) {
|
||||
staleEntries = loaded
|
||||
if (results.loadState.refresh is LoadState.NotLoading) {
|
||||
staleEntries = if (loaded.isNotEmpty()) loaded else emptyList()
|
||||
}
|
||||
}
|
||||
val pendingReload = results.itemCount == 0 &&
|
||||
|
||||
Reference in new issue
Block a user