|
| 1 | +package to.bitkit.ui.sheets.hardware |
| 2 | + |
| 3 | +import androidx.compose.runtime.Immutable |
| 4 | +import androidx.lifecycle.ViewModel |
| 5 | +import androidx.lifecycle.viewModelScope |
| 6 | +import com.synonym.bitkitcore.TrezorFeatures |
| 7 | +import dagger.hilt.android.lifecycle.HiltViewModel |
| 8 | +import kotlinx.coroutines.Job |
| 9 | +import kotlinx.coroutines.delay |
| 10 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 11 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 12 | +import kotlinx.coroutines.flow.asSharedFlow |
| 13 | +import kotlinx.coroutines.flow.asStateFlow |
| 14 | +import kotlinx.coroutines.flow.update |
| 15 | +import kotlinx.coroutines.isActive |
| 16 | +import kotlinx.coroutines.launch |
| 17 | +import to.bitkit.repositories.HwWalletRepo |
| 18 | +import to.bitkit.repositories.resolveHwWalletName |
| 19 | +import javax.inject.Inject |
| 20 | +import kotlin.time.Duration.Companion.seconds |
| 21 | + |
| 22 | +/** |
| 23 | + * Backs the Connect Hardware bottom-sheet flow (Intro -> Searching -> Found -> Paired). Drives |
| 24 | + * device discovery, connection and the Bitkit-side funds label through [HwWalletRepo], emitting |
| 25 | + * [HwConnectEffect]s that the sheet collects to navigate its inner [HardwareRoute] graph. The |
| 26 | + * one-time pairing code, when the device requests it during connect, is surfaced inline by |
| 27 | + * navigating to [HardwareRoute.PairCode]. |
| 28 | + */ |
| 29 | +@HiltViewModel |
| 30 | +class HwConnectViewModel @Inject constructor( |
| 31 | + private val hwWalletRepo: HwWalletRepo, |
| 32 | +) : ViewModel() { |
| 33 | + companion object { |
| 34 | + private val SCAN_INTERVAL = 2.seconds |
| 35 | + } |
| 36 | + |
| 37 | + private val _uiState = MutableStateFlow(HwConnectUiState()) |
| 38 | + val uiState = _uiState.asStateFlow() |
| 39 | + |
| 40 | + private val _effects = MutableSharedFlow<HwConnectEffect>(extraBufferCapacity = 1) |
| 41 | + val effects = _effects.asSharedFlow() |
| 42 | + |
| 43 | + private var searchJob: Job? = null |
| 44 | + private var labelInitialized = false |
| 45 | + |
| 46 | + init { |
| 47 | + observePairingCode() |
| 48 | + observeConnectedWallet() |
| 49 | + } |
| 50 | + |
| 51 | + fun onIntroContinue() { |
| 52 | + setEffect(HwConnectEffect.NavigateToSearching) |
| 53 | + startSearching() |
| 54 | + } |
| 55 | + |
| 56 | + fun onConnectClick() { |
| 57 | + val deviceId = _uiState.value.foundDeviceId ?: return |
| 58 | + searchJob?.cancel() |
| 59 | + _uiState.update { it.copy(isConnecting = true) } |
| 60 | + viewModelScope.launch { |
| 61 | + hwWalletRepo.connect(deviceId) |
| 62 | + .onSuccess { onConnected(deviceId, it) } |
| 63 | + .onFailure { _uiState.update { state -> state.copy(isConnecting = false) } } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fun onLabelChange(value: String) = _uiState.update { it.copy(labelInput = value) } |
| 68 | + |
| 69 | + fun onFinishClick() { |
| 70 | + val deviceId = _uiState.value.pairedDeviceId |
| 71 | + if (deviceId == null) { |
| 72 | + setEffect(HwConnectEffect.Dismiss) |
| 73 | + return |
| 74 | + } |
| 75 | + viewModelScope.launch { |
| 76 | + hwWalletRepo.setDeviceLabel(deviceId, _uiState.value.labelInput) |
| 77 | + setEffect(HwConnectEffect.Dismiss) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + fun resetState() { |
| 82 | + searchJob?.cancel() |
| 83 | + searchJob = null |
| 84 | + labelInitialized = false |
| 85 | + _uiState.update { HwConnectUiState() } |
| 86 | + } |
| 87 | + |
| 88 | + private fun startSearching() { |
| 89 | + if (searchJob?.isActive == true) return |
| 90 | + _uiState.update { it.copy(isSearching = true) } |
| 91 | + searchJob = viewModelScope.launch { |
| 92 | + while (isActive) { |
| 93 | + hwWalletRepo.scan() |
| 94 | + val device = hwWalletRepo.deviceState.value.nearbyDevices.firstOrNull() |
| 95 | + if (device != null) { |
| 96 | + _uiState.update { |
| 97 | + it.copy( |
| 98 | + isSearching = false, |
| 99 | + foundDeviceId = device.id, |
| 100 | + deviceModel = resolveHwWalletName(label = null, model = device.model), |
| 101 | + ) |
| 102 | + } |
| 103 | + setEffect(HwConnectEffect.NavigateToFound) |
| 104 | + return@launch |
| 105 | + } |
| 106 | + delay(SCAN_INTERVAL) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private fun onConnected(deviceId: String, features: TrezorFeatures) { |
| 112 | + val name = resolveHwWalletName(label = features.label, model = features.model) |
| 113 | + _uiState.update { |
| 114 | + it.copy( |
| 115 | + isConnecting = false, |
| 116 | + pairedDeviceId = deviceId, |
| 117 | + deviceName = name, |
| 118 | + labelInput = if (labelInitialized) it.labelInput else name, |
| 119 | + ) |
| 120 | + } |
| 121 | + labelInitialized = true |
| 122 | + setEffect(HwConnectEffect.NavigateToPaired) |
| 123 | + } |
| 124 | + |
| 125 | + private fun observePairingCode() { |
| 126 | + viewModelScope.launch { |
| 127 | + hwWalletRepo.needsPairingCode.collect { needsCode -> |
| 128 | + if (needsCode) setEffect(HwConnectEffect.NavigateToPairCode) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private fun observeConnectedWallet() { |
| 134 | + viewModelScope.launch { |
| 135 | + hwWalletRepo.wallets.collect { wallets -> |
| 136 | + val deviceId = _uiState.value.pairedDeviceId ?: return@collect |
| 137 | + val wallet = wallets.firstOrNull { deviceId == it.id || deviceId in it.deviceIds } ?: return@collect |
| 138 | + _uiState.update { |
| 139 | + it.copy( |
| 140 | + deviceName = wallet.name, |
| 141 | + balanceSats = wallet.balanceSats, |
| 142 | + labelInput = if (labelInitialized) it.labelInput else wallet.name, |
| 143 | + ) |
| 144 | + } |
| 145 | + labelInitialized = true |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private fun setEffect(effect: HwConnectEffect) = viewModelScope.launch { _effects.emit(effect) } |
| 151 | +} |
| 152 | + |
| 153 | +@Immutable |
| 154 | +data class HwConnectUiState( |
| 155 | + val isSearching: Boolean = false, |
| 156 | + val isConnecting: Boolean = false, |
| 157 | + val foundDeviceId: String? = null, |
| 158 | + val pairedDeviceId: String? = null, |
| 159 | + val deviceName: String = "", |
| 160 | + val deviceModel: String = "", |
| 161 | + val balanceSats: ULong = 0uL, |
| 162 | + val labelInput: String = "", |
| 163 | +) |
| 164 | + |
| 165 | +sealed interface HwConnectEffect { |
| 166 | + data object NavigateToSearching : HwConnectEffect |
| 167 | + data object NavigateToFound : HwConnectEffect |
| 168 | + data object NavigateToPairCode : HwConnectEffect |
| 169 | + data object NavigateToPaired : HwConnectEffect |
| 170 | + data object Dismiss : HwConnectEffect |
| 171 | +} |
0 commit comments