Skip to content

Commit cd6c766

Browse files
committed
refactor: model trezor state
1 parent 6ba7053 commit cd6c766

5 files changed

Lines changed: 68 additions & 31 deletions

File tree

app/src/main/java/to/bitkit/repositories/TrezorRepo.kt

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import kotlinx.coroutines.flow.launchIn
3333
import kotlinx.coroutines.flow.onEach
3434
import kotlinx.coroutines.flow.update
3535
import kotlinx.coroutines.withContext
36+
import kotlinx.serialization.SerialName
3637
import kotlinx.serialization.Serializable
3738
import to.bitkit.data.TrezorStore
3839
import 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+
}

app/src/main/java/to/bitkit/ui/screens/trezor/DeviceListSection.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.synonym.bitkitcore.TrezorDeviceInfo
2222
import com.synonym.bitkitcore.TrezorTransportType
2323
import to.bitkit.R
2424
import to.bitkit.repositories.KnownDevice
25+
import to.bitkit.repositories.KnownDeviceTransportType
2526
import to.bitkit.ui.components.Caption
2627
import to.bitkit.ui.components.CaptionB
2728
import to.bitkit.ui.components.HorizontalSpacer
@@ -96,10 +97,9 @@ internal fun KnownDeviceCard(
9697
) {
9798
Icon(
9899
painter = painterResource(
99-
if (device.transportType == "bluetooth") {
100-
R.drawable.ic_broadcast
101-
} else {
102-
R.drawable.ic_git_branch
100+
when (device.transportType) {
101+
KnownDeviceTransportType.BLUETOOTH -> R.drawable.ic_broadcast
102+
KnownDeviceTransportType.USB -> R.drawable.ic_git_branch
103103
}
104104
),
105105
contentDescription = null,
@@ -119,7 +119,10 @@ internal fun KnownDeviceCard(
119119
verticalAlignment = Alignment.CenterVertically,
120120
) {
121121
Caption(
122-
text = if (device.transportType == "bluetooth") "Bluetooth" else "USB",
122+
text = when (device.transportType) {
123+
KnownDeviceTransportType.BLUETOOTH -> "Bluetooth"
124+
KnownDeviceTransportType.USB -> "USB"
125+
},
123126
color = Colors.White50,
124127
)
125128
Caption(

app/src/main/java/to/bitkit/ui/screens/trezor/TrezorPreviewData.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import com.synonym.bitkitcore.TrezorSignedTx
1717
import com.synonym.bitkitcore.TrezorTransportType
1818
import com.synonym.bitkitcore.TxDirection
1919
import com.synonym.bitkitcore.WalletBalance
20+
import to.bitkit.repositories.ConnectedTrezorDevice
2021
import to.bitkit.repositories.KnownDevice
22+
import to.bitkit.repositories.KnownDeviceTransportType
2123
import to.bitkit.repositories.TrezorState
2224
import com.synonym.bitkitcore.Network as BitkitCoreNetwork
2325

@@ -55,7 +57,7 @@ internal object TrezorPreviewData {
5557
id = "usb-1",
5658
name = "Trezor Safe 5",
5759
path = "/dev/usb/001",
58-
transportType = "usb",
60+
transportType = KnownDeviceTransportType.USB,
5961
label = "My Savings",
6062
model = "Safe 5",
6163
lastConnectedAt = 1_700_000_000_000L,
@@ -65,7 +67,7 @@ internal object TrezorPreviewData {
6567
id = "ble-1",
6668
name = "Trezor Safe 7",
6769
path = "AA:BB:CC:DD:EE:FF",
68-
transportType = "bluetooth",
70+
transportType = KnownDeviceTransportType.BLUETOOTH,
6971
label = "Daily Wallet",
7072
model = "Safe 7",
7173
lastConnectedAt = 1_700_000_000_000L,
@@ -182,14 +184,18 @@ internal object TrezorPreviewData {
182184

183185
val connectedState = TrezorState(
184186
isInitialized = true,
185-
connectedDevice = sampleFeatures,
186-
connectedDeviceId = "trezor-abc123",
187+
connected = ConnectedTrezorDevice(
188+
id = "trezor-abc123",
189+
features = sampleFeatures,
190+
),
187191
)
188192

189193
val connectedStateWithResults = TrezorState(
190194
isInitialized = true,
191-
connectedDevice = sampleFeatures,
192-
connectedDeviceId = "trezor-abc123",
195+
connected = ConnectedTrezorDevice(
196+
id = "trezor-abc123",
197+
features = sampleFeatures,
198+
),
193199
lastAddress = sampleAddressResponse,
194200
lastPublicKey = samplePublicKeyResponse,
195201
)

app/src/main/java/to/bitkit/ui/screens/trezor/TrezorScreen.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import com.google.accompanist.permissions.rememberMultiplePermissionsState
4747
import com.synonym.bitkitcore.CoinSelection
4848
import kotlinx.collections.immutable.toImmutableList
4949
import to.bitkit.R
50+
import to.bitkit.repositories.ConnectedTrezorDevice
5051
import to.bitkit.repositories.KnownDevice
5152
import to.bitkit.repositories.TrezorState
5253
import to.bitkit.services.TrezorDebugLog
@@ -660,7 +661,10 @@ private fun PreviewWithDevices() {
660661
isInitialized = true,
661662
knownDevices = listOf(TrezorPreviewData.sampleKnownDevice).toImmutableList(),
662663
nearbyDevices = listOf(TrezorPreviewData.sampleNearbyDevice).toImmutableList(),
663-
connectedDeviceId = TrezorPreviewData.sampleKnownDevice.id,
664+
connected = ConnectedTrezorDevice(
665+
id = TrezorPreviewData.sampleKnownDevice.id,
666+
features = TrezorPreviewData.sampleFeatures,
667+
),
664668
),
665669
uiState = TrezorUiState(),
666670
)

app/src/test/java/to/bitkit/repositories/TrezorRepoTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class TrezorRepoTest : BaseUnitTest() {
113113
id = id,
114114
name = name,
115115
path = path,
116-
transportType = "usb",
116+
transportType = KnownDeviceTransportType.USB,
117117
label = label,
118118
model = model,
119119
lastConnectedAt = 123L,

0 commit comments

Comments
 (0)