From e89e8e5d010d4d4a9aaea5312aec82e5f0ee6380 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 25 Sep 2023 14:19:27 -0400 Subject: [PATCH] moves mark as read to the AccountViewModel scope. --- .../amethyst/ui/note/NoteCompose.kt | 41 ++++++++----------- .../ui/screen/loggedIn/AccountViewModel.kt | 13 ++++++ 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt index 4ba139024..af6635131 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt @@ -831,34 +831,17 @@ private fun CheckNewAndRenderNote( val backgroundColor = remember { mutableStateOf(defaultBackgroundColor) } LaunchedEffect(key1 = routeForLastRead, key2 = parentBackgroundColor?.value) { - launch(Dispatchers.IO) { - routeForLastRead?.let { - val lastTime = accountViewModel.account.loadLastRead(it) - - val createdAt = baseNote.createdAt() - if (createdAt != null) { - accountViewModel.account.markAsRead(it, createdAt) - - val isNew = createdAt > lastTime - - val newBackgroundColor = if (isNew) { - if (parentBackgroundColor != null) { - newItemColor.compositeOver(parentBackgroundColor.value) - } else { - newItemColor.compositeOver(defaultBackgroundColor) - } + routeForLastRead?.let { + accountViewModel.loadAndMarkAsRead(it, baseNote.createdAt()) { isNew -> + val newBackgroundColor = if (isNew) { + if (parentBackgroundColor != null) { + newItemColor.compositeOver(parentBackgroundColor.value) } else { - parentBackgroundColor?.value ?: defaultBackgroundColor - } - - if (newBackgroundColor != backgroundColor.value) { - launch(Dispatchers.Main) { - backgroundColor.value = newBackgroundColor - } + newItemColor.compositeOver(defaultBackgroundColor) } + } else { + parentBackgroundColor?.value ?: defaultBackgroundColor } - } ?: run { - val newBackgroundColor = parentBackgroundColor?.value ?: defaultBackgroundColor if (newBackgroundColor != backgroundColor.value) { launch(Dispatchers.Main) { @@ -866,6 +849,14 @@ private fun CheckNewAndRenderNote( } } } + } ?: run { + val newBackgroundColor = parentBackgroundColor?.value ?: defaultBackgroundColor + + if (newBackgroundColor != backgroundColor.value) { + launch(Dispatchers.Main) { + backgroundColor.value = newBackgroundColor + } + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index e2b26e48e..788f7cbae 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -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 { override fun create(modelClass: Class): AccountViewModel { return AccountViewModel(account) as AccountViewModel