Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only auto-refresh the currencies needed by the app #352

Merged
merged 1 commit into from
Apr 11, 2023

Conversation

robbiehanson
Copy link
Contributor

The CurrencyManager is currently fetching data from 5 different sources. This is largely unnecessary work for most users. For example, if you're only using the Euro or the US Dollar, then you only really need to fetch from 1 source.

This PR optimizes the auto-refresh task to only fetch the currencies needed by the app. For example, if the user is only using COP (Colombian Pesos), then the currency manager will only fetch:

  • BTC / USD (from blockchain.info)
  • USD / COP (from coinbase)

and we'll see something like this in the logs:

[CurrencyManager] API(blockchain.info): Next refresh: 0s
[CurrencyManager] Fetching 1 exchange rate(s) from blockchain.info
[CurrencyManager] API(coinbase): Next refresh: 0s
[CurrencyManager] Fetching 1 exchange rate(s) from coinbase
[CurrencyManager] API(coindesk): Nothing to refresh
[CurrencyManager] API(bluelytics): Nothing to refresh
[CurrencyManager] API(yadio): Nothing to refresh

The key to this optimization is the existing AppConfigurationManager.preferredFiatCurrencies:

data class PreferredFiatCurrencies(
   val primary: FiatCurrency,
   val others: Set<FiatCurrency>
) { /* ... */ }

val preferredFiatCurrencies: StateFlow<PreferredFiatCurrencies?>

(Where primary is the user's configured fiat currency, and others includes those in the Currency Converter)

So as long as the UI code properly updates AppConfigurationManager.preferredFiatCurrencies, then the CurrencyManager can optimize the auto-refresh logic, and perform much less work.

…calculate when preferredFiatCurrencies changes.
@robbiehanson robbiehanson requested a review from dpad85 March 30, 2023 16:55
@robbiehanson robbiehanson merged commit 4fb5534 into master Apr 11, 2023
@robbiehanson robbiehanson deleted the optimize-auto-refresh branch April 11, 2023 20:43
robbiehanson added a commit that referenced this pull request Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants