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.fiat.FiatViewModel
|
||||
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.SendViewModel
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
@@ -110,6 +111,12 @@ class WalletViewModel(
|
||||
) = sendVm.requestPayment(activity, subtitle, strings, pay)
|
||||
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
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
|
||||
@@ -46,9 +46,6 @@ class WalletViewModelFactory(private val app: Application) : ViewModelProvider.F
|
||||
aliasRepo = aliasRepo,
|
||||
lnurlCache = lnurlCache,
|
||||
balanceVm = balanceVm,
|
||||
onWithdrawScanned = { raw, lnurl ->
|
||||
receiveVm.handleWithdrawResponse(raw, lnurl)
|
||||
},
|
||||
scanner = lnurlScanner,
|
||||
payer = lnurlPayer,
|
||||
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(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
|
||||
@@ -21,12 +21,21 @@ import com.bitcointxoko.gudariwallet.util.SendInputType
|
||||
import com.bitcointxoko.gudariwallet.util.WalletConstants
|
||||
import com.bitcointxoko.gudariwallet.util.lnurlProtocolError
|
||||
import com.bitcointxoko.gudariwallet.util.parseApiError
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
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 ───────────────────────────────────
|
||||
/**
|
||||
* All user-visible strings produced by [SendViewModel].
|
||||
@@ -53,11 +62,12 @@ class SendViewModel(
|
||||
private val aliasRepo : NodeAliasRepository,
|
||||
private val lnurlCache : LnurlCacheRepository,
|
||||
private val balanceVm : BalanceViewModel,
|
||||
private val onWithdrawScanned: (raw: LnurlScanResponse, lnurl: String) -> Unit,
|
||||
private val scanner : LnurlScanner,
|
||||
private val payer : LnurlPayer,
|
||||
private val poller : PaymentPoller,
|
||||
) : ViewModel() {
|
||||
private val _events = MutableSharedFlow<SendEvent>(extraBufferCapacity = 1)
|
||||
val events: SharedFlow<SendEvent> = _events
|
||||
|
||||
private val _sendState = MutableStateFlow<SendState>(SendState.Idle)
|
||||
val sendState: StateFlow<SendState> = _sendState
|
||||
@@ -372,7 +382,7 @@ class SendViewModel(
|
||||
when (raw.tag) {
|
||||
"withdrawRequest" -> {
|
||||
_sendState.value = SendState.Idle
|
||||
onWithdrawScanned(raw, httpsUrl)
|
||||
_events.tryEmit(SendEvent.WithdrawScanned(raw, httpsUrl))
|
||||
}
|
||||
"payRequest" -> {
|
||||
lnurlCache.put(httpsUrl, raw)
|
||||
@@ -464,7 +474,7 @@ class SendViewModel(
|
||||
|
||||
when (raw.tag) {
|
||||
"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)
|
||||
else -> _sendState.value = SendState.Error(strings.unsupportedType(raw.tag))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user