refactor: remove navigation orchestration from SendViewModel
This commit is contained in:
@@ -17,6 +17,7 @@ import com.bitcointxoko.gudariwallet.ui.balance.BalanceViewModel
|
|||||||
import com.bitcointxoko.gudariwallet.ui.clipboard.ClipboardViewModel
|
import com.bitcointxoko.gudariwallet.ui.clipboard.ClipboardViewModel
|
||||||
import com.bitcointxoko.gudariwallet.ui.fiat.FiatViewModel
|
import com.bitcointxoko.gudariwallet.ui.fiat.FiatViewModel
|
||||||
import com.bitcointxoko.gudariwallet.ui.receive.ReceiveViewModel
|
import com.bitcointxoko.gudariwallet.ui.receive.ReceiveViewModel
|
||||||
|
import com.bitcointxoko.gudariwallet.ui.send.SendEvent
|
||||||
import com.bitcointxoko.gudariwallet.ui.send.SendStrings
|
import com.bitcointxoko.gudariwallet.ui.send.SendStrings
|
||||||
import com.bitcointxoko.gudariwallet.ui.send.SendViewModel
|
import com.bitcointxoko.gudariwallet.ui.send.SendViewModel
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
@@ -110,6 +111,12 @@ class WalletViewModel(
|
|||||||
) = sendVm.requestPayment(activity, subtitle, strings, pay)
|
) = sendVm.requestPayment(activity, subtitle, strings, pay)
|
||||||
fun resetSendState() = sendVm.resetSendState()
|
fun resetSendState() = sendVm.resetSendState()
|
||||||
|
|
||||||
|
val sendEvents: SharedFlow<SendEvent> = sendVm.events
|
||||||
|
|
||||||
|
fun handleWithdrawScanned(raw: LnurlScanResponse, lnurl: String) {
|
||||||
|
receiveVm.handleWithdrawResponse(raw, lnurl)
|
||||||
|
}
|
||||||
|
|
||||||
val lightningAddress: StateFlow<String?> = lnAddressStore.lightningAddressFlow
|
val lightningAddress: StateFlow<String?> = lnAddressStore.lightningAddressFlow
|
||||||
.stateIn(
|
.stateIn(
|
||||||
scope = viewModelScope,
|
scope = viewModelScope,
|
||||||
|
|||||||
@@ -46,9 +46,6 @@ class WalletViewModelFactory(private val app: Application) : ViewModelProvider.F
|
|||||||
aliasRepo = aliasRepo,
|
aliasRepo = aliasRepo,
|
||||||
lnurlCache = lnurlCache,
|
lnurlCache = lnurlCache,
|
||||||
balanceVm = balanceVm,
|
balanceVm = balanceVm,
|
||||||
onWithdrawScanned = { raw, lnurl ->
|
|
||||||
receiveVm.handleWithdrawResponse(raw, lnurl)
|
|
||||||
},
|
|
||||||
scanner = lnurlScanner,
|
scanner = lnurlScanner,
|
||||||
payer = lnurlPayer,
|
payer = lnurlPayer,
|
||||||
poller = poller,
|
poller = poller,
|
||||||
|
|||||||
@@ -71,6 +71,15 @@ fun SendScreen(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(vm) {
|
||||||
|
vm.sendEvents.collect { event ->
|
||||||
|
when (event) {
|
||||||
|
is SendEvent.WithdrawScanned ->
|
||||||
|
vm.handleWithdrawScanned(event.raw, event.lnurl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
|
|||||||
@@ -21,12 +21,21 @@ import com.bitcointxoko.gudariwallet.util.SendInputType
|
|||||||
import com.bitcointxoko.gudariwallet.util.WalletConstants
|
import com.bitcointxoko.gudariwallet.util.WalletConstants
|
||||||
import com.bitcointxoko.gudariwallet.util.lnurlProtocolError
|
import com.bitcointxoko.gudariwallet.util.lnurlProtocolError
|
||||||
import com.bitcointxoko.gudariwallet.util.parseApiError
|
import com.bitcointxoko.gudariwallet.util.parseApiError
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.SharedFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
private const val TAG = "SendViewModel"
|
private const val TAG = "SendViewModel"
|
||||||
|
|
||||||
|
sealed class SendEvent {
|
||||||
|
data class WithdrawScanned(
|
||||||
|
val raw : LnurlScanResponse,
|
||||||
|
val lnurl : String,
|
||||||
|
) : SendEvent()
|
||||||
|
}
|
||||||
|
|
||||||
// ── String bundle injected at the call site ───────────────────────────────────
|
// ── String bundle injected at the call site ───────────────────────────────────
|
||||||
/**
|
/**
|
||||||
* All user-visible strings produced by [SendViewModel].
|
* All user-visible strings produced by [SendViewModel].
|
||||||
@@ -53,11 +62,12 @@ class SendViewModel(
|
|||||||
private val aliasRepo : NodeAliasRepository,
|
private val aliasRepo : NodeAliasRepository,
|
||||||
private val lnurlCache : LnurlCacheRepository,
|
private val lnurlCache : LnurlCacheRepository,
|
||||||
private val balanceVm : BalanceViewModel,
|
private val balanceVm : BalanceViewModel,
|
||||||
private val onWithdrawScanned: (raw: LnurlScanResponse, lnurl: String) -> Unit,
|
|
||||||
private val scanner : LnurlScanner,
|
private val scanner : LnurlScanner,
|
||||||
private val payer : LnurlPayer,
|
private val payer : LnurlPayer,
|
||||||
private val poller : PaymentPoller,
|
private val poller : PaymentPoller,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
private val _events = MutableSharedFlow<SendEvent>(extraBufferCapacity = 1)
|
||||||
|
val events: SharedFlow<SendEvent> = _events
|
||||||
|
|
||||||
private val _sendState = MutableStateFlow<SendState>(SendState.Idle)
|
private val _sendState = MutableStateFlow<SendState>(SendState.Idle)
|
||||||
val sendState: StateFlow<SendState> = _sendState
|
val sendState: StateFlow<SendState> = _sendState
|
||||||
@@ -372,7 +382,7 @@ class SendViewModel(
|
|||||||
when (raw.tag) {
|
when (raw.tag) {
|
||||||
"withdrawRequest" -> {
|
"withdrawRequest" -> {
|
||||||
_sendState.value = SendState.Idle
|
_sendState.value = SendState.Idle
|
||||||
onWithdrawScanned(raw, httpsUrl)
|
_events.tryEmit(SendEvent.WithdrawScanned(raw, httpsUrl))
|
||||||
}
|
}
|
||||||
"payRequest" -> {
|
"payRequest" -> {
|
||||||
lnurlCache.put(httpsUrl, raw)
|
lnurlCache.put(httpsUrl, raw)
|
||||||
@@ -464,7 +474,7 @@ class SendViewModel(
|
|||||||
|
|
||||||
when (raw.tag) {
|
when (raw.tag) {
|
||||||
"payRequest" -> _sendState.value = buildLnurlReadyState(raw, normalized)
|
"payRequest" -> _sendState.value = buildLnurlReadyState(raw, normalized)
|
||||||
"withdrawRequest" -> { _sendState.value = SendState.Idle; onWithdrawScanned(raw, normalized) }
|
"withdrawRequest" -> { _sendState.value = SendState.Idle; _events.tryEmit(SendEvent.WithdrawScanned(raw, normalized)) }
|
||||||
null -> _sendState.value = SendState.Error(strings.emptyResponse)
|
null -> _sendState.value = SendState.Error(strings.emptyResponse)
|
||||||
else -> _sendState.value = SendState.Error(strings.unsupportedType(raw.tag))
|
else -> _sendState.value = SendState.Error(strings.unsupportedType(raw.tag))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user