From a706b71d7c3b9ef44c195786082349dd02a69741 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 20 Sep 2023 18:12:41 -0400 Subject: [PATCH] Moves reaction calls to the viewModelScope --- .../amethyst/ui/note/ReactionsRow.kt | 26 +++++++------------ .../ui/screen/loggedIn/AccountViewModel.kt | 12 +++++---- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt index b8da5452c..7f9a63a57 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt @@ -1366,18 +1366,14 @@ private fun ActionableReactionButton( onChangeAmount: () -> Unit, toRemove: Set ) { - val scope = rememberCoroutineScope() - Button( modifier = Modifier.padding(horizontal = 3.dp), onClick = { - scope.launch(Dispatchers.IO) { - accountViewModel.reactToOrDelete( - baseNote, - reactionType - ) - onDismiss() - } + accountViewModel.reactToOrDelete( + baseNote, + reactionType + ) + onDismiss() }, shape = ButtonBorder, colors = ButtonDefaults @@ -1388,13 +1384,11 @@ private fun ActionableReactionButton( val thisModifier = remember(reactionType) { Modifier.combinedClickable( onClick = { - scope.launch(Dispatchers.IO) { - accountViewModel.reactToOrDelete( - baseNote, - reactionType - ) - onDismiss() - } + accountViewModel.reactToOrDelete( + baseNote, + reactionType + ) + onDismiss() }, onLongClick = { onChangeAmount() 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 640988931..974759a99 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 @@ -117,11 +117,13 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao { } fun reactToOrDelete(note: Note, reaction: String) { - val currentReactions = account.reactionTo(note, reaction) - if (currentReactions.isNotEmpty()) { - account.delete(currentReactions) - } else { - account.reactTo(note, reaction) + viewModelScope.launch(Dispatchers.IO) { + val currentReactions = account.reactionTo(note, reaction) + if (currentReactions.isNotEmpty()) { + account.delete(currentReactions) + } else { + account.reactTo(note, reaction) + } } }