Moves the default zap type to a state flow and avoids passing on to the screen when using the default value.

This commit is contained in:
Vitor Pamplona 2024-08-16 17:45:53 -04:00
parent ec83ad9d20
commit 29c5b45764
12 changed files with 80 additions and 75 deletions

View File

@ -296,7 +296,7 @@ object LocalPreferences {
PrefKeys.REACTION_CHOICES,
Event.mapper.writeValueAsString(account.reactionChoices),
)
putString(PrefKeys.DEFAULT_ZAPTYPE, account.defaultZapType.name)
putString(PrefKeys.DEFAULT_ZAPTYPE, account.defaultZapType.value.name)
putString(
PrefKeys.DEFAULT_FILE_SERVER,
Event.mapper.writeValueAsString(account.defaultFileServer),
@ -658,7 +658,7 @@ object LocalPreferences {
translateTo = translateTo,
zapAmountChoices = zapAmountChoices,
reactionChoices = reactionChoices,
defaultZapType = defaultZapType,
defaultZapType = MutableStateFlow(defaultZapType),
defaultFileServer = defaultFileServer,
defaultHomeFollowList = MutableStateFlow(defaultHomeFollowList),
defaultStoriesFollowList = MutableStateFlow(defaultStoriesFollowList),

View File

@ -184,7 +184,7 @@ class Account(
var translateTo: String = Locale.getDefault().language,
var zapAmountChoices: List<Long> = DefaultZapAmounts,
var reactionChoices: List<String> = DefaultReactions,
var defaultZapType: LnZapEvent.ZapType = LnZapEvent.ZapType.PUBLIC,
var defaultZapType: MutableStateFlow<LnZapEvent.ZapType> = MutableStateFlow(LnZapEvent.ZapType.PUBLIC),
var defaultFileServer: Nip96MediaServers.ServerName = Nip96MediaServers.DEFAULT[0],
var defaultHomeFollowList: MutableStateFlow<String> = MutableStateFlow(KIND3_FOLLOWS),
var defaultStoriesFollowList: MutableStateFlow<String> = MutableStateFlow(GLOBAL_FOLLOWS),
@ -2665,7 +2665,7 @@ class Account(
}
fun changeDefaultZapType(zapType: LnZapEvent.ZapType) {
defaultZapType = zapType
defaultZapType.tryEmit(zapType)
live.invalidateData()
saveable.invalidateData()
}

View File

@ -355,7 +355,7 @@ fun EditPostView(
InvoiceRequest(
lud16,
user.pubkeyHex,
accountViewModel.account,
accountViewModel,
stringRes(id = R.string.lightning_invoice),
stringRes(id = R.string.lightning_create_and_add_invoice),
onSuccess = {

View File

@ -515,7 +515,7 @@ fun NewPostView(
InvoiceRequest(
lud16,
accountViewModel.account.userProfile().pubkeyHex,
accountViewModel.account,
accountViewModel,
stringRes(id = R.string.lightning_invoice),
stringRes(id = R.string.lightning_create_and_add_invoice),
onSuccess = {

View File

@ -37,7 +37,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -52,24 +51,19 @@ import androidx.compose.ui.unit.sp
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.commons.hashtags.CustomHashTagIcons
import com.vitorpamplona.amethyst.commons.hashtags.Lightning
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.DividerThickness
import com.vitorpamplona.amethyst.ui.theme.QuoteBorder
import com.vitorpamplona.amethyst.ui.theme.Size20Modifier
import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.amethyst.ui.theme.subtleBorder
import com.vitorpamplona.quartz.events.LnZapEvent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@Composable
fun InvoiceRequestCard(
lud16: String,
toUserPubKeyHex: String,
account: Account,
accountViewModel: AccountViewModel,
titleText: String? = null,
buttonText: String? = null,
onSuccess: (String) -> Unit,
@ -90,7 +84,7 @@ fun InvoiceRequestCard(
InvoiceRequest(
lud16,
toUserPubKeyHex,
account,
accountViewModel,
titleText,
buttonText,
onSuccess,
@ -105,7 +99,7 @@ fun InvoiceRequestCard(
fun InvoiceRequest(
lud16: String,
toUserPubKeyHex: String,
account: Account,
accountViewModel: AccountViewModel,
titleText: String? = null,
buttonText: String? = null,
onSuccess: (String) -> Unit,
@ -113,7 +107,6 @@ fun InvoiceRequest(
onError: (String, String) -> Unit,
) {
val context = LocalContext.current
val scope = rememberCoroutineScope()
Row(
verticalAlignment = Alignment.CenterVertically,
@ -186,36 +179,16 @@ fun InvoiceRequest(
Button(
modifier = Modifier.fillMaxWidth().padding(vertical = 10.dp),
onClick = {
scope.launch(Dispatchers.IO) {
if (account.defaultZapType == LnZapEvent.ZapType.NONZAP) {
LightningAddressResolver()
.lnAddressInvoice(
lud16,
amount * 1000,
message,
null,
onSuccess = onSuccess,
onError = onError,
onProgress = {},
context = context,
)
} else {
account.createZapRequestFor(toUserPubKeyHex, message, account.defaultZapType) { zapRequest ->
LocalCache.justConsume(zapRequest, null)
LightningAddressResolver()
.lnAddressInvoice(
lud16,
amount * 1000,
message,
zapRequest.toJson(),
onSuccess = onSuccess,
onError = onError,
onProgress = {},
context = context,
)
}
}
}
accountViewModel.sendSats(
lnaddress = lud16,
milliSats = amount * 1000,
message = message,
toUserPubKeyHex = toUserPubKeyHex,
onSuccess = onSuccess,
onError = onError,
onProgress = {},
context = context,
)
},
shape = QuoteBorder,
colors =

View File

@ -85,7 +85,6 @@ import com.vitorpamplona.amethyst.ui.theme.mediumImportanceLink
import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.events.EmptyTagList
import com.vitorpamplona.quartz.events.ImmutableListOfLists
import com.vitorpamplona.quartz.events.LnZapEvent
import com.vitorpamplona.quartz.events.toImmutableListOfLists
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
@ -398,7 +397,6 @@ fun ZapVote(
},
onProgress = { scope.launch(Dispatchers.Main) { zappingProgress = it } },
onPayViaIntent = {},
zapType = accountViewModel.account.defaultZapType,
)
} else {
wantsToZap = true
@ -522,10 +520,6 @@ fun FilteredZapAmountChoicePopup(
val context = LocalContext.current
val accountState by accountViewModel.accountLiveData.observeAsState()
val defaultZapType by
remember(accountState) {
derivedStateOf { accountState?.account?.defaultZapType ?: LnZapEvent.ZapType.PRIVATE }
}
val zapMessage = ""
@ -554,7 +548,6 @@ fun FilteredZapAmountChoicePopup(
onError,
onProgress,
onPayViaIntent,
defaultZapType,
)
onDismiss()
},
@ -581,7 +574,6 @@ fun FilteredZapAmountChoicePopup(
onError,
onProgress,
onPayViaIntent,
defaultZapType,
)
onDismiss()
},

View File

@ -1180,7 +1180,6 @@ fun zapClick(
context,
onError = onError,
onProgress = { onZappingProgress(it) },
zapType = accountViewModel.account.defaultZapType,
onPayViaIntent = onPayViaIntent,
)
} else if (accountViewModel.account.zapAmountChoices.size > 1) {
@ -1501,7 +1500,6 @@ fun ZapAmountChoicePopup(
onError,
onProgress,
onPayViaIntent,
accountViewModel.account.defaultZapType,
)
onDismiss()
},
@ -1528,7 +1526,6 @@ fun ZapAmountChoicePopup(
onError,
onProgress,
onPayViaIntent,
accountViewModel.account.defaultZapType,
)
onDismiss()
},

View File

@ -134,7 +134,7 @@ class UpdateZapAmountViewModel(
account.zapPaymentRequest?.relayUri?.let { TextFieldValue(it) } ?: TextFieldValue("")
this.walletConnectSecret =
account.zapPaymentRequest?.secret?.let { TextFieldValue(it) } ?: TextFieldValue("")
this.selectedZapType = account.defaultZapType
this.selectedZapType = account.defaultZapType.value
}
fun toListOfAmounts(commaSeparatedAmounts: String): List<Long> = commaSeparatedAmounts.split(",").map { it.trim().toLongOrNull() ?: 0 }
@ -196,7 +196,7 @@ class UpdateZapAmountViewModel(
fun hasChanged(): Boolean =
(
selectedZapType != account?.defaultZapType ||
selectedZapType != account?.defaultZapType?.value ||
amountSet != account?.zapAmountChoices ||
walletConnectPubkey.text != (account?.zapPaymentRequest?.pubKeyHex ?: "") ||
walletConnectRelay.text != (account?.zapPaymentRequest?.relayUri ?: "") ||

View File

@ -151,8 +151,9 @@ fun ZapCustomDialog(
remember {
zapTypes.map { TitleExplainer(it.second, it.third) }.toImmutableList()
}
var selectedZapType by
remember(accountViewModel) { mutableStateOf(accountViewModel.account.defaultZapType) }
remember(accountViewModel) { mutableStateOf(accountViewModel.account.defaultZapType.value) }
Dialog(
onDismissRequest = { onClose() },
@ -223,7 +224,7 @@ fun ZapCustomDialog(
label = stringRes(id = R.string.zap_type),
placeholder =
zapTypes
.filter { it.first == accountViewModel.account.defaultZapType }
.filter { it.first == accountViewModel.account.defaultZapType.value }
.first()
.second,
options = zapOptions,

View File

@ -501,7 +501,6 @@ fun customZapClick(
showErrorIfNoLnAddress = false,
onError = onError,
onProgress = { onZappingProgress(it) },
zapType = accountViewModel.account.defaultZapType,
onPayViaIntent = onPayViaIntent,
)
} else {

View File

@ -56,6 +56,7 @@ import com.vitorpamplona.amethyst.service.Nip11Retriever
import com.vitorpamplona.amethyst.service.OnlineChecker
import com.vitorpamplona.amethyst.service.ZapPaymentHandler
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver
import com.vitorpamplona.amethyst.ui.actions.Dao
import com.vitorpamplona.amethyst.ui.components.UrlPreviewState
import com.vitorpamplona.amethyst.ui.navigation.Route
@ -616,23 +617,23 @@ class AccountViewModel(
onError: (String, String) -> Unit,
onProgress: (percent: Float) -> Unit,
onPayViaIntent: (ImmutableList<ZapPaymentHandler.Payable>) -> Unit,
zapType: LnZapEvent.ZapType,
zapType: LnZapEvent.ZapType? = null,
) {
viewModelScope.launch(Dispatchers.IO) {
ZapPaymentHandler(account)
.zap(
note,
amount,
pollOption,
message,
context,
showErrorIfNoLnAddress,
onError,
note = note,
amountMilliSats = amount,
pollOption = pollOption,
message = message,
context = context,
showErrorIfNoLnAddress = showErrorIfNoLnAddress,
onError = onError,
onProgress = {
onProgress(it)
},
onPayViaIntent,
zapType,
onPayViaIntent = onPayViaIntent,
zapType = zapType ?: account.defaultZapType.value,
)
}
}
@ -857,7 +858,7 @@ class AccountViewModel(
}
}
fun defaultZapType(): LnZapEvent.ZapType = account.defaultZapType
fun defaultZapType(): LnZapEvent.ZapType = account.defaultZapType.value
fun unwrap(
event: GiftWrapEvent,
@ -1470,6 +1471,48 @@ class AccountViewModel(
AdvertisedRelayListEvent.createAddressTag(user.pubkeyHex),
)
fun sendSats(
lnaddress: String,
milliSats: Long,
message: String,
toUserPubKeyHex: HexKey,
onSuccess: (String) -> Unit,
onError: (String, String) -> Unit,
onProgress: (percent: Float) -> Unit,
context: Context,
) {
viewModelScope.launch(Dispatchers.IO) {
if (account.defaultZapType.value == LnZapEvent.ZapType.NONZAP) {
LightningAddressResolver()
.lnAddressInvoice(
lnaddress,
milliSats * 1000,
message,
null,
onSuccess = onSuccess,
onError = onError,
onProgress = onProgress,
context = context,
)
} else {
account.createZapRequestFor(toUserPubKeyHex, message, account.defaultZapType.value) { zapRequest ->
LocalCache.justConsume(zapRequest, null)
LightningAddressResolver()
.lnAddressInvoice(
lnaddress,
milliSats * 1000,
message,
zapRequest.toJson(),
onSuccess = onSuccess,
onError = onError,
onProgress = onProgress,
context = context,
)
}
}
}
}
val draftNoteCache = CachedDraftNotes(this)
class CachedDraftNotes(

View File

@ -1190,7 +1190,7 @@ fun DisplayLNAddress(
InvoiceRequestCard(
lud16,
userHex,
accountViewModel.account,
accountViewModel,
onSuccess = {
zapExpanded = false
// pay directly