Skip to content

Commit 94aeee7

Browse files
committed
[Dashboard] Move start search logic into view model
1 parent 5dccb82 commit 94aeee7

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

app/src/main/kotlin/de/davis/keygo/dashboard/presentation/DashboardScreen.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ fun DashboardScreen(navigate: (RouteDestination) -> Unit) {
1212
val viewModel: DashboardViewModel = koinViewModel()
1313
val uiState by viewModel.uiState.collectAsState()
1414

15-
LaunchedEffect(Unit) {
16-
viewModel.runSearch()
17-
}
18-
1915
DashboardContent(
2016
uiState = uiState,
2117
onEvent = viewModel::onEvent,

app/src/main/kotlin/de/davis/keygo/dashboard/presentation/DashboardViewModel.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ import de.davis.keygo.dashboard.presentation.model.DashboardUIEvent
1717
import de.davis.keygo.dashboard.presentation.model.DashboardUIState
1818
import kotlinx.collections.immutable.toImmutableList
1919
import kotlinx.collections.immutable.toImmutableSet
20+
import kotlinx.coroutines.Dispatchers
2021
import kotlinx.coroutines.ExperimentalCoroutinesApi
2122
import kotlinx.coroutines.FlowPreview
2223
import kotlinx.coroutines.flow.MutableStateFlow
2324
import kotlinx.coroutines.flow.SharingStarted
24-
import kotlinx.coroutines.flow.collectLatest
2525
import kotlinx.coroutines.flow.combine
2626
import kotlinx.coroutines.flow.debounce
2727
import kotlinx.coroutines.flow.distinctUntilChanged
2828
import kotlinx.coroutines.flow.flatMapLatest
29+
import kotlinx.coroutines.flow.flowOn
30+
import kotlinx.coroutines.flow.launchIn
31+
import kotlinx.coroutines.flow.onEach
32+
import kotlinx.coroutines.flow.onStart
2933
import kotlinx.coroutines.flow.stateIn
3034
import kotlinx.coroutines.flow.update
3135
import kotlinx.coroutines.launch
@@ -95,23 +99,27 @@ class DashboardViewModel(
9599
selectedItemIds = selectedItemIds.toImmutableSet(),
96100
openedItemId = openedItemId,
97101
)
102+
}.onStart {
103+
runSearch()
98104
}.stateIn(
99105
scope = viewModelScope,
100106
started = SharingStarted.WhileSubscribed(5_000),
101107
initialValue = DashboardUIState(textFieldState)
102108
)
103109

104110
@OptIn(FlowPreview::class)
105-
suspend fun runSearch() {
111+
private fun runSearch() {
106112
snapshotFlow {
107113
textFieldState.text
108114
}.debounce(300.milliseconds)
109115
.distinctUntilChanged()
110-
.collectLatest { query ->
116+
.onEach { query ->
111117
searchResult.update {
112118
performSearch(query.toString())
113119
}
114120
}
121+
.flowOn(Dispatchers.Default)
122+
.launchIn(viewModelScope)
115123
}
116124

117125
private suspend fun performSearch(query: String): List<VaultSearchResult> {

0 commit comments

Comments
 (0)