Skip to content

Commit 84abe6b

Browse files
authored
feat: unify Exchange rate API around preferredRate (#752)
Replace entryRate with preferredRate across the Exchange interface and all callers. Simplify OpenCodeExchange by removing redundant rate properties and consolidating currency selection logic in PreferredCurrencyController. Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent fd5d0fe commit 84abe6b

27 files changed

Lines changed: 81 additions & 234 deletions

File tree

apps/flipcash/app/src/main/kotlin/com/flipcash/app/internal/ui/navigation/AppScreenContent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fun appEntryProvider(
9090
}
9191
annotatedEntry<AppRoute.Main.AppRestricted> { key -> AppRestrictedScreen(key.restrictionType) }
9292
annotatedEntry<AppRoute.Main.Scanner> { ScannerScreen() }
93-
annotatedEntry<AppRoute.Main.RegionSelection> { key -> RegionSelectionScreen(key.kind) }
93+
annotatedEntry<AppRoute.Main.RegionSelection> { RegionSelectionScreen() }
9494

9595
// Sheets (inner content — wrapped in Main.Sheet by navigateTo())
9696
annotatedEntry<AppRoute.Sheets.Give> { key -> CashScreen(key.mint, key.fromTokenInfo) }

apps/flipcash/core/src/main/kotlin/com/flipcash/app/core/AppRoute.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.os.Parcelable
44
import androidx.navigation3.runtime.NavKey
55
import com.flipcash.app.core.deposit.DepositResult
66
import com.flipcash.app.core.deposit.DepositStep
7-
import com.flipcash.app.core.money.RegionSelectionKind
87
import com.flipcash.app.core.tokens.CurrencyCreatorResult
98
import com.flipcash.app.core.tokens.CurrencyCreatorStep
109
import com.flipcash.app.core.tokens.SwapPurpose
@@ -71,7 +70,7 @@ sealed interface AppRoute : NavKey, Parcelable {
7170

7271
// TODO: is there a better place for this to live?
7372
@Serializable
74-
data class RegionSelection(val kind: RegionSelectionKind) : Main
73+
data object RegionSelection : Main
7574

7675
@Serializable
7776
@Parcelize

apps/flipcash/core/src/main/kotlin/com/flipcash/app/core/money/RegionSelectionKind.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

apps/flipcash/features/balance/src/main/kotlin/com/flipcash/app/balance/BalanceScreen.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
1111
import com.flipcash.app.balance.internal.BalanceScreen
1212
import com.flipcash.app.balance.internal.BalanceViewModel
1313
import com.flipcash.app.core.AppRoute
14-
import com.flipcash.app.core.money.RegionSelectionKind
1514
import com.flipcash.app.core.tokens.TokenPurpose
1615
import com.flipcash.app.tokens.ui.SelectTokenViewModel
1716
import com.flipcash.core.R
@@ -55,11 +54,7 @@ fun BalanceScreen() {
5554
viewModel.eventFlow
5655
.filterIsInstance<BalanceViewModel.Event.OpenCurrencySelection>()
5756
.onEach {
58-
navigator.push(
59-
AppRoute.Main.RegionSelection(
60-
RegionSelectionKind.Balance
61-
)
62-
)
57+
navigator.push(AppRoute.Main.RegionSelection)
6358
}.launchIn(this)
6459
}
6560

apps/flipcash/features/cash/src/main/kotlin/com/flipcash/app/cash/internal/CashScreenContent.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import androidx.compose.runtime.getValue
1212
import androidx.compose.ui.Modifier
1313
import androidx.compose.ui.res.stringResource
1414
import com.flipcash.app.core.AppRoute
15-
import com.flipcash.app.core.money.RegionSelectionKind
1615
import com.flipcash.app.core.ui.AmountWithKeypad
1716
import com.flipcash.features.cash.R
1817
import com.getcode.navigation.core.LocalCodeNavigator
@@ -46,11 +45,7 @@ internal fun GiveScreenContent(viewModel: CashScreenViewModel) {
4645
},
4746
isClickable = true,
4847
onAmountClicked = {
49-
navigator.push(
50-
AppRoute.Main.RegionSelection(
51-
kind = RegionSelectionKind.Entry
52-
)
53-
)
48+
navigator.push(AppRoute.Main.RegionSelection)
5449
},
5550
isError = state.isError,
5651
onNumberPressed = { viewModel.dispatchEvent(CashScreenViewModel.Event.OnNumberPressed(it)) },

apps/flipcash/features/cash/src/main/kotlin/com/flipcash/app/cash/internal/CashScreenViewModel.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ internal class CashScreenViewModel @Inject constructor(
142142
style = BottomBarManager.BottomBarButtonStyle.Filled,
143143
) {
144144
viewModelScope.launch {
145-
val rate = exchange.entryRate
145+
val rate = exchange.preferredRate
146146
val (token, balance) = stateFlow.value.token!!
147147
val amountFiat = verifiedFiatCalculator.compute(
148148
amount = Fiat(amount, rate.currency),
@@ -212,7 +212,7 @@ internal class CashScreenViewModel @Inject constructor(
212212
combine(
213213
tokenCoordinator.tokens,
214214
tokenCoordinator.balanceForToken(tokenAddress),
215-
exchange.observeEntryRate(),
215+
exchange.observePreferredRate(),
216216
) { tokens, balance, rate ->
217217
val token = tokens.find { it.address == tokenAddress } ?: return@combine null
218218
TokenWithLocalizedBalance(
@@ -232,7 +232,7 @@ internal class CashScreenViewModel @Inject constructor(
232232
dispatchEvent(Event.OnCurrencyChanged(it))
233233
}.launchIn(viewModelScope)
234234

235-
exchange.observeEntryRate()
235+
exchange.observePreferredRate()
236236
.onEach {
237237
// reset when entry rate changes
238238
numberInputHelper.reset()
@@ -309,7 +309,7 @@ internal class CashScreenViewModel @Inject constructor(
309309
.onEach { data ->
310310
dispatchEvent(Event.UpdateLoadingState(loading = true))
311311
val (token, balance) = stateFlow.value.token!!
312-
val rate = exchange.entryRate
312+
val rate = exchange.preferredRate
313313

314314
val result = verifiedFiatCalculator.compute(
315315
amount = Fiat(data.amountData.amount, rate.currency),
@@ -352,9 +352,6 @@ internal class CashScreenViewModel @Inject constructor(
352352
}.launchIn(viewModelScope)
353353
}
354354

355-
override fun onCleared() {
356-
exchange.resetEntryToBalance()
357-
}
358355

359356
internal companion object {
360357
val updateStateForEvent: (Event) -> ((State) -> State) = { event ->

apps/flipcash/features/cash/src/test/kotlin/com/flipcash/app/cash/internal/CashScreenViewModelTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class CashScreenViewModelTest {
7676
every { resources.getString(R.string.error_description_sendLimitReached) } returns "error_description_sendLimitReached"
7777

7878
// Default stubs for flows consumed in init
79-
every { exchange.observeEntryRate() } returns emptyFlow()
80-
every { exchange.entryRate } returns Rate.oneToOne
79+
every { exchange.observePreferredRate() } returns emptyFlow()
80+
every { exchange.preferredRate } returns Rate.oneToOne
8181
every { tokenCoordinator.observeSelectedTokenMint() } returns emptyFlow()
8282
every { tokenCoordinator.tokens } returns emptyFlow()
8383
every { transactionController.limits } returns MutableStateFlow(null)

apps/flipcash/features/deposit/src/main/kotlin/com/flipcash/app/deposit/DepositFlowScreen.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ private fun DepositSelectTokenScreen() {
118118
modifier = Modifier.fillMaxSize(),
119119
tokens = state.tokens,
120120
selectedToken = state.selectedToken,
121-
showFlags = true,
122121
onTokenSelected = { viewModel.dispatchEvent(SelectTokenViewModel.Event.OnTokenSelected(it.address)) },
123122
)
124123
}

apps/flipcash/features/tokens/src/main/kotlin/com/flipcash/app/tokens/internal/SwapEntryScreenContent.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import androidx.compose.ui.res.stringResource
1515
import androidx.compose.ui.text.AnnotatedString
1616
import androidx.lifecycle.compose.collectAsStateWithLifecycle
1717
import com.flipcash.app.core.AppRoute
18-
import com.flipcash.app.core.money.RegionSelectionKind
1918
import com.flipcash.app.core.onramp.ui.buildPhantomButtonLabel
2019
import com.flipcash.app.core.tokens.FundingSource
2120
import com.flipcash.app.core.tokens.SwapPurpose
@@ -87,11 +86,7 @@ internal fun SwapEntryScreenContent(
8786
decimalPlaces = entryState.currencyModel.fractionUnits,
8887
isClickable = (state.purpose as? SwapPurpose.Buy)?.fundingSource != FundingSource.Phantom,
8988
onAmountClicked = {
90-
navigator.push(
91-
AppRoute.Main.RegionSelection(
92-
kind = RegionSelectionKind.Entry
93-
)
94-
)
89+
navigator.push(AppRoute.Main.RegionSelection)
9590
},
9691
isError = state.isError,
9792
onNumberPressed = {

apps/flipcash/features/tokens/src/main/kotlin/com/flipcash/app/tokens/internal/TokenInfoScreen.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import com.flipcash.app.analytics.FlipcashAnalyticsService
3535
import com.flipcash.app.analytics.rememberAnalytics
3636
import com.flipcash.app.core.AppRoute
3737
import com.flipcash.app.core.data.Loadable
38-
import com.flipcash.app.core.money.RegionSelectionKind
3938
import com.flipcash.app.core.tokens.SwapPurpose
4039
import com.flipcash.app.tokens.internal.components.info.MarketCapSection
4140
import com.flipcash.app.tokens.internal.components.info.TokenBalance
@@ -145,7 +144,7 @@ private fun TokenInfoScreen(
145144
onClick = {
146145
dispatch(
147146
TokenInfoViewModel.Event.OpenScreen(
148-
AppRoute.Main.RegionSelection(kind = RegionSelectionKind.Balance)
147+
AppRoute.Main.RegionSelection
149148
)
150149
)
151150
}

0 commit comments

Comments
 (0)