Skip to content

Commit afe55fb

Browse files
authored
Android: navigation refactor for asset pickers and screen actions (#1947)
1 parent 1f58c57 commit afe55fb

18 files changed

Lines changed: 154 additions & 112 deletions

File tree

‎android/app/src/main/kotlin/com/gemwallet/android/ui/navigation/WalletNavGraph.kt‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.gemwallet.android.features.onboarding.presents.import_wallet.importWa
2727
import com.gemwallet.android.features.onboarding.presents.terms.acceptTermsScreen
2828
import com.gemwallet.android.features.transactions.presents.transaction.TransactionAction
2929
import com.gemwallet.android.features.wallet.presents.WalletSearchAction
30+
import com.gemwallet.android.features.wallet.presents.WalletsAction
3031
import com.gemwallet.android.ui.components.animation.navigationSlideTransition
3132
import com.gemwallet.android.ui.models.actions.AmountTransactionAction
3233
import com.gemwallet.android.ui.models.actions.ConfirmTransactionAction
@@ -211,12 +212,16 @@ fun WalletNavGraph(
211212
)
212213

213214
walletsScreen(
214-
onCreateWallet = navigator::openCreateWalletSecurityReminder,
215-
onImportWallet = navigator::openImportWallet,
216-
onEditWallet = navigator::openWalletDetail,
215+
onAction = { action ->
216+
when (action) {
217+
WalletsAction.Create -> navigator.openCreateWalletSecurityReminder()
218+
WalletsAction.Import -> navigator.openImportWallet()
219+
is WalletsAction.Edit -> navigator.openWalletDetail(action.walletId)
220+
WalletsAction.Cancel -> onCancel()
221+
}
222+
},
217223
onSelectWallet = navigator::resetToWallet,
218224
onBoard = navigator::resetToOnboarding,
219-
onCancel = onCancel,
220225
)
221226

222227
walletDetailScreen(

‎android/app/src/main/kotlin/com/gemwallet/android/ui/navigation/WalletNavigator.kt‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import com.gemwallet.android.ui.navigation.routes.PerpetualRoute
7171
import com.gemwallet.android.ui.navigation.routes.PerpetualsRoute
7272
import com.gemwallet.android.ui.navigation.routes.PortfolioRoute
7373
import com.gemwallet.android.ui.navigation.routes.PreferencesRoute
74+
import com.gemwallet.android.ui.navigation.routes.PriceAlertSelectRoute
7475
import com.gemwallet.android.ui.navigation.routes.PriceAlertsRoute
7576
import com.gemwallet.android.ui.navigation.routes.PrivateKeyAccountsRoute
7677
import com.gemwallet.android.ui.navigation.routes.PrivateKeyWarningRoute
@@ -134,7 +135,7 @@ class WalletNavigator(
134135
) {
135136
private val routeMessages = mutableStateMapOf<NavKey, RouteMessage>()
136137
private val swapSelections = mutableStateMapOf<NavKey, SwapSelection>()
137-
private val paymentSelections = mutableStateMapOf<NavKey, AssetId>()
138+
private val assetSelections = mutableStateMapOf<NavKey, AssetId>()
138139

139140
private fun push(route: NavKey): Boolean {
140141
if (backStack.lastOrNull() == route) return true
@@ -215,15 +216,15 @@ class WalletNavigator(
215216

216217
fun swapSelection(route: NavKey): SwapSelection? = swapSelections[route]
217218

218-
fun paymentSelection(route: NavKey): AssetId? = paymentSelections[route]
219+
fun assetSelection(route: NavKey): AssetId? = assetSelections[route]
219220

220-
fun clearPaymentSelection(route: NavKey) {
221-
paymentSelections.remove(route)
221+
fun clearAssetSelection(route: NavKey) {
222+
assetSelections.remove(route)
222223
}
223224

224-
fun openPaymentSelect(assetIds: List<AssetId>) = push(PaymentSelectRoute(assetIds))
225+
fun finishAssetSelect(assetId: AssetId) = popWithResult(assetSelections, assetId)
225226

226-
fun finishPaymentSelect(assetId: AssetId) = popWithResult(paymentSelections, assetId)
227+
fun openPaymentSelect(assetIds: List<AssetId>) = push(PaymentSelectRoute(assetIds))
227228

228229
fun clearSwapSelection(route: NavKey) {
229230
swapSelections.remove(route)
@@ -313,6 +314,7 @@ class WalletNavigator(
313314
fun openPriceAlerts() = push(PriceAlertsRoute)
314315
fun openPriceAlerts(assetId: AssetId) = push(AssetPriceAlertsRoute(assetId))
315316
fun openSetPriceAlert(assetId: AssetId) = push(SetPriceAlertRoute(assetId))
317+
fun openPriceAlertSelect() = push(PriceAlertSelectRoute)
316318
fun openPerpetuals() = push(PerpetualsRoute)
317319
fun openPerpetual(assetId: AssetId) = push(PerpetualRoute(assetId))
318320

@@ -410,7 +412,7 @@ class WalletNavigator(
410412

411413
private fun clearTransientState() {
412414
clearSwapSelections()
413-
paymentSelections.clear()
415+
assetSelections.clear()
414416
routeMessages.clear()
415417
}
416418

‎android/app/src/main/kotlin/com/gemwallet/android/ui/navigation/routes/ConfirmTransfer.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ fun EntryProviderScope<NavKey>.confirmTransfer(navigator: WalletNavigator, finis
4040
input = input,
4141
cancelAction = cancelAction,
4242
onGetAsset = onGetAsset,
43-
paymentAsset = navigator.paymentSelection(key),
44-
onPaymentAssetConsumed = { navigator.clearPaymentSelection(key) },
43+
paymentAsset = navigator.assetSelection(key),
44+
onPaymentAssetConsumed = { navigator.clearAssetSelection(key) },
4545
onSelectPaymentAsset = navigator::openPaymentSelect,
4646
onOpenAddress = navigator::openAddress,
4747
finishAction = finishAction,
@@ -52,7 +52,7 @@ fun EntryProviderScope<NavKey>.confirmTransfer(navigator: WalletNavigator, finis
5252
SelectAssetScreen(
5353
selectType = GemSelectAssetType.Payment(key.assetIds.map { it.toIdentifier() }),
5454
onCancel = cancelAction::invoke,
55-
onSelect = navigator::finishPaymentSelect,
55+
onSelect = navigator::finishAssetSelect,
5656
)
5757
}
5858

‎android/app/src/main/kotlin/com/gemwallet/android/ui/navigation/routes/Settings.kt‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import androidx.compose.ui.platform.LocalUriHandler
99
import androidx.compose.ui.platform.UriHandler
1010
import androidx.navigation3.runtime.EntryProviderScope
1111
import androidx.navigation3.runtime.NavKey
12+
import com.gemwallet.android.features.assets.presents.select.SelectAssetScreen
1213
import com.gemwallet.android.features.in_app_notifications.presents.InAppNotificationsAction
1314
import com.gemwallet.android.features.in_app_notifications.presents.InAppNotificationsScreen
1415
import com.gemwallet.android.features.price_alerts.presents.AssetPriceAlertsScreen
16+
import com.gemwallet.android.features.price_alerts.presents.PriceAlertsAction
1517
import com.gemwallet.android.features.price_alerts.presents.PriceAlertsScreen
1618
import com.gemwallet.android.features.price_alerts.presents.SetPriceAlertScreen
19+
import com.gemwallet.android.features.settings.presents.NotificationsAction
1720
import com.gemwallet.android.features.settings.presents.NotificationsScreen
1821
import com.gemwallet.android.features.settings.presents.PreferencesScreen
1922
import com.gemwallet.android.features.settings.presents.about_us.AboutUsScreen
23+
import com.gemwallet.android.features.settings.presents.chain_settings.ChainListSettingsAction
2024
import com.gemwallet.android.features.settings.presents.chain_settings.ChainListSettingsScreen
2125
import com.gemwallet.android.features.settings.presents.chain_settings.ChainSettingsScreen
2226
import com.gemwallet.android.features.settings.presents.chain_settings.ServiceStatusScreen
@@ -35,6 +39,7 @@ import com.wallet.core.primitives.AssetId
3539
import com.wallet.core.primitives.Chain
3640
import kotlinx.serialization.Serializable
3741
import uniffi.gemstone.GemNotificationDestination
42+
import uniffi.gemstone.GemSelectAssetType
3843

3944
const val SettingsRoute = "settings"
4045

@@ -68,6 +73,9 @@ data object ServiceStatusRoute : NavKey
6873
@Serializable
6974
data object PriceAlertsRoute : NavKey
7075

76+
@Serializable
77+
data object PriceAlertSelectRoute : NavKey
78+
7179
@Serializable
7280
data class AssetPriceAlertsRoute(val assetId: AssetId) : NavKey
7381

@@ -132,9 +140,13 @@ fun EntryProviderScope<NavKey>.settingsScreen(navigator: WalletNavigator, onPaym
132140

133141
entry<ChainListSettingsRoute> {
134142
ChainListSettingsScreen(
135-
onSelectChain = navigator::openChainSettings,
136-
onShowStatus = navigator::openServiceStatus,
137-
onCancel = onCancel,
143+
onAction = { action ->
144+
when (action) {
145+
is ChainListSettingsAction.Select -> navigator.openChainSettings(action.chain)
146+
ChainListSettingsAction.ShowStatus -> navigator.openServiceStatus()
147+
ChainListSettingsAction.Cancel -> onCancel()
148+
}
149+
},
138150
)
139151
}
140152

@@ -152,8 +164,24 @@ fun EntryProviderScope<NavKey>.settingsScreen(navigator: WalletNavigator, onPaym
152164
PriceAlertsScreen(
153165
message = navigator.routeMessage(key),
154166
onMessageShown = { navigator.clearRouteMessage(key) },
155-
onChart = navigator::openChart,
167+
selectedAsset = navigator.assetSelection(key),
168+
onSelectedAssetConsumed = { navigator.clearAssetSelection(key) },
169+
onAction = { action ->
170+
when (action) {
171+
PriceAlertsAction.Close -> onCancel()
172+
PriceAlertsAction.Add -> navigator.openPriceAlertSelect()
173+
is PriceAlertsAction.OpenChart -> navigator.openChart(action.assetId)
174+
}
175+
},
176+
)
177+
}
178+
179+
entry<PriceAlertSelectRoute> {
180+
SelectAssetScreen(
181+
selectType = GemSelectAssetType.PriceAlert,
156182
onCancel = onCancel,
183+
onSelect = navigator::finishAssetSelect,
184+
onSelectRecent = null,
157185
)
158186
}
159187

@@ -179,8 +207,12 @@ fun EntryProviderScope<NavKey>.settingsScreen(navigator: WalletNavigator, onPaym
179207

180208
entry<NotificationsRoute> {
181209
NotificationsScreen(
182-
onPriceAlerts = navigator::openPriceAlerts,
183-
onCancel = onCancel,
210+
onAction = { action ->
211+
when (action) {
212+
NotificationsAction.PriceAlerts -> navigator.openPriceAlerts()
213+
NotificationsAction.Cancel -> onCancel()
214+
}
215+
},
184216
)
185217
}
186218

‎android/app/src/main/kotlin/com/gemwallet/android/ui/navigation/routes/WalletsRoute.kt‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ package com.gemwallet.android.ui.navigation.routes
22

33
import androidx.navigation3.runtime.EntryProviderScope
44
import androidx.navigation3.runtime.NavKey
5+
import com.gemwallet.android.features.wallet.presents.WalletsAction
56
import com.gemwallet.android.features.wallet.presents.WalletsScreen
6-
import com.wallet.core.primitives.WalletId
77
import kotlinx.serialization.Serializable
88

99
@Serializable
1010
data object WalletsRoute : NavKey
1111

12-
fun EntryProviderScope<NavKey>.walletsScreen(onCancel: () -> Unit, onCreateWallet: () -> Unit, onImportWallet: () -> Unit, onEditWallet: (WalletId) -> Unit, onSelectWallet: () -> Unit, onBoard: () -> Unit) {
12+
fun EntryProviderScope<NavKey>.walletsScreen(onAction: (WalletsAction.Navigation) -> Unit, onSelectWallet: () -> Unit, onBoard: () -> Unit) {
1313
entry<WalletsRoute> {
1414
WalletsScreen(
15-
onCreateWallet = onCreateWallet,
16-
onImportWallet = onImportWallet,
17-
onEditWallet = onEditWallet,
15+
onAction = onAction,
1816
onSelectWallet = onSelectWallet,
1917
onBoard = onBoard,
20-
onCancel = onCancel,
2118
)
2219
}
2320
}

‎android/app/src/test/kotlin/com/gemwallet/android/ui/navigation/WalletNavigatorTest.kt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ import com.gemwallet.android.ui.navigation.routes.ConfirmTransferRoute
3434
import com.gemwallet.android.ui.navigation.routes.DelegationRoute
3535
import com.gemwallet.android.ui.navigation.routes.ExportPrivateKeyRoute
3636
import com.gemwallet.android.ui.navigation.routes.ExportWalletRoute
37+
import com.gemwallet.android.ui.navigation.routes.PaymentSelectRoute
3738
import com.gemwallet.android.ui.navigation.routes.PerpetualDepositSelectRoute
3839
import com.gemwallet.android.ui.navigation.routes.PerpetualRoute
3940
import com.gemwallet.android.ui.navigation.routes.PerpetualsRoute
41+
import com.gemwallet.android.ui.navigation.routes.PriceAlertSelectRoute
42+
import com.gemwallet.android.ui.navigation.routes.PriceAlertsRoute
4043
import com.gemwallet.android.ui.navigation.routes.PrivateKeyAccountsRoute
4144
import com.gemwallet.android.ui.navigation.routes.PrivateKeyWarningRoute
4245
import com.gemwallet.android.ui.navigation.routes.ReceiveRoute
@@ -494,6 +497,34 @@ class WalletNavigatorTest {
494497
assertNull(navigator.swapSelection(route))
495498
}
496499

500+
@Test
501+
fun finishAssetSelect_popsSelectorAndStoresSelectionForTargetRoute() {
502+
val selectedAssetId = mockAssetId(Chain.Solana)
503+
val navigator = navigatorWith(WalletRootRoute, PriceAlertsRoute, PriceAlertSelectRoute)
504+
505+
navigator.finishAssetSelect(selectedAssetId)
506+
507+
assertEquals(listOf(WalletRootRoute, PriceAlertsRoute), navigator.backStack.toList())
508+
assertEquals(selectedAssetId, navigator.assetSelection(PriceAlertsRoute))
509+
510+
navigator.clearAssetSelection(PriceAlertsRoute)
511+
512+
assertNull(navigator.assetSelection(PriceAlertsRoute))
513+
}
514+
515+
@Test
516+
fun finishAssetSelect_fromPaymentSelectStoresSelectionForConfirm() {
517+
val confirm = ConfirmTransferRoute("confirm")
518+
val selectedAssetId = mockAssetId(Chain.Ethereum)
519+
val navigator = navigatorWith(WalletRootRoute, PriceAlertsRoute, confirm, PaymentSelectRoute(listOf(selectedAssetId)))
520+
521+
navigator.finishAssetSelect(selectedAssetId)
522+
523+
assertEquals(listOf(WalletRootRoute, PriceAlertsRoute, confirm), navigator.backStack.toList())
524+
assertEquals(selectedAssetId, navigator.assetSelection(confirm))
525+
assertNull(navigator.assetSelection(PriceAlertsRoute))
526+
}
527+
497528
@Test
498529
fun popConfirmFlow_popsTransferFlowToAsset() {
499530
val assetId = mockAssetId(Chain.Solana)

‎android/features/assets/presents/src/main/kotlin/com/gemwallet/android/features/assets/presents/add/AddAssetScreen.kt‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package com.gemwallet.android.features.assets.presents.add
22

33
import androidx.activity.compose.BackHandler
44
import androidx.compose.animation.AnimatedContent
5+
import androidx.compose.foundation.text.input.rememberTextFieldState
56
import androidx.compose.runtime.Composable
67
import androidx.compose.runtime.LaunchedEffect
78
import androidx.compose.runtime.getValue
9+
import androidx.compose.runtime.remember
810
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
911
import androidx.lifecycle.compose.collectAsStateWithLifecycle
1012
import com.gemwallet.android.ext.asset
@@ -21,7 +23,6 @@ import com.wallet.core.primitives.QRScanType
2123
fun AddAssetScreen(onFinish: () -> Unit, onCancel: () -> Unit, viewModel: AddAssetViewModel = hiltViewModel()) {
2224
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
2325
val showsChainPicker by viewModel.showsChainPicker.collectAsStateWithLifecycle()
24-
val chains by viewModel.chains.collectAsStateWithLifecycle()
2526
val network by viewModel.selectedChain.collectAsStateWithLifecycle()
2627
val sections by viewModel.sections.collectAsStateWithLifecycle()
2728
val isSearching by viewModel.isSearching.collectAsStateWithLifecycle()
@@ -50,9 +51,11 @@ fun AddAssetScreen(onFinish: () -> Unit, onCancel: () -> Unit, viewModel: AddAss
5051
label = "phrase",
5152
) { isSelectChain ->
5253
if (isSelectChain) {
54+
val chainFilter = rememberTextFieldState()
55+
val query = chainFilter.text.toString()
5356
SelectChain(
54-
list = chains,
55-
chainFilter = viewModel.chainFilter,
57+
list = remember(query) { viewModel.chains(query) },
58+
chainFilter = chainFilter,
5659
onSelect = viewModel::setChain,
5760
onCancel = viewModel::cancelSelectChain,
5861
)

‎android/features/assets/viewmodels/src/main/kotlin/com/gemwallet/android/features/assets/viewmodels/add/AddAssetViewModel.kt‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.gemwallet.android.features.assets.viewmodels.add
22

33
import android.content.Context
4-
import androidx.compose.foundation.text.input.TextFieldState
54
import androidx.compose.runtime.mutableStateOf
65
import androidx.compose.runtime.snapshotFlow
76
import androidx.lifecycle.ViewModel
@@ -42,6 +41,7 @@ import kotlinx.coroutines.withContext
4241
import uniffi.gemstone.GemAddAssetPhase
4342
import uniffi.gemstone.GemAddAssetServiceInterface
4443
import uniffi.gemstone.GemAddAssetSession
44+
import uniffi.gemstone.GemChainList
4545
import uniffi.gemstone.GemChainServiceInterface
4646
import uniffi.gemstone.GemGroup
4747
import javax.inject.Inject
@@ -62,26 +62,20 @@ class AddAssetViewModel @Inject constructor(
6262
val uiState = state.map { it.toUIState() }
6363
.stateIn(viewModelScope, SharingStarted.Eagerly, AddAssetUIState())
6464

65-
val chainFilter = TextFieldState()
66-
6765
private val wallet = getSession().map { it?.wallet }.filterNotNull()
6866
.stateIn(viewModelScope, SharingStarted.Eagerly, null)
6967

7068
private val chainPicker = wallet.map { wallet -> wallet?.let { service.chainPicker(it.toGem()) } }
7169
.flowOn(ioDispatcher)
7270
.stateIn(viewModelScope, SharingStarted.Eagerly, null)
7371

74-
val availableChains = chainPicker.map { picker -> picker?.chains?.map { chain -> chain.requireChain() } }
72+
private val availableChains = chainPicker.map { picker -> picker?.chains?.map { chain -> chain.requireChain() } }
7573
.stateIn(viewModelScope, SharingStarted.Eagerly, null)
7674

7775
val showsChainPicker = chainPicker.map { it?.showsPicker == true }
7876
.stateIn(viewModelScope, SharingStarted.Eagerly, false)
7977

80-
val chains = snapshotFlow { chainFilter.text }.combine(availableChains) { query, availableChains ->
81-
availableChains?.let { chainService.chainList(it.map { chain -> chain.string }, query.toString()) }
82-
}
83-
.flowOn(ioDispatcher)
84-
.stateIn(viewModelScope, SharingStarted.Eagerly, null)
78+
fun chains(query: String): GemChainList? = availableChains.value?.let { chainService.chainList(it.map { chain -> chain.string }, query) }
8579

8680
private val chain = MutableStateFlow<Chain?>(null)
8781
val selectedChain = chainPicker.combine(chain) { picker, chain ->

‎android/features/price_alerts/presents/build.gradle.kts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ android {
1414
dependencies {
1515
implementation(project(":ui"))
1616
implementation(project(":features:price_alerts:viewmodels"))
17-
implementation(project(":features:assets:presents"))
1817

1918
implementation(libs.hilt.lifecycle.viewmodel.compose)
2019

‎android/features/price_alerts/presents/src/main/kotlin/com/gemwallet/android/features/price_alerts/presents/PriceAlertsAction.kt‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package com.gemwallet.android.features.price_alerts.presents
22

33
import com.wallet.core.primitives.AssetId
44

5-
internal sealed interface PriceAlertsAction {
5+
sealed interface PriceAlertsAction {
6+
sealed interface Navigation : PriceAlertsAction
7+
68
data object Refresh : PriceAlertsAction
7-
data object Close : PriceAlertsAction
8-
data object Add : PriceAlertsAction
99
data class Exclude(val id: String) : PriceAlertsAction
10-
data class OpenChart(val assetId: AssetId) : PriceAlertsAction
10+
11+
data object Close : Navigation
12+
data object Add : Navigation
13+
data class OpenChart(val assetId: AssetId) : Navigation
1114
}

0 commit comments

Comments
 (0)