Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SAUL committed Nov 20, 2024
1 parent d0f8658 commit 58f567e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/ui/screens/SecVaultScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SecVaultScreen : Screen {
val toaster = rememberToasterState()

LaunchedEffect(Unit) {
screenModel.init()
screenModel.loadInitialPasswords()
}

Toaster(
Expand Down
42 changes: 25 additions & 17 deletions src/main/kotlin/viewmodel/SecVaultScreenModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import core.models.PasswordSort
import core.models.Result
import core.models.UiState
import core.models.criteria.PasswordSearchCriteria
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.slf4j.LoggerFactory
import repository.password.PasswordRepository
import repository.password.projection.PasswordSummary
Expand Down Expand Up @@ -42,6 +40,12 @@ class SecVaultScreenModel(
private val _passwordItems = MutableStateFlow<List<PasswordSummary>>(emptyList())
val passwordItems: StateFlow<List<PasswordSummary>> = _passwordItems.asStateFlow()

init {
observeMenuItemSelection()
observeSortItemSelection()
loadInitialPasswords()
}

fun selectMenuItem(item: DefaultMenuItem) {
_selectedMenuItem.value = item
}
Expand All @@ -54,24 +58,11 @@ class SecVaultScreenModel(
_secVaultState.value = UiState.Idle
}

fun init() {
fun loadInitialPasswords() {
screenModelScope.launch(dispatcher) {
_secVaultState.value = UiState.Loading
loadPasswords(PasswordSort.NAME)
}

screenModelScope.launch(dispatcher) {
_selectedMenuItem.collect { newMenuItem ->
LoggerFactory.getLogger(SecVaultScreenModel::class.java).info(newMenuItem.value)
}
}

screenModelScope.launch(dispatcher) {
_selectedSortItem.collect { newFilterOption ->
_secVaultState.value = UiState.Loading
loadPasswords(newFilterOption)
}
}
}

private suspend fun loadPasswords(sort: PasswordSort) {
Expand All @@ -89,4 +80,21 @@ class SecVaultScreenModel(
}
}

private fun observeMenuItemSelection() {
screenModelScope.launch(dispatcher) {
_selectedMenuItem.collect { newMenuItem ->
LoggerFactory.getLogger(SecVaultScreenModel::class.java).info(newMenuItem.value)
}
}
}

private fun observeSortItemSelection() {
screenModelScope.launch(dispatcher) {
_selectedSortItem.collect { newSortItem ->
_secVaultState.value = UiState.Loading
loadPasswords(newSortItem)
}
}
}

}

0 comments on commit 58f567e

Please sign in to comment.