@@ -33,6 +33,7 @@ import kotlinx.coroutines.flow.launchIn
3333import kotlinx.coroutines.flow.onEach
3434import kotlinx.coroutines.flow.update
3535import kotlinx.coroutines.withContext
36+ import kotlinx.serialization.SerialName
3637import kotlinx.serialization.Serializable
3738import to.bitkit.data.TrezorStore
3839import to.bitkit.di.IoDispatcher
@@ -136,10 +137,7 @@ class TrezorRepo @Inject constructor(
136137 ? : _state .value.knownDevices.find { it.id == deviceId }?.let { known ->
137138 TrezorDeviceInfo (
138139 id = known.id,
139- transportType = when (known.transportType) {
140- " bluetooth" -> TrezorTransportType .BLUETOOTH
141- else -> TrezorTransportType .USB
142- },
140+ transportType = known.transportType.toCoreTransportType(),
143141 name = known.name,
144142 path = known.path,
145143 label = known.label,
@@ -153,8 +151,7 @@ class TrezorRepo @Inject constructor(
153151 _state .update {
154152 it.copy(
155153 isConnecting = false ,
156- connectedDevice = features,
157- connectedDeviceId = deviceId,
154+ connected = ConnectedTrezorDevice (id = deviceId, features = features),
158155 nearbyDevices = it.nearbyDevices.filter { d -> d.id != deviceId }.toImmutableList(),
159156 )
160157 }
@@ -323,7 +320,7 @@ class TrezorRepo @Inject constructor(
323320 TrezorDebugLog .log(" DISCONNECT" , " disconnect() called, connectedDeviceId=${_state .value.connectedDeviceId} " )
324321 val result = runCatching { trezorService.disconnect() }
325322 _state .update {
326- it.copy(connectedDevice = null , connectedDeviceId = null , lastAddress = null , lastPublicKey = null )
323+ it.copy(connected = null , lastAddress = null , lastPublicKey = null )
327324 }
328325 result.onSuccess {
329326 TrezorDebugLog .log(" DISCONNECT" , " disconnect() complete (credentials NOT cleared)" )
@@ -448,7 +445,7 @@ class TrezorRepo @Inject constructor(
448445 TrezorDebugLog .log(" RECONNECT" , " Connected! label=${features.label} , model=${features.model} " )
449446 addOrUpdateKnownDevice(device, features)
450447 _state .update {
451- it.copy(isConnecting = false , connectedDevice = features, connectedDeviceId = device.id)
448+ it.copy(isConnecting = false , connected = ConnectedTrezorDevice (id = device.id, features = features) )
452449 }
453450 TrezorDebugLog .log(" RECONNECT" , " === connectKnownDevice SUCCESS ===" )
454451 features
@@ -464,7 +461,7 @@ class TrezorRepo @Inject constructor(
464461 TrezorDebugLog .log(" FORGET" , " forgetDevice called for: $deviceId " )
465462 val disconnectResult = if (_state .value.connectedDeviceId == deviceId) {
466463 runCatching { trezorService.disconnect() }.also {
467- _state .update { it.copy(connectedDevice = null , connectedDeviceId = null ) }
464+ _state .update { it.copy(connected = null ) }
468465 }
469466 } else {
470467 Result .success(Unit )
@@ -497,7 +494,7 @@ class TrezorRepo @Inject constructor(
497494 if (knownDevice?.id == currentId || path.contains(currentId)) {
498495 Logger .warn(" External disconnect detected for '$currentId '" , context = TAG )
499496 _state .update {
500- it.copy(connectedDevice = null , connectedDeviceId = null , error = " Device disconnected" )
497+ it.copy(connected = null , error = " Device disconnected" )
501498 }
502499 }
503500 }.launchIn(scope)
@@ -509,10 +506,7 @@ class TrezorRepo @Inject constructor(
509506 id = deviceInfo.id,
510507 name = deviceInfo.name,
511508 path = deviceInfo.path,
512- transportType = when (deviceInfo.transportType) {
513- TrezorTransportType .BLUETOOTH -> " bluetooth"
514- TrezorTransportType .USB -> " usb"
515- },
509+ transportType = deviceInfo.transportType.toKnownTransportType(),
516510 label = features.label ? : deviceInfo.label,
517511 model = features.model ? : deviceInfo.model,
518512 lastConnectedAt = System .currentTimeMillis(),
@@ -548,7 +542,7 @@ class TrezorRepo @Inject constructor(
548542 val device = devices.find { it.id == deviceId }
549543 ? : throw AppError (" Device not found during reconnect" )
550544 val features = connectWithThpRetry(device.id)
551- _state .update { it.copy(connectedDevice = features, connectedDeviceId = deviceId ) }
545+ _state .update { it.copy(connected = ConnectedTrezorDevice (id = deviceId, features = features) ) }
552546 }
553547
554548 suspend fun clearCredentials (deviceId : String ): Result <Unit > = withContext(ioDispatcher) {
@@ -609,11 +603,22 @@ data class TrezorState(
609603 val isAutoReconnecting : Boolean = false ,
610604 val knownDevices : ImmutableList <KnownDevice > = persistentListOf(),
611605 val nearbyDevices : ImmutableList <TrezorDeviceInfo > = persistentListOf(),
612- val connectedDevice : TrezorFeatures ? = null ,
613- val connectedDeviceId : String? = null ,
606+ val connected : ConnectedTrezorDevice ? = null ,
614607 val lastAddress : TrezorAddressResponse ? = null ,
615608 val lastPublicKey : TrezorPublicKeyResponse ? = null ,
616609 val error : String? = null ,
610+ ) {
611+ val connectedDevice: TrezorFeatures ?
612+ get() = connected?.features
613+
614+ val connectedDeviceId: String?
615+ get() = connected?.id
616+ }
617+
618+ @Stable
619+ data class ConnectedTrezorDevice (
620+ val id : String ,
621+ val features : TrezorFeatures ,
617622)
618623
619624@Serializable
@@ -622,8 +627,27 @@ data class KnownDevice(
622627 val id : String ,
623628 val name : String? ,
624629 val path : String ,
625- val transportType : String ,
630+ val transportType : KnownDeviceTransportType ,
626631 val label : String? ,
627632 val model : String? ,
628633 val lastConnectedAt : Long ,
629634)
635+
636+ @Serializable
637+ enum class KnownDeviceTransportType {
638+ @SerialName(" bluetooth" )
639+ BLUETOOTH ,
640+
641+ @SerialName(" usb" )
642+ USB ,
643+ }
644+
645+ private fun TrezorTransportType.toKnownTransportType (): KnownDeviceTransportType = when (this ) {
646+ TrezorTransportType .BLUETOOTH -> KnownDeviceTransportType .BLUETOOTH
647+ TrezorTransportType .USB -> KnownDeviceTransportType .USB
648+ }
649+
650+ private fun KnownDeviceTransportType.toCoreTransportType (): TrezorTransportType = when (this ) {
651+ KnownDeviceTransportType .BLUETOOTH -> TrezorTransportType .BLUETOOTH
652+ KnownDeviceTransportType .USB -> TrezorTransportType .USB
653+ }
0 commit comments