@@ -47,6 +47,7 @@ class HwConnectViewModel @Inject constructor(
4747 val effects = _effects .asSharedFlow()
4848
4949 private var searchJob: Job ? = null
50+ private var connectJob: Job ? = null
5051 private var labelInitialized = false
5152 private var includeBluetoothInScan = true
5253 private var scanUsbBeforeConnect = false
@@ -85,26 +86,64 @@ class HwConnectViewModel @Inject constructor(
8586 fun onConnectClick (deviceIdOverride : String? = null) {
8687 val state = _uiState .value
8788 val deviceId = deviceIdOverride ? : state.foundDeviceId ? : return
89+ if (connectJob?.isActive == true ) return
8890 val shouldScanUsbBeforeConnect = scanUsbBeforeConnect
8991 searchJob?.cancel()
9092 _uiState .update { it.copy(isConnecting = true , errorMessage = null ) }
91- viewModelScope.launch {
93+ connectJob = viewModelScope.launch {
94+ var resolvedDeviceId = deviceId
95+ var resolvedDeviceModel = state.deviceModel
9296 if (shouldScanUsbBeforeConnect) {
9397 hwWalletRepo.scan(includeBluetooth = false )
94- }
95- hwWalletRepo.connect(deviceId)
96- .onSuccess { onConnected(deviceId, it) }
97- .onFailure {
98- _uiState .update { state ->
99- state.copy(
100- isConnecting = false ,
101- errorMessage = context.getString(R .string.hardware__connect_error),
102- )
98+ .onSuccess { devices ->
99+ devices.firstOrNull { it.id == deviceId || it.path == deviceId }?.let { device ->
100+ resolvedDeviceId = device.id
101+ resolvedDeviceModel = resolveHwWalletName(label = null , model = device.model)
102+ _uiState .update {
103+ it.copy(
104+ foundDeviceId = resolvedDeviceId,
105+ deviceModel = resolvedDeviceModel,
106+ )
107+ }
108+ }
103109 }
104- }
110+ .onFailure {
111+ onConnectFailed(resolvedDeviceId, resolvedDeviceModel)
112+ return @launch
113+ }
114+ }
115+ hwWalletRepo.connect(resolvedDeviceId)
116+ .onSuccess { onConnected(resolvedDeviceId, it) }
117+ .onFailure { onConnectFailed(resolvedDeviceId, resolvedDeviceModel) }
118+ connectJob = null
105119 }
106120 }
107121
122+ private fun onConnectFailed (deviceId : String , deviceModel : String ) {
123+ _uiState .update {
124+ it.copy(
125+ isConnecting = false ,
126+ foundDeviceId = deviceId,
127+ deviceModel = deviceModel,
128+ errorMessage = context.getString(R .string.hardware__connect_error),
129+ )
130+ }
131+ setEffect(
132+ HwConnectEffect .NavigateToFound (
133+ deviceId = deviceId,
134+ deviceModel = deviceModel,
135+ )
136+ )
137+ connectJob = null
138+ }
139+
140+ fun cancelConnect () {
141+ connectJob?.cancel()
142+ connectJob = null
143+ hwWalletRepo.cancelPairingCode()
144+ _uiState .update { it.copy(isConnecting = false ) }
145+ }
146+
108147 fun onLabelChange (value : String ) = _uiState .update { it.copy(labelInput = value.take(DEVICE_LABEL_MAX_LENGTH )) }
109148
110149 fun onFinishClick () {
@@ -122,6 +161,9 @@ class HwConnectViewModel @Inject constructor(
122161 fun resetState () {
123162 searchJob?.cancel()
124163 searchJob = null
164+ connectJob?.cancel()
165+ connectJob = null
166+ hwWalletRepo.cancelPairingCode()
125167 labelInitialized = false
126168 includeBluetoothInScan = true
127169 scanUsbBeforeConnect = false
0 commit comments