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,29 +341,27 @@ 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, poolOption.option,
poolOption.option, "",
"", context,
context, onError = {
onError = { scope.launch {
scope.launch { zappingProgress = 0f
zappingProgress = 0f Toast
Toast .makeText(context, it, Toast.LENGTH_SHORT)
.makeText(context, it, Toast.LENGTH_SHORT) .show()
.show() }
} },
}, onProgress = {
onProgress = { scope.launch(Dispatchers.Main) {
scope.launch(Dispatchers.Main) { zappingProgress = it
zappingProgress = it }
} },
}, 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,19 +479,17 @@ 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, pollOption,
pollOption, zapMessage,
zapMessage, context,
context, onError,
onError, onProgress,
onProgress, defaultZapType
defaultZapType )
) onDismiss()
onDismiss()
}
}, },
shape = ButtonBorder, shape = ButtonBorder,
colors = ButtonDefaults colors = ButtonDefaults
@@ -508,19 +503,17 @@ 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, pollOption,
pollOption, zapMessage,
zapMessage, context,
context, onError,
onError, onProgress,
onProgress, defaultZapType
defaultZapType )
) onDismiss()
onDismiss()
}
}, },
onLongClick = { onLongClick = {
onChangeAmount() onChangeAmount()

View File

@@ -1088,29 +1088,27 @@ 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, null,
null, "",
"", context,
context, onError = {
onError = { scope.launch {
scope.launch { onZappingProgress(0f)
onZappingProgress(0f) Toast
Toast .makeText(context, it, Toast.LENGTH_SHORT)
.makeText(context, it, Toast.LENGTH_SHORT) .show()
.show() }
} },
}, onProgress = {
onProgress = { scope.launch(Dispatchers.Main) {
scope.launch(Dispatchers.Main) { onZappingProgress(it)
onZappingProgress(it) }
} },
}, 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,19 +1431,17 @@ 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, null,
null, zapMessage,
zapMessage, context,
context, onError,
onError, onProgress,
onProgress, account.defaultZapType
account.defaultZapType )
) onDismiss()
onDismiss()
}
}, },
shape = ButtonBorder, shape = ButtonBorder,
colors = ButtonDefaults colors = ButtonDefaults
@@ -1460,19 +1455,17 @@ 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, null,
null, zapMessage,
zapMessage, context,
context, onError,
onError, onProgress,
onProgress, account.defaultZapType
account.defaultZapType )
) onDismiss()
onDismiss()
}
}, },
onLongClick = { onLongClick = {
onChangeAmount() onChangeAmount()

View File

@@ -102,28 +102,26 @@ 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, null,
null, postViewModel.customMessage.text,
postViewModel.customMessage.text, context,
context, onError = {
onError = { zappingProgress = 0f
zappingProgress = 0f scope.launch {
scope.launch { Toast
Toast .makeText(context, it, Toast.LENGTH_SHORT).show()
.makeText(context, it, Toast.LENGTH_SHORT).show() }
} },
}, onProgress = {
onProgress = { scope.launch(Dispatchers.Main) {
scope.launch(Dispatchers.Main) { zappingProgress = it
zappingProgress = it }
} },
}, 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) {
account.report(user, type) viewModelScope.launch(Dispatchers.IO) {
account.report(user, type)
account.hideUser(user.pubkeyHex)
}
} }
fun boost(note: Note) { fun boost(note: Note) {