-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
571dcaf
commit 002527a
Showing
8 changed files
with
84 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 41 additions & 17 deletions
58
...rc/commonMain/kotlin/com/softartdev/notedelight/shared/presentation/main/MainViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,55 @@ | ||
package com.softartdev.notedelight.shared.presentation.main | ||
|
||
import com.softartdev.notedelight.shared.base.BaseStateViewModel | ||
import com.softartdev.notedelight.shared.db.NoteDAO | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.softartdev.notedelight.shared.db.SafeRepo | ||
import com.softartdev.notedelight.shared.navigation.AppNavGraph | ||
import com.softartdev.notedelight.shared.navigation.Router | ||
import io.github.aakira.napier.Napier | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.IO | ||
import kotlinx.coroutines.ensureActive | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.coroutines.launch | ||
|
||
class MainViewModel( | ||
private val safeRepo: SafeRepo, | ||
private val noteDAO: NoteDAO, | ||
) : BaseStateViewModel<NoteListResult>() { | ||
|
||
override val loadingResult: NoteListResult = NoteListResult.Loading | ||
private val router: Router | ||
) : ViewModel() { | ||
private val mutableStateFlow: MutableStateFlow<NoteListResult> = MutableStateFlow( | ||
value = NoteListResult.Loading | ||
) | ||
val stateFlow: StateFlow<NoteListResult> = mutableStateFlow | ||
|
||
init { | ||
safeRepo.relaunchListFlowCallback = this::updateNotes | ||
} | ||
|
||
fun updateNotes() = launch( | ||
useIdling = false, | ||
flow = safeRepo.noteDAO.listFlow.map(NoteListResult::Success) | ||
) | ||
fun updateNotes() = viewModelScope.launch(Dispatchers.Main) { | ||
safeRepo.noteDAO.listFlow | ||
.onStart { mutableStateFlow.value = NoteListResult.Loading } | ||
.map(transform = NoteListResult::Success) | ||
.onEach(action = mutableStateFlow::emit) | ||
.flowOn(Dispatchers.IO) | ||
.catch { throwable -> | ||
Napier.e("❌", throwable) | ||
val errorName: String = throwable::class.simpleName.orEmpty() | ||
if (errorName.contains("SQLite")) { | ||
router.navigateClearingBackStack(AppNavGraph.SignIn.name) | ||
} else { | ||
mutableStateFlow.value = NoteListResult.Error(throwable.message) | ||
} | ||
}.launchIn(this) | ||
}.ensureActive() | ||
|
||
override fun errorResult(throwable: Throwable): NoteListResult { | ||
val errorName: String = throwable::class.simpleName.orEmpty() | ||
return when { | ||
errorName.contains("SQLite") -> NoteListResult.NavSignIn | ||
else -> NoteListResult.Error(throwable.message) | ||
} | ||
} | ||
fun onNoteClicked(id: Long) = router.navigate(route = "${AppNavGraph.Details.name}/$id") | ||
|
||
fun onSettingsClicked() = router.navigate(route = AppNavGraph.Settings.name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters