Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fun appEntryProvider(
}
annotatedEntry<AppRoute.Main.AppRestricted> { key -> AppRestrictedScreen(key.restrictionType) }
annotatedEntry<AppRoute.Main.Scanner> { ScannerScreen() }
annotatedEntry<AppRoute.Main.RegionSelection> { key -> RegionSelectionScreen(key.kind) }
annotatedEntry<AppRoute.Main.RegionSelection> { RegionSelectionScreen() }

// Sheets (inner content — wrapped in Main.Sheet by navigateTo())
annotatedEntry<AppRoute.Sheets.Give> { key -> CashScreen(key.mint, key.fromTokenInfo) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.os.Parcelable
import androidx.navigation3.runtime.NavKey
import com.flipcash.app.core.deposit.DepositResult
import com.flipcash.app.core.deposit.DepositStep
import com.flipcash.app.core.money.RegionSelectionKind
import com.flipcash.app.core.tokens.CurrencyCreatorResult
import com.flipcash.app.core.tokens.CurrencyCreatorStep
import com.flipcash.app.core.tokens.SwapPurpose
Expand Down Expand Up @@ -71,7 +70,7 @@ sealed interface AppRoute : NavKey, Parcelable {

// TODO: is there a better place for this to live?
@Serializable
data class RegionSelection(val kind: RegionSelectionKind) : Main
data object RegionSelection : Main

@Serializable
@Parcelize
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import com.flipcash.app.balance.internal.BalanceScreen
import com.flipcash.app.balance.internal.BalanceViewModel
import com.flipcash.app.core.AppRoute
import com.flipcash.app.core.money.RegionSelectionKind
import com.flipcash.app.core.tokens.TokenPurpose
import com.flipcash.app.tokens.ui.SelectTokenViewModel
import com.flipcash.core.R
Expand Down Expand Up @@ -55,11 +54,7 @@ fun BalanceScreen() {
viewModel.eventFlow
.filterIsInstance<BalanceViewModel.Event.OpenCurrencySelection>()
.onEach {
navigator.push(
AppRoute.Main.RegionSelection(
RegionSelectionKind.Balance
)
)
navigator.push(AppRoute.Main.RegionSelection)
}.launchIn(this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.flipcash.app.core.AppRoute
import com.flipcash.app.core.money.RegionSelectionKind
import com.flipcash.app.core.ui.AmountWithKeypad
import com.flipcash.features.cash.R
import com.getcode.navigation.core.LocalCodeNavigator
Expand Down Expand Up @@ -46,11 +45,7 @@ internal fun GiveScreenContent(viewModel: CashScreenViewModel) {
},
isClickable = true,
onAmountClicked = {
navigator.push(
AppRoute.Main.RegionSelection(
kind = RegionSelectionKind.Entry
)
)
navigator.push(AppRoute.Main.RegionSelection)
},
isError = state.isError,
onNumberPressed = { viewModel.dispatchEvent(CashScreenViewModel.Event.OnNumberPressed(it)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ internal class CashScreenViewModel @Inject constructor(
style = BottomBarManager.BottomBarButtonStyle.Filled,
) {
viewModelScope.launch {
val rate = exchange.entryRate
val rate = exchange.preferredRate
val (token, balance) = stateFlow.value.token!!
val amountFiat = verifiedFiatCalculator.compute(
amount = Fiat(amount, rate.currency),
Expand Down Expand Up @@ -212,7 +212,7 @@ internal class CashScreenViewModel @Inject constructor(
combine(
tokenCoordinator.tokens,
tokenCoordinator.balanceForToken(tokenAddress),
exchange.observeEntryRate(),
exchange.observePreferredRate(),
) { tokens, balance, rate ->
val token = tokens.find { it.address == tokenAddress } ?: return@combine null
TokenWithLocalizedBalance(
Expand All @@ -232,7 +232,7 @@ internal class CashScreenViewModel @Inject constructor(
dispatchEvent(Event.OnCurrencyChanged(it))
}.launchIn(viewModelScope)

exchange.observeEntryRate()
exchange.observePreferredRate()
.onEach {
// reset when entry rate changes
numberInputHelper.reset()
Expand Down Expand Up @@ -309,7 +309,7 @@ internal class CashScreenViewModel @Inject constructor(
.onEach { data ->
dispatchEvent(Event.UpdateLoadingState(loading = true))
val (token, balance) = stateFlow.value.token!!
val rate = exchange.entryRate
val rate = exchange.preferredRate

val result = verifiedFiatCalculator.compute(
amount = Fiat(data.amountData.amount, rate.currency),
Expand Down Expand Up @@ -352,9 +352,6 @@ internal class CashScreenViewModel @Inject constructor(
}.launchIn(viewModelScope)
}

override fun onCleared() {
exchange.resetEntryToBalance()
}

internal companion object {
val updateStateForEvent: (Event) -> ((State) -> State) = { event ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class CashScreenViewModelTest {
every { resources.getString(R.string.error_description_sendLimitReached) } returns "error_description_sendLimitReached"

// Default stubs for flows consumed in init
every { exchange.observeEntryRate() } returns emptyFlow()
every { exchange.entryRate } returns Rate.oneToOne
every { exchange.observePreferredRate() } returns emptyFlow()
every { exchange.preferredRate } returns Rate.oneToOne
every { tokenCoordinator.observeSelectedTokenMint() } returns emptyFlow()
every { tokenCoordinator.tokens } returns emptyFlow()
every { transactionController.limits } returns MutableStateFlow(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ private fun DepositSelectTokenScreen() {
modifier = Modifier.fillMaxSize(),
tokens = state.tokens,
selectedToken = state.selectedToken,
showFlags = true,
onTokenSelected = { viewModel.dispatchEvent(SelectTokenViewModel.Event.OnTokenSelected(it.address)) },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.flipcash.app.core.AppRoute
import com.flipcash.app.core.money.RegionSelectionKind
import com.flipcash.app.core.onramp.ui.buildPhantomButtonLabel
import com.flipcash.app.core.tokens.FundingSource
import com.flipcash.app.core.tokens.SwapPurpose
Expand Down Expand Up @@ -87,11 +86,7 @@ internal fun SwapEntryScreenContent(
decimalPlaces = entryState.currencyModel.fractionUnits,
isClickable = (state.purpose as? SwapPurpose.Buy)?.fundingSource != FundingSource.Phantom,
onAmountClicked = {
navigator.push(
AppRoute.Main.RegionSelection(
kind = RegionSelectionKind.Entry
)
)
navigator.push(AppRoute.Main.RegionSelection)
},
isError = state.isError,
onNumberPressed = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import com.flipcash.app.analytics.FlipcashAnalyticsService
import com.flipcash.app.analytics.rememberAnalytics
import com.flipcash.app.core.AppRoute
import com.flipcash.app.core.data.Loadable
import com.flipcash.app.core.money.RegionSelectionKind
import com.flipcash.app.core.tokens.SwapPurpose
import com.flipcash.app.tokens.internal.components.info.MarketCapSection
import com.flipcash.app.tokens.internal.components.info.TokenBalance
Expand Down Expand Up @@ -145,7 +144,7 @@ private fun TokenInfoScreen(
onClick = {
dispatch(
TokenInfoViewModel.Event.OpenScreen(
AppRoute.Main.RegionSelection(kind = RegionSelectionKind.Balance)
AppRoute.Main.RegionSelection
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ private fun WithdrawalSelectTokenScreen() {
modifier = Modifier.fillMaxSize(),
tokens = state.tokens,
selectedToken = state.selectedToken,
showFlags = true,
onTokenSelected = { viewModel.dispatchEvent(SelectTokenViewModel.Event.OnTokenSelected(it.address)) },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@ internal class WithdrawalViewModel @Inject constructor(

init {
numberInputHelper.reset()
dispatchEvent(Event.OnEntryRateUpdated(exchange.entryRate))
dispatchEvent(Event.OnEntryRateUpdated(exchange.preferredRate))

stateFlow
.mapNotNull { it.selectedTokenAddress }
.flatMapLatest { tokenAddress ->
combine(
tokenCoordinator.tokens,
tokenCoordinator.balanceForToken(tokenAddress),
exchange.observeEntryRate(),
exchange.observePreferredRate(),
) { tokens, balance, rate ->
val token = tokens.find { it.address == tokenAddress } ?: return@combine null
TokenWithBalance(
Expand Down Expand Up @@ -271,7 +271,7 @@ internal class WithdrawalViewModel @Inject constructor(
numberInputHelper.fractionUnits = it.fractionUnits
}.launchIn(viewModelScope)

exchange.observeEntryRate()
exchange.observePreferredRate()
.onEach {
// reset when entry rate changes
numberInputHelper.reset()
Expand Down Expand Up @@ -329,7 +329,7 @@ internal class WithdrawalViewModel @Inject constructor(
.filterNot { checkMinimumExceeded() }
.onEach { data ->
dispatchEvent(Event.UpdateConfirmingAmountState(loading = true))
val rate = exchange.entryRate
val rate = exchange.preferredRate
val token = stateFlow.value.token!!.token
val amountVerified = verifiedFiatCalculator.compute(
amount = Fiat(data.amountData.amount, rate.currency),
Expand Down Expand Up @@ -647,9 +647,6 @@ internal class WithdrawalViewModel @Inject constructor(
.launchIn(viewModelScope)
}

override fun onCleared() {
exchange.resetEntryToBalance()
}

internal companion object {
val updateStateForEvent: (Event) -> ((State) -> State) = { event ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal fun TransactionReceipt(
}
},
formattedBalance = { amount ->
amount.convertingToUsdIfNeeded(exchange.entryRate)
amount.convertingToUsdIfNeeded(exchange.preferredRate)
.estimatedTokenAmountIn(tokenWithBalance.token, fractionDigits = 2)
},
styling = rememberTokenBalanceRowStyling(
Expand Down Expand Up @@ -157,7 +157,7 @@ private fun LineItems(
label = AnnotatedString(
stringResource(R.string.label_amountInToken, tokenWithBalance.displayName)
),
amount = transferAmount.convertingToUsdIfNeeded(exchange.entryRate)
amount = transferAmount.convertingToUsdIfNeeded(exchange.preferredRate)
.estimatedTokenAmountIn(tokenWithBalance.token, fractionDigits = 2),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.flipcash.app.core.AppRoute
import com.flipcash.app.core.money.RegionSelectionKind
import com.flipcash.app.core.withdrawal.WithdrawalResult
import com.flipcash.app.core.withdrawal.WithdrawalStep
import com.flipcash.app.withdrawal.WithdrawalViewModel
Expand Down Expand Up @@ -45,9 +44,7 @@ internal fun WithdrawalEntryScreen(
viewModel = viewModel,
mint = selectedMint,
onOpenRegionSelection = {
codeNavigator.push(
AppRoute.Main.RegionSelection(kind = RegionSelectionKind.Entry)
)
codeNavigator.push(AppRoute.Main.RegionSelection)
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringSetPreferencesKey
import androidx.datastore.preferences.preferencesDataStoreFile
import com.flipcash.app.core.money.RegionSelectionKind
import com.flipcash.services.controllers.SettingsController
import com.flipcash.services.user.UserManager
import com.getcode.opencode.exchange.Exchange
Expand Down Expand Up @@ -43,10 +42,8 @@ class PreferredCurrencyController @Inject constructor(
fun initializedKeyForUser(userIdentifier: String) =
booleanPreferencesKey("init-$userIdentifier")

fun kindKeyForUser(
kind: RegionSelectionKind,
userIdentifier: String
) = stringPreferencesKey("${kind.name.lowercase()}-$userIdentifier")
fun currencyKeyForUser(userIdentifier: String) =
stringPreferencesKey("balance-$userIdentifier")

fun recentsKeyForUser(userIdentifier: String) =
stringSetPreferencesKey("recents-$userIdentifier")
Expand Down Expand Up @@ -80,34 +77,21 @@ class PreferredCurrencyController @Inject constructor(
prefs[initKey] = true
}

val balanceCurrency = prefs[kindKeyForUser(
kind = RegionSelectionKind.Balance,
userId
)]?.let { CurrencyCode.tryValueOf(it) }
val currencyCode = prefs[currencyKeyForUser(userId)]
?.let { CurrencyCode.tryValueOf(it) }
?: CurrencyCode.tryValueOf(locale.getDefaultCurrencyName())
?: CurrencyCode.USD

// entry defaults to balance — no longer read from DataStore
exchange.setPreferredEntryCurrency(balanceCurrency)
exchange.setPreferredBalanceCurrency(balanceCurrency)
exchange.setPreferredCurrency(currencyCode)
}
}.launchIn(dataScope)

}

fun observePreferredForKind(
kind: RegionSelectionKind
): Flow<String> {
return when (kind) {
RegionSelectionKind.Entry -> {
exchange.observeEntryRate().map { it.currency.name }
}
RegionSelectionKind.Balance -> {
val identifier = userManager.accountId?.base58 ?: return emptyFlow()
storage.data.map { prefs ->
prefs[kindKeyForUser(kind, identifier)] ?: locale.getDefaultCurrencyName()
}
}
fun observePreferredCurrency(): Flow<String> {
val identifier = userManager.accountId?.base58 ?: return emptyFlow()
return storage.data.map { prefs ->
prefs[currencyKeyForUser(identifier)] ?: locale.getDefaultCurrencyName()
}
}

Expand All @@ -117,31 +101,19 @@ class PreferredCurrencyController @Inject constructor(
return storage.data.map { prefs -> prefs[recentsKeyForUser(identifier)].orEmpty() }
}

suspend fun updateSelection(
kind: RegionSelectionKind,
currency: Currency
) {
suspend fun updateSelection(currency: Currency) {
val code = CurrencyCode.tryValueOf(currency.code) ?: return
when (kind) {
RegionSelectionKind.Entry -> {
// temporary — only update Exchange, don't persist
exchange.setPreferredEntryCurrency(code)
}
RegionSelectionKind.Balance -> {
val identifier = userManager.accountId?.base58 ?: return
storage.edit { prefs ->
prefs[kindKeyForUser(kind, identifier)] = currency.code
val identifier = userManager.accountId?.base58 ?: return
storage.edit { prefs ->
prefs[currencyKeyForUser(identifier)] = currency.code

val recentKey = recentsKeyForUser(identifier)
val recents = prefs[recentKey].orEmpty()
val recentKey = recentsKeyForUser(identifier)
val recents = prefs[recentKey].orEmpty()

prefs[recentKey] = updateRecents(recents, currency.code)
}
exchange.setPreferredBalanceCurrency(code)
exchange.setPreferredEntryCurrency(code)
settingsController.update()
}
prefs[recentKey] = updateRecents(recents, currency.code)
}
exchange.setPreferredCurrency(code)
settingsController.update()
}

suspend fun removeFromRecents(
Expand All @@ -168,4 +140,4 @@ class PreferredCurrencyController @Inject constructor(

return updated
}
}
}
Loading
Loading