Moves reaction calls to the viewModelScope

This commit is contained in:
Vitor Pamplona 2023-09-20 18:12:41 -04:00
parent dc73843447
commit a706b71d7c
2 changed files with 17 additions and 21 deletions

View File

@ -1366,18 +1366,14 @@ private fun ActionableReactionButton(
onChangeAmount: () -> Unit, onChangeAmount: () -> Unit,
toRemove: Set<String> toRemove: Set<String>
) { ) {
val scope = rememberCoroutineScope()
Button( Button(
modifier = Modifier.padding(horizontal = 3.dp), modifier = Modifier.padding(horizontal = 3.dp),
onClick = { onClick = {
scope.launch(Dispatchers.IO) { accountViewModel.reactToOrDelete(
accountViewModel.reactToOrDelete( baseNote,
baseNote, reactionType
reactionType )
) onDismiss()
onDismiss()
}
}, },
shape = ButtonBorder, shape = ButtonBorder,
colors = ButtonDefaults colors = ButtonDefaults
@ -1388,13 +1384,11 @@ private fun ActionableReactionButton(
val thisModifier = remember(reactionType) { val thisModifier = remember(reactionType) {
Modifier.combinedClickable( Modifier.combinedClickable(
onClick = { onClick = {
scope.launch(Dispatchers.IO) { accountViewModel.reactToOrDelete(
accountViewModel.reactToOrDelete( baseNote,
baseNote, reactionType
reactionType )
) onDismiss()
onDismiss()
}
}, },
onLongClick = { onLongClick = {
onChangeAmount() onChangeAmount()

View File

@ -117,11 +117,13 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao {
} }
fun reactToOrDelete(note: Note, reaction: String) { fun reactToOrDelete(note: Note, reaction: String) {
val currentReactions = account.reactionTo(note, reaction) viewModelScope.launch(Dispatchers.IO) {
if (currentReactions.isNotEmpty()) { val currentReactions = account.reactionTo(note, reaction)
account.delete(currentReactions) if (currentReactions.isNotEmpty()) {
} else { account.delete(currentReactions)
account.reactTo(note, reaction) } else {
account.reactTo(note, reaction)
}
} }
} }