Skip to content

Commit 2786f24

Browse files
committed
feat: wire hardware connect flow
1 parent 1ebdd90 commit 2786f24

3 files changed

Lines changed: 70 additions & 28 deletions

File tree

app/src/main/java/to/bitkit/ui/sheets/hardware/HardwareSheet.kt

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,55 @@ package to.bitkit.ui.sheets.hardware
33
import androidx.compose.foundation.layout.Column
44
import androidx.compose.foundation.layout.fillMaxWidth
55
import androidx.compose.runtime.Composable
6+
import androidx.compose.runtime.DisposableEffect
7+
import androidx.compose.runtime.LaunchedEffect
8+
import androidx.compose.runtime.getValue
69
import androidx.compose.ui.Modifier
710
import androidx.compose.ui.platform.testTag
11+
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
12+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
813
import androidx.navigation.compose.NavHost
914
import androidx.navigation.compose.rememberNavController
1015
import kotlinx.serialization.Serializable
1116
import to.bitkit.ui.components.Sheet
1217
import to.bitkit.ui.components.SheetSize
18+
import to.bitkit.ui.navigateTo
1319
import to.bitkit.ui.shared.modifiers.sheetHeight
1420
import to.bitkit.ui.utils.composableWithDefaultTransitions
1521
import to.bitkit.viewmodels.AppViewModel
1622

1723
/**
18-
* Entry point for the hardware-wallet connect flow opened from the home suggestion card,
19-
* and host of the Pair Device screen shown app-wide when the device asks for its one-time pairing code.
24+
* Entry point for the hardware-wallet connect flow opened from the home suggestion card and the
25+
* Hardware Wallets settings Add button. Hosts the four connect steps (Intro -> Searching -> Found
26+
* -> Paired) plus the Pair Device step shown when the device asks for its one-time pairing code.
2027
*/
2128
@Composable
2229
fun HardwareSheet(
2330
sheet: Sheet.Hardware,
2431
appViewModel: AppViewModel,
25-
) {
26-
Content(
27-
sheet = sheet,
28-
onDismiss = appViewModel::hideSheet,
29-
onSubmitPairingCode = appViewModel::submitPairingCode,
30-
onCancelPairingCode = appViewModel::cancelPairingCode,
31-
)
32-
}
33-
34-
@Composable
35-
private fun Content(
36-
sheet: Sheet.Hardware,
37-
modifier: Modifier = Modifier,
38-
onDismiss: () -> Unit = {},
39-
onSubmitPairingCode: (String) -> Unit = {},
40-
onCancelPairingCode: () -> Unit = {},
32+
viewModel: HwConnectViewModel = hiltViewModel(),
4133
) {
4234
val navController = rememberNavController()
35+
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
36+
37+
DisposableEffect(Unit) {
38+
onDispose { viewModel.resetState() }
39+
}
40+
41+
LaunchedEffect(Unit) {
42+
viewModel.effects.collect { effect ->
43+
when (effect) {
44+
HwConnectEffect.NavigateToSearching -> navController.navigateTo(HardwareRoute.Searching)
45+
HwConnectEffect.NavigateToFound -> navController.navigateTo(HardwareRoute.Found)
46+
HwConnectEffect.NavigateToPairCode -> navController.navigateTo(HardwareRoute.PairCode)
47+
HwConnectEffect.NavigateToPaired -> navController.navigateTo(HardwareRoute.Paired)
48+
HwConnectEffect.Dismiss -> appViewModel.hideSheet()
49+
}
50+
}
51+
}
4352

4453
Column(
45-
modifier = modifier
54+
modifier = Modifier
4655
.fillMaxWidth()
4756
.sheetHeight(SheetSize.LARGE)
4857
.testTag("hardware_sheet")
@@ -52,12 +61,33 @@ private fun Content(
5261
startDestination = sheet.route,
5362
) {
5463
composableWithDefaultTransitions<HardwareRoute.Intro> {
55-
HwIntroSheet(onDismiss = onDismiss)
64+
HwIntroSheet(
65+
onContinue = viewModel::onIntroContinue,
66+
onCancel = appViewModel::hideSheet,
67+
)
68+
}
69+
composableWithDefaultTransitions<HardwareRoute.Searching> {
70+
HwSearchingSheet(onCancel = appViewModel::hideSheet)
71+
}
72+
composableWithDefaultTransitions<HardwareRoute.Found> {
73+
HwFoundSheet(
74+
deviceModel = uiState.deviceModel,
75+
isConnecting = uiState.isConnecting,
76+
onConnect = viewModel::onConnectClick,
77+
onCancel = appViewModel::hideSheet,
78+
)
79+
}
80+
composableWithDefaultTransitions<HardwareRoute.Paired> {
81+
HwPairedSheet(
82+
uiState = uiState,
83+
onLabelChange = viewModel::onLabelChange,
84+
onFinish = viewModel::onFinishClick,
85+
)
5686
}
5787
composableWithDefaultTransitions<HardwareRoute.PairCode> {
5888
HwPairCodeSheet(
59-
onSubmit = onSubmitPairingCode,
60-
onCancel = onCancelPairingCode,
89+
onSubmit = appViewModel::submitPairingCode,
90+
onCancel = appViewModel::cancelPairingCode,
6191
)
6292
}
6393
}
@@ -68,6 +98,15 @@ sealed interface HardwareRoute {
6898
@Serializable
6999
data object Intro : HardwareRoute
70100

101+
@Serializable
102+
data object Searching : HardwareRoute
103+
104+
@Serializable
105+
data object Found : HardwareRoute
106+
107+
@Serializable
108+
data object Paired : HardwareRoute
109+
71110
@Serializable
72111
data object PairCode : HardwareRoute
73112
}

app/src/main/java/to/bitkit/ui/sheets/hardware/HwIntroSheet.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ import to.bitkit.ui.utils.withAccent
3030
@Composable
3131
fun HwIntroSheet(
3232
modifier: Modifier = Modifier,
33-
onDismiss: () -> Unit = {},
33+
onContinue: () -> Unit = {},
34+
onCancel: () -> Unit = {},
3435
) {
3536
Content(
36-
onDismiss = onDismiss,
37+
onContinue = onContinue,
38+
onCancel = onCancel,
3739
modifier = modifier
3840
)
3941
}
4042

4143
@Composable
4244
private fun Content(
4345
modifier: Modifier = Modifier,
44-
onDismiss: () -> Unit = {},
46+
onContinue: () -> Unit = {},
47+
onCancel: () -> Unit = {},
4548
) {
4649
Column(
4750
modifier = modifier
@@ -71,13 +74,12 @@ private fun Content(
7174
) {
7275
SecondaryButton(
7376
text = stringResource(R.string.common__cancel),
74-
onClick = onDismiss,
77+
onClick = onCancel,
7578
modifier = Modifier.weight(1f)
7679
)
7780
PrimaryButton(
7881
text = stringResource(R.string.common__continue),
79-
onClick = {},
80-
enabled = false,
82+
onClick = onContinue,
8183
modifier = Modifier.weight(1f)
8284
)
8385
}

changelog.d/next/1027.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Connect a Trezor hardware wallet from the home suggestion card or Hardware Wallets settings to watch its balance.

0 commit comments

Comments
 (0)