Skip to content

Commit 97a03cc

Browse files
committed
feat(exchange): utilize Balance Rate as app-wide rate; make changes in entry flows empheremal
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent f5c27c0 commit 97a03cc

9 files changed

Lines changed: 51 additions & 32 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ internal class CashScreenViewModel @Inject constructor(
335335
}.launchIn(viewModelScope)
336336
}
337337

338+
override fun onCleared() {
339+
exchange.resetEntryToBalance()
340+
}
341+
338342
internal companion object {
339343
val updateStateForEvent: (Event) -> ((State) -> State) = { event ->
340344
when (event) {

apps/flipcash/features/onramp/src/main/kotlin/com/flipcash/app/onramp/internal/OnRampViewModel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal data class AmountEntryState(
8484
@HiltViewModel
8585
internal class OnRampViewModel @Inject constructor(
8686
userManager: UserManager,
87-
exchange: Exchange,
87+
private val exchange: Exchange,
8888
transactionController: TransactionOperations,
8989
private val resources: ResourceHelper,
9090
onRampController: OnRampController,
@@ -513,6 +513,10 @@ internal class OnRampViewModel @Inject constructor(
513513
}
514514
}
515515

516+
override fun onCleared() {
517+
exchange.resetEntryToBalance()
518+
}
519+
516520
internal companion object {
517521
val updateStateForEvent: (Event) -> ((State) -> State) = { event ->
518522
when (event) {

apps/flipcash/features/withdrawal/src/main/kotlin/com/flipcash/app/withdrawal/WithdrawalViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,10 @@ internal class WithdrawalViewModel @Inject constructor(
443443
.launchIn(viewModelScope)
444444
}
445445

446+
override fun onCleared() {
447+
exchange.resetEntryToBalance()
448+
}
449+
446450
internal companion object {
447451
val updateStateForEvent: (Event) -> ((State) -> State) = { event ->
448452
when (event) {

apps/flipcash/shared/currency-selection/core/src/main/kotlin/com/flipcash/app/currency/PreferredCurrencyController.kt

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,15 @@ class PreferredCurrencyController @Inject constructor(
8080
prefs[initKey] = true
8181
}
8282

83-
val entryCurrency = prefs[kindKeyForUser(
84-
kind = RegionSelectionKind.Entry,
85-
userId
86-
)]?.let { CurrencyCode.tryValueOf(it) }
87-
?: CurrencyCode.tryValueOf(locale.getDefaultCurrencyName())
88-
?: CurrencyCode.USD
89-
9083
val balanceCurrency = prefs[kindKeyForUser(
9184
kind = RegionSelectionKind.Balance,
9285
userId
9386
)]?.let { CurrencyCode.tryValueOf(it) }
9487
?: CurrencyCode.tryValueOf(locale.getDefaultCurrencyName())
9588
?: CurrencyCode.USD
9689

97-
exchange.setPreferredEntryCurrency(entryCurrency)
90+
// entry defaults to balance — no longer read from DataStore
91+
exchange.setPreferredEntryCurrency(balanceCurrency)
9892
exchange.setPreferredBalanceCurrency(balanceCurrency)
9993
}
10094
}.launchIn(dataScope)
@@ -104,12 +98,17 @@ class PreferredCurrencyController @Inject constructor(
10498
fun observePreferredForKind(
10599
kind: RegionSelectionKind
106100
): Flow<String> {
107-
val identifier = userManager.accountId?.base58 ?: return emptyFlow()
108-
109-
return storage.data
110-
.map { prefs ->
111-
prefs[kindKeyForUser(kind, identifier)] ?: locale.getDefaultCurrencyName()
101+
return when (kind) {
102+
RegionSelectionKind.Entry -> {
103+
exchange.observeEntryRate().map { it.currency.name }
104+
}
105+
RegionSelectionKind.Balance -> {
106+
val identifier = userManager.accountId?.base58 ?: return emptyFlow()
107+
storage.data.map { prefs ->
108+
prefs[kindKeyForUser(kind, identifier)] ?: locale.getDefaultCurrencyName()
109+
}
112110
}
111+
}
113112
}
114113

115114
fun observeRecentCurrencies(): Flow<Set<String>> {
@@ -122,28 +121,24 @@ class PreferredCurrencyController @Inject constructor(
122121
kind: RegionSelectionKind,
123122
currency: Currency
124123
) {
125-
val identifier = userManager.accountId?.base58 ?: return
126-
storage.edit { prefs ->
127-
prefs[kindKeyForUser(kind, identifier)] = currency.code
128-
129-
val recentKey = recentsKeyForUser(identifier)
130-
val recents = prefs[recentKey].orEmpty()
131-
132-
prefs[recentKey] = updateRecents(recents, currency.code)
133-
}
124+
val code = CurrencyCode.tryValueOf(currency.code) ?: return
134125
when (kind) {
135126
RegionSelectionKind.Entry -> {
136-
val code = CurrencyCode.tryValueOf(currency.code)
137-
if (code != null) {
138-
exchange.setPreferredEntryCurrency(code)
139-
}
127+
// temporary — only update Exchange, don't persist
128+
exchange.setPreferredEntryCurrency(code)
140129
}
141130
RegionSelectionKind.Balance -> {
142-
val code = CurrencyCode.tryValueOf(currency.code)
143-
if (code != null) {
144-
exchange.setPreferredBalanceCurrency(code)
145-
settingsController.update()
131+
val identifier = userManager.accountId?.base58 ?: return
132+
storage.edit { prefs ->
133+
prefs[kindKeyForUser(kind, identifier)] = currency.code
134+
135+
val recentKey = recentsKeyForUser(identifier)
136+
val recents = prefs[recentKey].orEmpty()
137+
138+
prefs[recentKey] = updateRecents(recents, currency.code)
146139
}
140+
exchange.setPreferredBalanceCurrency(code)
141+
settingsController.update()
147142
}
148143
}
149144
}

apps/flipcash/shared/tokens/src/main/kotlin/com/flipcash/app/tokens/ui/SwapViewModel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ data class AmountEntryState(
6666
@HiltViewModel
6767
class SwapViewModel @Inject constructor(
6868
userManager: UserManager,
69-
exchange: Exchange,
69+
private val exchange: Exchange,
7070
transactionController: TransactionOperations,
7171
resources: ResourceHelper,
7272
tokenCoordinator: TokenCoordinator,
@@ -667,6 +667,10 @@ class SwapViewModel @Inject constructor(
667667
}
668668
}
669669

670+
override fun onCleared() {
671+
exchange.resetEntryToBalance()
672+
}
673+
670674
internal companion object {
671675
val updateStateForEvent: (Event) -> ((State) -> State) = { event ->
672676
when (event) {

services/opencode-compose/src/main/kotlin/com/getcode/opencode/compose/Exchange.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ private class ExchangeNull : Exchange {
2828

2929
}
3030

31+
override fun resetEntryToBalance() = Unit
32+
3133
override fun observeBalanceRate(): Flow<Rate> {
3234
return emptyFlow()
3335
}

services/opencode-compose/src/main/kotlin/com/getcode/opencode/compose/ExchangeStub.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ExchangeStub(
2424
override fun observeEntryRate(): Flow<Rate> = emptyFlow()
2525

2626
override suspend fun setPreferredEntryCurrency(currencyCode: CurrencyCode) = Unit
27+
override fun resetEntryToBalance() = Unit
2728

2829
override val balanceRate: Rate = Rate.oneToOne
2930

services/opencode/src/main/kotlin/com/getcode/opencode/exchange/Exchange.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface Exchange {
1111
val entryRate: Rate
1212
fun observeEntryRate(): Flow<Rate>
1313
suspend fun setPreferredEntryCurrency(currencyCode: CurrencyCode)
14+
fun resetEntryToBalance()
1415
val balanceRate: Rate
1516
fun observeBalanceRate(): Flow<Rate>
1617
suspend fun setPreferredBalanceCurrency(currencyCode: CurrencyCode)

services/opencode/src/main/kotlin/com/getcode/opencode/internal/exchange/OpenCodeExchange.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ internal class OpenCodeExchange @Inject constructor(
9696
}
9797
}
9898

99+
override fun resetEntryToBalance() {
100+
scope.launch { setPreferredEntryCurrency(balanceRate.currency) }
101+
}
102+
99103
private var balanceCurrency: CurrencyCode? = null
100104
private var entryCurrency: CurrencyCode? = null
101105

0 commit comments

Comments
 (0)