moves mark as read to the AccountViewModel scope.

This commit is contained in:
Vitor Pamplona
2023-09-25 14:19:27 -04:00
parent fc4433e7ae
commit e89e8e5d01
2 changed files with 29 additions and 25 deletions
@@ -831,16 +831,8 @@ private fun CheckNewAndRenderNote(
val backgroundColor = remember { mutableStateOf<Color>(defaultBackgroundColor) } val backgroundColor = remember { mutableStateOf<Color>(defaultBackgroundColor) }
LaunchedEffect(key1 = routeForLastRead, key2 = parentBackgroundColor?.value) { LaunchedEffect(key1 = routeForLastRead, key2 = parentBackgroundColor?.value) {
launch(Dispatchers.IO) {
routeForLastRead?.let { routeForLastRead?.let {
val lastTime = accountViewModel.account.loadLastRead(it) accountViewModel.loadAndMarkAsRead(it, baseNote.createdAt()) { isNew ->
val createdAt = baseNote.createdAt()
if (createdAt != null) {
accountViewModel.account.markAsRead(it, createdAt)
val isNew = createdAt > lastTime
val newBackgroundColor = if (isNew) { val newBackgroundColor = if (isNew) {
if (parentBackgroundColor != null) { if (parentBackgroundColor != null) {
newItemColor.compositeOver(parentBackgroundColor.value) newItemColor.compositeOver(parentBackgroundColor.value)
@@ -867,7 +859,6 @@ private fun CheckNewAndRenderNote(
} }
} }
} }
}
ClickableNote( ClickableNote(
baseNote = baseNote, baseNote = baseNote,
@@ -720,6 +720,19 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao {
} }
} }
fun loadAndMarkAsRead(routeForLastRead: String, baseNoteCreatedAt: Long?, onIsNew: (Boolean) -> Unit) {
viewModelScope.launch(Dispatchers.IO) {
val lastTime = account.loadLastRead(routeForLastRead)
if (baseNoteCreatedAt != null) {
account.markAsRead(routeForLastRead, baseNoteCreatedAt)
onIsNew(baseNoteCreatedAt > lastTime)
} else {
onIsNew(false)
}
}
}
class Factory(val account: Account) : ViewModelProvider.Factory { class Factory(val account: Account) : ViewModelProvider.Factory {
override fun <AccountViewModel : ViewModel> create(modelClass: Class<AccountViewModel>): AccountViewModel { override fun <AccountViewModel : ViewModel> create(modelClass: Class<AccountViewModel>): AccountViewModel {
return AccountViewModel(account) as AccountViewModel return AccountViewModel(account) as AccountViewModel