diff --git a/app/src/main/java/com/bitcointxoko/gudariwallet/ui/WalletViewModel.kt b/app/src/main/java/com/bitcointxoko/gudariwallet/ui/WalletViewModel.kt index e074bc6..9b87ad5 100644 --- a/app/src/main/java/com/bitcointxoko/gudariwallet/ui/WalletViewModel.kt +++ b/app/src/main/java/com/bitcointxoko/gudariwallet/ui/WalletViewModel.kt @@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.SharingStarted import com.bitcointxoko.gudariwallet.util.lnurlProtocolError import androidx.fragment.app.FragmentActivity +import com.bitcointxoko.gudariwallet.api.RouteHintHop import com.bitcointxoko.gudariwallet.data.BalancePrefsStore import com.bitcointxoko.gudariwallet.data.FiatRateCache import com.bitcointxoko.gudariwallet.util.BiometricHelper @@ -413,31 +414,20 @@ class WalletViewModel( allRouteHints = decoded.routeHints, routeHintAliases = emptyMap() ) - decoded.payee?.let { pubkey -> - viewModelScope.launch { - val alias = aliasRepo.resolve(pubkey) - val current = _sendState.value - if (alias != null && - current is SendState.Bolt11Decoded && - current.payee == pubkey) { - _sendState.value = current.copy(nodeAlias = alias) - } - } - } - decoded.routeHints - .map { it.publicKey } - .distinct() - .forEach { pubkey -> - viewModelScope.launch { - val alias = aliasRepo.resolve(pubkey) ?: return@launch - val current = _sendState.value - if (current is SendState.Bolt11Decoded) { - _sendState.value = current.copy( - routeHintAliases = current.routeHintAliases + (pubkey to alias) - ) - } - } + resolveAliasesInBackground( + payee = decoded.payee, + routeHints = decoded.routeHints, + readState = { _sendState.value }, + copyAlias = { state, alias -> + (state as? SendState.Bolt11Decoded) + ?.takeIf { it.payee == decoded.payee } + ?.copy(nodeAlias = alias) + }, + copyHint = { state, pubkey, alias -> + (state as? SendState.Bolt11Decoded) + ?.copy(routeHintAliases = state.routeHintAliases + (pubkey to alias)) } + ) } .onFailure { e -> Log.e(TAG, "SEND [DECODE ERROR] ${e.message}") @@ -791,33 +781,20 @@ class WalletViewModel( routeHintAliases = emptyMap() ) - decoded.payee?.let { pubkey -> - viewModelScope.launch { - val alias = aliasRepo.resolve(pubkey) - val current = _sendState.value - if (alias != null && - current is SendState.LnurlInvoiceReady && - current.payee == pubkey) { - _sendState.value = current.copy(nodeAlias = alias) - } - } - } - - // ── 5. Resolve node aliases for route-hint hops in the background - decoded.routeHints - .map { it.publicKey } - .distinct() - .forEach { pubkey -> - viewModelScope.launch { - val alias = aliasRepo.resolve(pubkey) ?: return@launch - val current = _sendState.value - if (current is SendState.LnurlInvoiceReady) { - _sendState.value = current.copy( - routeHintAliases = current.routeHintAliases + (pubkey to alias) - ) - } - } + resolveAliasesInBackground( + payee = decoded.payee, + routeHints = decoded.routeHints, + readState = { _sendState.value }, + copyAlias = { state, alias -> + (state as? SendState.LnurlInvoiceReady) + ?.takeIf { it.payee == decoded.payee } + ?.copy(nodeAlias = alias) + }, + copyHint = { state, pubkey, alias -> + (state as? SendState.LnurlInvoiceReady) + ?.copy(routeHintAliases = state.routeHintAliases + (pubkey to alias)) } + ) } } @@ -944,4 +921,37 @@ class WalletViewModel( rawRes = raw ) } + + /** + * Fires background coroutines to resolve node aliases for [payee] and each + * pubkey in [routeHints]. + * + * [readState] — returns the current SendState to check it's still relevant. + * [copyAlias] — called with the resolved payee alias; return the new state or + * null to skip the update. + * [copyHint] — called with (pubkey, alias); return the new state or null to + * skip the update. + */ + private fun resolveAliasesInBackground( + payee : String?, + routeHints : List, + readState : () -> SendState, + copyAlias : (SendState, String) -> SendState?, + copyHint : (SendState, String, String) -> SendState? + ) { + payee?.let { pubkey -> + viewModelScope.launch { + val alias = aliasRepo.resolve(pubkey) ?: return@launch + val current = readState() + copyAlias(current, alias)?.let { _sendState.value = it } + } + } + routeHints.map { it.publicKey }.distinct().forEach { pubkey -> + viewModelScope.launch { + val alias = aliasRepo.resolve(pubkey) ?: return@launch + val current = readState() + copyHint(current, pubkey, alias)?.let { _sendState.value = it } + } + } + } }