From a15263eeb9d93254ad1c60ce7473f61684c594d8 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Sat, 12 Aug 2023 21:38:14 -0400 Subject: [PATCH] Moves zaps from Compose scope to ViewModel Scope --- .../amethyst/ui/note/PollNote.kt | 93 +++++++++---------- .../amethyst/ui/note/ReactionsRow.kt | 93 +++++++++---------- .../amethyst/ui/note/ZapCustomDialog.kt | 42 ++++----- .../ui/screen/loggedIn/AccountViewModel.kt | 11 ++- 4 files changed, 116 insertions(+), 123 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt index e5d033be1..decc55f5f 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt @@ -341,29 +341,27 @@ fun ZapVote( } else if (accountViewModel.account.zapAmountChoices.size == 1 && pollViewModel.isValidInputVoteAmount(accountViewModel.account.zapAmountChoices.first()) ) { - scope.launch(Dispatchers.IO) { - accountViewModel.zap( - baseNote, - accountViewModel.account.zapAmountChoices.first() * 1000, - poolOption.option, - "", - context, - onError = { - scope.launch { - zappingProgress = 0f - Toast - .makeText(context, it, Toast.LENGTH_SHORT) - .show() - } - }, - onProgress = { - scope.launch(Dispatchers.Main) { - zappingProgress = it - } - }, - zapType = accountViewModel.account.defaultZapType - ) - } + accountViewModel.zap( + baseNote, + accountViewModel.account.zapAmountChoices.first() * 1000, + poolOption.option, + "", + context, + onError = { + scope.launch { + zappingProgress = 0f + Toast + .makeText(context, it, Toast.LENGTH_SHORT) + .show() + } + }, + onProgress = { + scope.launch(Dispatchers.Main) { + zappingProgress = it + } + }, + zapType = accountViewModel.account.defaultZapType + ) } else { wantsToZap = true } @@ -462,7 +460,6 @@ fun FilteredZapAmountChoicePopup( } val zapMessage = "" - val scope = rememberCoroutineScope() val sortedOptions = remember(accountState) { pollViewModel.createZapOptionsThatMatchThePollingParameters() @@ -482,19 +479,17 @@ fun FilteredZapAmountChoicePopup( Button( modifier = Modifier.padding(horizontal = 3.dp), onClick = { - scope.launch(Dispatchers.IO) { - accountViewModel.zap( - baseNote, - amountInSats * 1000, - pollOption, - zapMessage, - context, - onError, - onProgress, - defaultZapType - ) - onDismiss() - } + accountViewModel.zap( + baseNote, + amountInSats * 1000, + pollOption, + zapMessage, + context, + onError, + onProgress, + defaultZapType + ) + onDismiss() }, shape = ButtonBorder, colors = ButtonDefaults @@ -508,19 +503,17 @@ fun FilteredZapAmountChoicePopup( textAlign = TextAlign.Center, modifier = Modifier.combinedClickable( onClick = { - scope.launch(Dispatchers.IO) { - accountViewModel.zap( - baseNote, - amountInSats * 1000, - pollOption, - zapMessage, - context, - onError, - onProgress, - defaultZapType - ) - onDismiss() - } + accountViewModel.zap( + baseNote, + amountInSats * 1000, + pollOption, + zapMessage, + context, + onError, + onProgress, + defaultZapType + ) + onDismiss() }, onLongClick = { onChangeAmount() 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 0ef17030c..be0802cb5 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 @@ -1088,29 +1088,27 @@ private fun zapClick( .show() } } else if (accountViewModel.account.zapAmountChoices.size == 1) { - scope.launch(Dispatchers.IO) { - accountViewModel.zap( - baseNote, - accountViewModel.account.zapAmountChoices.first() * 1000, - null, - "", - context, - onError = { - scope.launch { - onZappingProgress(0f) - Toast - .makeText(context, it, Toast.LENGTH_SHORT) - .show() - } - }, - onProgress = { - scope.launch(Dispatchers.Main) { - onZappingProgress(it) - } - }, - zapType = accountViewModel.account.defaultZapType - ) - } + accountViewModel.zap( + baseNote, + accountViewModel.account.zapAmountChoices.first() * 1000, + null, + "", + context, + onError = { + scope.launch { + onZappingProgress(0f) + Toast + .makeText(context, it, Toast.LENGTH_SHORT) + .show() + } + }, + onProgress = { + scope.launch(Dispatchers.Main) { + onZappingProgress(it) + } + }, + zapType = accountViewModel.account.defaultZapType + ) } else if (accountViewModel.account.zapAmountChoices.size > 1) { onMultipleChoices() } @@ -1422,7 +1420,6 @@ fun ZapAmountChoicePopup( val accountState by accountViewModel.accountLiveData.observeAsState() val account = accountState?.account ?: return val zapMessage = "" - val scope = rememberCoroutineScope() Popup( alignment = Alignment.BottomCenter, @@ -1434,19 +1431,17 @@ fun ZapAmountChoicePopup( Button( modifier = Modifier.padding(horizontal = 3.dp), onClick = { - scope.launch(Dispatchers.IO) { - accountViewModel.zap( - baseNote, - amountInSats * 1000, - null, - zapMessage, - context, - onError, - onProgress, - account.defaultZapType - ) - onDismiss() - } + accountViewModel.zap( + baseNote, + amountInSats * 1000, + null, + zapMessage, + context, + onError, + onProgress, + account.defaultZapType + ) + onDismiss() }, shape = ButtonBorder, colors = ButtonDefaults @@ -1460,19 +1455,17 @@ fun ZapAmountChoicePopup( textAlign = TextAlign.Center, modifier = Modifier.combinedClickable( onClick = { - scope.launch(Dispatchers.IO) { - accountViewModel.zap( - baseNote, - amountInSats * 1000, - null, - zapMessage, - context, - onError, - onProgress, - account.defaultZapType - ) - onDismiss() - } + accountViewModel.zap( + baseNote, + amountInSats * 1000, + null, + zapMessage, + context, + onError, + onProgress, + account.defaultZapType + ) + onDismiss() }, onLongClick = { onChangeAmount() diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapCustomDialog.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapCustomDialog.kt index 8aae0e444..85a8109f4 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapCustomDialog.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapCustomDialog.kt @@ -102,28 +102,26 @@ fun ZapCustomDialog(onClose: () -> Unit, accountViewModel: AccountViewModel, bas ZapButton( isActive = postViewModel.canSend() ) { - scope.launch(Dispatchers.IO) { - accountViewModel.zap( - baseNote, - postViewModel.value()!! * 1000L, - null, - postViewModel.customMessage.text, - context, - onError = { - zappingProgress = 0f - scope.launch { - Toast - .makeText(context, it, Toast.LENGTH_SHORT).show() - } - }, - onProgress = { - scope.launch(Dispatchers.Main) { - zappingProgress = it - } - }, - zapType = selectedZapType - ) - } + accountViewModel.zap( + baseNote, + postViewModel.value()!! * 1000L, + null, + postViewModel.customMessage.text, + context, + onError = { + zappingProgress = 0f + scope.launch { + Toast + .makeText(context, it, Toast.LENGTH_SHORT).show() + } + }, + onProgress = { + scope.launch(Dispatchers.Main) { + zappingProgress = it + } + }, + zapType = selectedZapType + ) onClose() } } 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 b7de6d31a..480e63e23 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 @@ -130,6 +130,12 @@ class AccountViewModel(val account: Account) : ViewModel() { } fun zap(note: Note, amount: Long, pollOption: Int?, message: String, context: Context, onError: (String) -> Unit, onProgress: (percent: Float) -> Unit, zapType: LnZapEvent.ZapType) { + viewModelScope.launch(Dispatchers.IO) { + innerZap(note, amount, pollOption, message, context, onError, onProgress, zapType) + } + } + + suspend fun innerZap(note: Note, amount: Long, pollOption: Int?, message: String, context: Context, onError: (String) -> Unit, onProgress: (percent: Float) -> Unit, zapType: LnZapEvent.ZapType) { val lud16 = note.event?.zapAddress() ?: note.author?.info?.lud16?.trim() ?: note.author?.info?.lud06?.trim() if (lud16.isNullOrBlank()) { @@ -197,7 +203,10 @@ class AccountViewModel(val account: Account) : ViewModel() { } fun report(user: User, type: ReportEvent.ReportType) { - account.report(user, type) + viewModelScope.launch(Dispatchers.IO) { + account.report(user, type) + account.hideUser(user.pubkeyHex) + } } fun boost(note: Note) {