introduce zap types, implemented non-zaps in custom zap dialog

Non-Zaps allow to tip the author of a note without using the zap mechanism (no record on the note)
This commit is contained in:
Believethehype
2023-04-09 23:37:23 +02:00
parent 09bad79870
commit 9d1ab5a9b8
5 changed files with 62 additions and 21 deletions
@@ -69,4 +69,11 @@ class LnZapEvent(
companion object { companion object {
const val kind = 9735 const val kind = 9735
} }
enum class ZapType() {
PUBLIC,
PRIVATE, // not implemented
ANONYMOUS, // not implemented
NONZAP
}
} }
@@ -31,6 +31,7 @@ import androidx.compose.ui.window.Popup
import androidx.navigation.NavController import androidx.navigation.NavController
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.model.LnZapEvent
import com.vitorpamplona.amethyst.ui.components.TranslatableRichTextViewer import com.vitorpamplona.amethyst.ui.components.TranslatableRichTextViewer
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange
@@ -251,7 +252,8 @@ fun ZapVote(
scope.launch(Dispatchers.Main) { scope.launch(Dispatchers.Main) {
zappingProgress = it zappingProgress = it
} }
} },
type = LnZapEvent.ZapType.PUBLIC
) )
} }
} else { } else {
@@ -378,7 +380,8 @@ fun FilteredZapAmountChoicePopup(
zapMessage, zapMessage,
context, context,
onError, onError,
onProgress onProgress,
LnZapEvent.ZapType.PUBLIC
) )
onDismiss() onDismiss()
} }
@@ -403,7 +406,8 @@ fun FilteredZapAmountChoicePopup(
zapMessage, zapMessage,
context, context,
onError, onError,
onProgress onProgress,
LnZapEvent.ZapType.PUBLIC
) )
onDismiss() onDismiss()
} }
@@ -495,7 +499,8 @@ fun ZapVoteAmountChoicePopup(
"", "",
context, context,
onError, onError,
onProgress onProgress,
LnZapEvent.ZapType.PUBLIC
) )
onDismiss() onDismiss()
} }
@@ -520,7 +525,8 @@ fun ZapVoteAmountChoicePopup(
"", "",
context, context,
onError, onError,
onProgress onProgress,
LnZapEvent.ZapType.PUBLIC
) )
onDismiss() onDismiss()
} }
@@ -55,6 +55,7 @@ import coil.request.CachePolicy
import coil.request.ImageRequest import coil.request.ImageRequest
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.model.LnZapEvent
import com.vitorpamplona.amethyst.ui.actions.NewPostView import com.vitorpamplona.amethyst.ui.actions.NewPostView
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange
@@ -363,7 +364,8 @@ fun ZapReaction(
scope.launch(Dispatchers.Main) { scope.launch(Dispatchers.Main) {
zappingProgress = it zappingProgress = it
} }
} },
type = LnZapEvent.ZapType.PUBLIC
) )
} }
} else if (account.zapAmountChoices.size > 1) { } else if (account.zapAmountChoices.size > 1) {
@@ -559,7 +561,8 @@ fun ZapAmountChoicePopup(
zapMessage, zapMessage,
context, context,
onError, onError,
onProgress onProgress,
LnZapEvent.ZapType.PUBLIC
) )
onDismiss() onDismiss()
} }
@@ -584,7 +587,8 @@ fun ZapAmountChoicePopup(
zapMessage, zapMessage,
context, context,
onError, onError,
onProgress onProgress,
LnZapEvent.ZapType.PUBLIC
) )
onDismiss() onDismiss()
} }
@@ -5,12 +5,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.* import androidx.compose.material.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.*
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@@ -27,15 +22,16 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.model.LnZapEvent
import com.vitorpamplona.amethyst.ui.actions.CloseButton import com.vitorpamplona.amethyst.ui.actions.CloseButton
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.TextSpinner
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class ZapOptionstViewModel : ViewModel() { class ZapOptionstViewModel : ViewModel() {
private var account: Account? = null private var account: Account? = null
var customAmount by mutableStateOf(TextFieldValue("21"))
var customAmount by mutableStateOf(TextFieldValue("1000"))
var customMessage by mutableStateOf(TextFieldValue("")) var customMessage by mutableStateOf(TextFieldValue(""))
fun load(account: Account) { fun load(account: Account) {
@@ -63,11 +59,21 @@ fun ZapCustomDialog(onClose: () -> Unit, account: Account, accountViewModel: Acc
val context = LocalContext.current val context = LocalContext.current
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val postViewModel: ZapOptionstViewModel = viewModel() val postViewModel: ZapOptionstViewModel = viewModel()
postViewModel.load(account)
LaunchedEffect(account) { LaunchedEffect(account) {
postViewModel.load(account) postViewModel.load(account)
} }
var zappingProgress by remember { mutableStateOf(0f) }
val zapTypes = listOf(
Pair(LnZapEvent.ZapType.PUBLIC, "Public"),
Pair(LnZapEvent.ZapType.NONZAP, "Non-Zap")
)
val zapOptions = zapTypes.map { it.second }
var selectedZapType by remember { mutableStateOf(zapTypes[0]) }
Dialog( Dialog(
onDismissRequest = { onClose() }, onDismissRequest = { onClose() },
properties = DialogProperties( properties = DialogProperties(
@@ -98,6 +104,7 @@ fun ZapCustomDialog(onClose: () -> Unit, account: Account, accountViewModel: Acc
postViewModel.customMessage.text, postViewModel.customMessage.text,
context, context,
onError = { onError = {
zappingProgress = 0f
scope.launch { scope.launch {
Toast Toast
.makeText(context, it, Toast.LENGTH_SHORT).show() .makeText(context, it, Toast.LENGTH_SHORT).show()
@@ -105,8 +112,10 @@ fun ZapCustomDialog(onClose: () -> Unit, account: Account, accountViewModel: Acc
}, },
onProgress = { onProgress = {
scope.launch(Dispatchers.Main) { scope.launch(Dispatchers.Main) {
zappingProgress = it
} }
} },
type = selectedZapType.first
) )
} }
onClose() onClose()
@@ -174,6 +183,15 @@ fun ZapCustomDialog(onClose: () -> Unit, account: Account, accountViewModel: Acc
.weight(1f) .weight(1f)
) )
} }
TextSpinner(
label = "Zap Type",
placeholder = "Public",
options = zapOptions,
onSelect = {
selectedZapType = zapTypes[it]
},
modifier = Modifier.fillMaxWidth()
)
} }
} }
} }
@@ -14,6 +14,7 @@ import com.vitorpamplona.amethyst.model.AccountState
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver
import com.vitorpamplona.amethyst.service.model.LnZapEvent
import com.vitorpamplona.amethyst.service.model.ReportEvent import com.vitorpamplona.amethyst.service.model.ReportEvent
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@@ -52,7 +53,7 @@ class AccountViewModel(private val account: Account) : ViewModel() {
account.delete(account.boostsTo(note)) account.delete(account.boostsTo(note))
} }
fun zap(note: Note, amount: Long, pollOption: Int?, message: String, context: Context, onError: (String) -> Unit, onProgress: (percent: Float) -> Unit) { fun zap(note: Note, amount: Long, pollOption: Int?, message: String, context: Context, onError: (String) -> Unit, onProgress: (percent: Float) -> Unit, type: LnZapEvent.ZapType) {
val lud16 = note.author?.info?.lud16?.trim() ?: note.author?.info?.lud06?.trim() val lud16 = note.author?.info?.lud16?.trim() ?: note.author?.info?.lud06?.trim()
if (lud16.isNullOrBlank()) { if (lud16.isNullOrBlank()) {
@@ -60,7 +61,12 @@ class AccountViewModel(private val account: Account) : ViewModel() {
return return
} }
val zapRequest = account.createZapRequestFor(note, pollOption, message) var zapRequest = account.createZapRequestFor(note, pollOption, message)
var zapRequestJson = zapRequest?.toJson()
if (type == LnZapEvent.ZapType.NONZAP) {
zapRequestJson = ""
}
onProgress(0.10f) onProgress(0.10f)
@@ -68,7 +74,7 @@ class AccountViewModel(private val account: Account) : ViewModel() {
lud16, lud16,
amount, amount,
message, message,
zapRequest?.toJson(), zapRequestJson,
onSuccess = { onSuccess = {
onProgress(0.7f) onProgress(0.7f)
if (account.hasWalletConnectSetup()) { if (account.hasWalletConnectSetup()) {