Moves zaps from Compose scope to ViewModel Scope

This commit is contained in:
Vitor Pamplona
2023-08-12 21:38:14 -04:00
parent 6f19bccc54
commit a15263eeb9
4 changed files with 116 additions and 123 deletions

View File

@@ -341,7 +341,6 @@ fun ZapVote(
} else if (accountViewModel.account.zapAmountChoices.size == 1 && } else if (accountViewModel.account.zapAmountChoices.size == 1 &&
pollViewModel.isValidInputVoteAmount(accountViewModel.account.zapAmountChoices.first()) pollViewModel.isValidInputVoteAmount(accountViewModel.account.zapAmountChoices.first())
) { ) {
scope.launch(Dispatchers.IO) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
accountViewModel.account.zapAmountChoices.first() * 1000, accountViewModel.account.zapAmountChoices.first() * 1000,
@@ -363,7 +362,6 @@ fun ZapVote(
}, },
zapType = accountViewModel.account.defaultZapType zapType = accountViewModel.account.defaultZapType
) )
}
} else { } else {
wantsToZap = true wantsToZap = true
} }
@@ -462,7 +460,6 @@ fun FilteredZapAmountChoicePopup(
} }
val zapMessage = "" val zapMessage = ""
val scope = rememberCoroutineScope()
val sortedOptions = remember(accountState) { val sortedOptions = remember(accountState) {
pollViewModel.createZapOptionsThatMatchThePollingParameters() pollViewModel.createZapOptionsThatMatchThePollingParameters()
@@ -482,7 +479,6 @@ fun FilteredZapAmountChoicePopup(
Button( Button(
modifier = Modifier.padding(horizontal = 3.dp), modifier = Modifier.padding(horizontal = 3.dp),
onClick = { onClick = {
scope.launch(Dispatchers.IO) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
amountInSats * 1000, amountInSats * 1000,
@@ -494,7 +490,6 @@ fun FilteredZapAmountChoicePopup(
defaultZapType defaultZapType
) )
onDismiss() onDismiss()
}
}, },
shape = ButtonBorder, shape = ButtonBorder,
colors = ButtonDefaults colors = ButtonDefaults
@@ -508,7 +503,6 @@ fun FilteredZapAmountChoicePopup(
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
modifier = Modifier.combinedClickable( modifier = Modifier.combinedClickable(
onClick = { onClick = {
scope.launch(Dispatchers.IO) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
amountInSats * 1000, amountInSats * 1000,
@@ -520,7 +514,6 @@ fun FilteredZapAmountChoicePopup(
defaultZapType defaultZapType
) )
onDismiss() onDismiss()
}
}, },
onLongClick = { onLongClick = {
onChangeAmount() onChangeAmount()

View File

@@ -1088,7 +1088,6 @@ private fun zapClick(
.show() .show()
} }
} else if (accountViewModel.account.zapAmountChoices.size == 1) { } else if (accountViewModel.account.zapAmountChoices.size == 1) {
scope.launch(Dispatchers.IO) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
accountViewModel.account.zapAmountChoices.first() * 1000, accountViewModel.account.zapAmountChoices.first() * 1000,
@@ -1110,7 +1109,6 @@ private fun zapClick(
}, },
zapType = accountViewModel.account.defaultZapType zapType = accountViewModel.account.defaultZapType
) )
}
} else if (accountViewModel.account.zapAmountChoices.size > 1) { } else if (accountViewModel.account.zapAmountChoices.size > 1) {
onMultipleChoices() onMultipleChoices()
} }
@@ -1422,7 +1420,6 @@ fun ZapAmountChoicePopup(
val accountState by accountViewModel.accountLiveData.observeAsState() val accountState by accountViewModel.accountLiveData.observeAsState()
val account = accountState?.account ?: return val account = accountState?.account ?: return
val zapMessage = "" val zapMessage = ""
val scope = rememberCoroutineScope()
Popup( Popup(
alignment = Alignment.BottomCenter, alignment = Alignment.BottomCenter,
@@ -1434,7 +1431,6 @@ fun ZapAmountChoicePopup(
Button( Button(
modifier = Modifier.padding(horizontal = 3.dp), modifier = Modifier.padding(horizontal = 3.dp),
onClick = { onClick = {
scope.launch(Dispatchers.IO) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
amountInSats * 1000, amountInSats * 1000,
@@ -1446,7 +1442,6 @@ fun ZapAmountChoicePopup(
account.defaultZapType account.defaultZapType
) )
onDismiss() onDismiss()
}
}, },
shape = ButtonBorder, shape = ButtonBorder,
colors = ButtonDefaults colors = ButtonDefaults
@@ -1460,7 +1455,6 @@ fun ZapAmountChoicePopup(
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
modifier = Modifier.combinedClickable( modifier = Modifier.combinedClickable(
onClick = { onClick = {
scope.launch(Dispatchers.IO) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
amountInSats * 1000, amountInSats * 1000,
@@ -1472,7 +1466,6 @@ fun ZapAmountChoicePopup(
account.defaultZapType account.defaultZapType
) )
onDismiss() onDismiss()
}
}, },
onLongClick = { onLongClick = {
onChangeAmount() onChangeAmount()

View File

@@ -102,7 +102,6 @@ fun ZapCustomDialog(onClose: () -> Unit, accountViewModel: AccountViewModel, bas
ZapButton( ZapButton(
isActive = postViewModel.canSend() isActive = postViewModel.canSend()
) { ) {
scope.launch(Dispatchers.IO) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
postViewModel.value()!! * 1000L, postViewModel.value()!! * 1000L,
@@ -123,7 +122,6 @@ fun ZapCustomDialog(onClose: () -> Unit, accountViewModel: AccountViewModel, bas
}, },
zapType = selectedZapType zapType = selectedZapType
) )
}
onClose() onClose()
} }
} }

View File

@@ -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) { 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() val lud16 = note.event?.zapAddress() ?: note.author?.info?.lud16?.trim() ?: note.author?.info?.lud06?.trim()
if (lud16.isNullOrBlank()) { if (lud16.isNullOrBlank()) {
@@ -197,7 +203,10 @@ class AccountViewModel(val account: Account) : ViewModel() {
} }
fun report(user: User, type: ReportEvent.ReportType) { fun report(user: User, type: ReportEvent.ReportType) {
viewModelScope.launch(Dispatchers.IO) {
account.report(user, type) account.report(user, type)
account.hideUser(user.pubkeyHex)
}
} }
fun boost(note: Note) { fun boost(note: Note) {