Skip to content

Commit

Permalink
Moves the default zap type to a state flow and avoids passing on to t…
Browse files Browse the repository at this point in the history
…he screen when using the default value.
  • Loading branch information
vitorpamplona committed Aug 16, 2024
1 parent ec83ad9 commit 29c5b45
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -2665,7 +2665,7 @@ class Account(
}

fun changeDefaultZapType(zapType: LnZapEvent.ZapType) {
defaultZapType = zapType
defaultZapType.tryEmit(zapType)
live.invalidateData()
saveable.invalidateData()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -90,7 +84,7 @@ fun InvoiceRequestCard(
InvoiceRequest(
lud16,
toUserPubKeyHex,
account,
accountViewModel,
titleText,
buttonText,
onSuccess,
Expand All @@ -105,15 +99,14 @@ fun InvoiceRequestCard(
fun InvoiceRequest(
lud16: String,
toUserPubKeyHex: String,
account: Account,
accountViewModel: AccountViewModel,
titleText: String? = null,
buttonText: String? = null,
onSuccess: (String) -> Unit,
onClose: () -> Unit,
onError: (String, String) -> Unit,
) {
val context = LocalContext.current
val scope = rememberCoroutineScope()

Row(
verticalAlignment = Alignment.CenterVertically,
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -398,7 +397,6 @@ fun ZapVote(
},
onProgress = { scope.launch(Dispatchers.Main) { zappingProgress = it } },
onPayViaIntent = {},
zapType = accountViewModel.account.defaultZapType,
)
} else {
wantsToZap = true
Expand Down Expand Up @@ -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 = ""

Expand Down Expand Up @@ -554,7 +548,6 @@ fun FilteredZapAmountChoicePopup(
onError,
onProgress,
onPayViaIntent,
defaultZapType,
)
onDismiss()
},
Expand All @@ -581,7 +574,6 @@ fun FilteredZapAmountChoicePopup(
onError,
onProgress,
onPayViaIntent,
defaultZapType,
)
onDismiss()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1501,7 +1500,6 @@ fun ZapAmountChoicePopup(
onError,
onProgress,
onPayViaIntent,
accountViewModel.account.defaultZapType,
)
onDismiss()
},
Expand All @@ -1528,7 +1526,6 @@ fun ZapAmountChoicePopup(
onError,
onProgress,
onPayViaIntent,
accountViewModel.account.defaultZapType,
)
onDismiss()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 ?: "") ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ fun customZapClick(
showErrorIfNoLnAddress = false,
onError = onError,
onProgress = { onZappingProgress(it) },
zapType = accountViewModel.account.defaultZapType,
onPayViaIntent = onPayViaIntent,
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
}
}
Expand Down Expand Up @@ -857,7 +858,7 @@ class AccountViewModel(
}
}

fun defaultZapType(): LnZapEvent.ZapType = account.defaultZapType
fun defaultZapType(): LnZapEvent.ZapType = account.defaultZapType.value

fun unwrap(
event: GiftWrapEvent,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ fun DisplayLNAddress(
InvoiceRequestCard(
lud16,
userHex,
accountViewModel.account,
accountViewModel,
onSuccess = {
zapExpanded = false
// pay directly
Expand Down

0 comments on commit 29c5b45

Please sign in to comment.