Skip to content

Commit 12b210a

Browse files
committed
feat: add account-type override to watcher
1 parent c3c5254 commit 12b210a

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,15 @@ class TrezorRepo @Inject constructor(
577577
extendedKey: String,
578578
network: BitkitCoreNetwork,
579579
gapLimit: UInt = 20u,
580+
accountType: AccountType? = null,
580581
): Result<Unit> = withContext(ioDispatcher) {
581582
runCatching {
582583
val params = WatcherParams(
583584
watcherId = watcherId,
584585
extendedKey = extendedKey,
585586
electrumUrl = electrumUrlForNetwork(network),
586587
network = network,
587-
accountType = null,
588+
accountType = accountType,
588589
gapLimit = gapLimit,
589590
)
590591
trezorService.startWatcher(params, eventBridge)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
4444
import androidx.navigation.NavController
4545
import com.google.accompanist.permissions.ExperimentalPermissionsApi
4646
import com.google.accompanist.permissions.rememberMultiplePermissionsState
47+
import com.synonym.bitkitcore.AccountType
4748
import com.synonym.bitkitcore.CoinSelection
4849
import kotlinx.collections.immutable.toImmutableList
4950
import to.bitkit.R
@@ -169,6 +170,7 @@ private fun TrezorScreenContent(
169170
onLookupTxHistory = viewModel::lookupTransactionHistory,
170171
onWatcherExtendedKeyChange = viewModel::setWatcherExtendedKey,
171172
onWatcherGapLimitChange = viewModel::setWatcherGapLimit,
173+
onWatcherAccountTypeChange = viewModel::setWatcherAccountType,
172174
onStartWatcher = viewModel::startWatcher,
173175
onStopWatcher = viewModel::stopWatcher,
174176
onPopulateWatcherFromXpub = viewModel::populateWatcherFromXpub,
@@ -214,6 +216,7 @@ private fun Content(
214216
onLookupTxHistory: () -> Unit = {},
215217
onWatcherExtendedKeyChange: (String) -> Unit = {},
216218
onWatcherGapLimitChange: (String) -> Unit = {},
219+
onWatcherAccountTypeChange: (AccountType?) -> Unit = {},
217220
onStartWatcher: () -> Unit = {},
218221
onStopWatcher: () -> Unit = {},
219222
onPopulateWatcherFromXpub: () -> Unit = {},
@@ -444,6 +447,7 @@ private fun Content(
444447
trezorState = trezorState,
445448
onExtendedKeyChange = onWatcherExtendedKeyChange,
446449
onGapLimitChange = onWatcherGapLimitChange,
450+
onAccountTypeChange = onWatcherAccountTypeChange,
447451
onStartWatcher = onStartWatcher,
448452
onStopWatcher = onStopWatcher,
449453
onPopulateFromXpub = onPopulateWatcherFromXpub,

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,10 @@ class TrezorViewModel @Inject constructor(
685685
_uiState.update { it.copy(watcher = it.watcher.copy(gapLimit = limit)) }
686686
}
687687

688+
fun setWatcherAccountType(type: AccountType?) {
689+
_uiState.update { it.copy(watcher = it.watcher.copy(selectedAccountType = type)) }
690+
}
691+
688692
fun populateWatcherFromXpub() {
689693
val xpub = trezorRepo.state.value.lastPublicKey?.xpub ?: return
690694
_uiState.update { it.copy(watcher = it.watcher.copy(extendedKey = xpub)) }
@@ -721,6 +725,7 @@ class TrezorViewModel @Inject constructor(
721725
extendedKey = key,
722726
network = state.selectedNetwork,
723727
gapLimit = gapLimit,
728+
accountType = state.watcher.selectedAccountType,
724729
)
725730

726731
if (result.isSuccess) {
@@ -979,6 +984,9 @@ data class TrezorUiState(
979984
val watcherAccountType: AccountType?
980985
get() = watcher.accountType
981986

987+
val watcherSelectedAccountType: AccountType?
988+
get() = watcher.selectedAccountType
989+
982990
val watcherEvents: ImmutableList<String>
983991
get() = watcher.events
984992
}
@@ -1045,6 +1053,7 @@ data class TrezorWatcherState(
10451053
val transactionCount: UInt = 0u,
10461054
val blockHeight: UInt = 0u,
10471055
val accountType: AccountType? = null,
1056+
val selectedAccountType: AccountType? = null,
10481057
val events: ImmutableList<String> = persistentListOf(),
10491058
)
10501059

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.compose.animation.shrinkVertically
88
import androidx.compose.foundation.background
99
import androidx.compose.foundation.layout.Arrangement
1010
import androidx.compose.foundation.layout.Column
11+
import androidx.compose.foundation.layout.FlowRow
1112
import androidx.compose.foundation.layout.Row
1213
import androidx.compose.foundation.layout.fillMaxWidth
1314
import androidx.compose.foundation.layout.heightIn
@@ -23,6 +24,7 @@ import androidx.compose.ui.draw.clip
2324
import androidx.compose.ui.graphics.Color
2425
import androidx.compose.ui.tooling.preview.Preview
2526
import androidx.compose.ui.unit.dp
27+
import com.synonym.bitkitcore.AccountType
2628
import com.synonym.bitkitcore.TxDirection
2729
import to.bitkit.models.safe
2830
import to.bitkit.repositories.TrezorState
@@ -33,6 +35,7 @@ import to.bitkit.ui.components.Footnote
3335
import to.bitkit.ui.components.HorizontalSpacer
3436
import to.bitkit.ui.components.PrimaryButton
3537
import to.bitkit.ui.components.SecondaryButton
38+
import to.bitkit.ui.components.TagButton
3639
import to.bitkit.ui.components.VerticalSpacer
3740
import to.bitkit.ui.theme.AppThemeSurface
3841
import to.bitkit.ui.theme.Colors
@@ -44,6 +47,7 @@ internal fun WatcherSection(
4447
trezorState: TrezorState,
4548
onExtendedKeyChange: (String) -> Unit,
4649
onGapLimitChange: (String) -> Unit,
50+
onAccountTypeChange: (AccountType?) -> Unit,
4751
onStartWatcher: () -> Unit,
4852
onStopWatcher: () -> Unit,
4953
onPopulateFromXpub: () -> Unit,
@@ -103,6 +107,13 @@ internal fun WatcherSection(
103107
)
104108
}
105109

110+
VerticalSpacer(8.dp)
111+
112+
AccountTypeSelectorRow(
113+
selectedAccountType = uiState.watcherSelectedAccountType,
114+
onAccountTypeChange = onAccountTypeChange,
115+
)
116+
106117
VerticalSpacer(16.dp)
107118

108119
if (uiState.activeWatcherId != null) {
@@ -134,6 +145,39 @@ internal fun WatcherSection(
134145
}
135146
}
136147

148+
private fun AccountType?.label(): String = when (this) {
149+
null -> "Auto"
150+
AccountType.LEGACY -> "Legacy"
151+
AccountType.WRAPPED_SEGWIT -> "Wrapped"
152+
AccountType.NATIVE_SEGWIT -> "Native"
153+
AccountType.TAPROOT -> "Taproot"
154+
}
155+
156+
@Composable
157+
private fun AccountTypeSelectorRow(
158+
selectedAccountType: AccountType?,
159+
onAccountTypeChange: (AccountType?) -> Unit,
160+
) {
161+
Column {
162+
Caption("Account type (Auto = detect from key prefix)", color = Colors.White50)
163+
VerticalSpacer(8.dp)
164+
FlowRow(
165+
horizontalArrangement = Arrangement.spacedBy(8.dp),
166+
verticalArrangement = Arrangement.spacedBy(8.dp),
167+
modifier = Modifier.fillMaxWidth()
168+
) {
169+
val options = listOf(null) + AccountType.entries
170+
options.forEach { type ->
171+
TagButton(
172+
text = type.label(),
173+
onClick = { onAccountTypeChange(type) },
174+
isSelected = type == selectedAccountType,
175+
)
176+
}
177+
}
178+
}
179+
}
180+
137181
private fun WatcherConnectionStatus.toColor(): Color = when (this) {
138182
WatcherConnectionStatus.IDLE -> Colors.White50
139183
WatcherConnectionStatus.STARTING -> Colors.Yellow
@@ -242,6 +286,7 @@ private fun PreviewWatcherEmpty() {
242286
trezorState = TrezorState(),
243287
onExtendedKeyChange = {},
244288
onGapLimitChange = {},
289+
onAccountTypeChange = {},
245290
onStartWatcher = {},
246291
onStopWatcher = {},
247292
onPopulateFromXpub = {},
@@ -258,6 +303,7 @@ private fun PreviewWatcherActive() {
258303
trezorState = TrezorState(),
259304
onExtendedKeyChange = {},
260305
onGapLimitChange = {},
306+
onAccountTypeChange = {},
261307
onStartWatcher = {},
262308
onStopWatcher = {},
263309
onPopulateFromXpub = {},

0 commit comments

Comments
 (0)