Overview
The Currency Converter is a specialized component of the Unit Converter system that handles conversion between different currencies. It uses theICurrencyConverterDataLoader interface to fetch live exchange rates from the web in production builds, but uses mock data in developer builds as documented in the project README.
Currency Data Loading
TheICurrencyConverterDataLoader interface extends the standard IConverterDataLoader with currency-specific functionality.
Header: ~/workspace/source/src/CalcManager/UnitConverter.h:188
Interface Methods
SetViewModelCallback
Registers a callback for currency-specific UI updates.Callback interface for receiving currency symbols, ratios, timestamps, and network behavior updates
GetCurrencySymbols
Retrieves the currency symbols for two units.First currency unit
Second currency unit
GetCurrencyRatioEquality
Retrieves formatted strings showing the conversion ratio between two currencies.Source currency unit
Target currency unit
("1 USD = 0.85 EUR", "1 US Dollar equals 0.85 Euros")
GetCurrencyTimestamp
Retrieves the timestamp of when the currency data was last updated.TryLoadDataFromCacheAsync
Attempts to load currency data from local cache asynchronously.true if cache data was loaded successfully, false otherwise
TryLoadDataFromWebAsync
Attempts to fetch fresh currency data from the web asynchronously.true if web data was fetched successfully, false otherwise
In developer builds, this method returns mock data instead of making actual network requests.
TryLoadDataFromWebOverrideAsync
Forces a refresh of currency data from the web, overriding any existing cached data.true if refresh succeeded, false otherwise
See UnitConverter.h:200
Currency Callback Interface
TheIViewModelCurrencyCallback interface allows the view model to receive currency-specific updates.
Header: ~/workspace/source/src/CalcManager/UnitConverter.h:166
Callback Methods
CurrencyDataLoadFinished
Called when currency data loading completes.true if data loaded successfully, false if loading failedCurrencySymbolsCallback
Called when currency symbols are available.Currency symbol for the source unit (e.g., ”$”)
Currency symbol for the target unit (e.g., ”€“)
CurrencyRatiosCallback
Called with formatted currency conversion ratio strings.Standard formatted ratio string (e.g., “1 USD = 0.85 EUR”)
Accessibility-friendly ratio string (e.g., “1 US Dollar equals 0.85 Euros”)
CurrencyTimestampCallback
Called with the timestamp of the currency data.Formatted timestamp string indicating when rates were last updated
true if the data is more than a week old (may need refresh)NetworkBehaviorChanged
Called when the network behavior or connectivity status changes.New network behavior code
Currency Data Structures
CurrencyStaticData
Static metadata for a currency.ISO country code (e.g., “US”, “GB”)
Full country name (e.g., “United States”)In dev builds: Planet name instead (e.g., “Mars”, “Jupiter”)
ISO 4217 currency code (e.g., “USD”, “EUR”)
Full currency name (e.g., “US Dollar”)
Currency symbol (e.g., ”$”, ”€”, ”£”)
UnitConverter.h:143
CurrencyRatio
Conversion ratio between two currencies.Exchange rate from source to target currency
ISO 4217 code for the source currency
ISO 4217 code for the target currency
UnitConverter.h:152
Currency Unit Construction
Currency units have a specialized constructor that formats the display name differently:Unique identifier for the currency unit
Name of the currency (e.g., “Dollar”)
Name of the country (e.g., “United States”)
In dev builds: Planet name (e.g., “Mars”)
In dev builds: Planet name (e.g., “Mars”)
Currency code (e.g., “USD”)
Whether the language is right-to-left (affects name formatting order)
Whether this is the source unit
Whether this is the target unit
- For LTR languages:
"United States - Dollar"→name = "United States - Dollar" - For RTL languages:
"Dollar - United States"→name = "Dollar - United States" - Accessible name uses space instead of dash:
"United States Dollar"
UnitConverter.h:32
Integration with UnitConverter
TheUnitConverter class integrates currency conversion through several methods:
UpdateCurrencySymbols
Updates the currency symbols in the UI when units change.SetCurrentUnitTypes() or SwitchActive() is invoked for currency categories.
See UnitConverter.cpp:929
Currency-Specific Calculation Behavior
When converting currency values, theCalculate() method applies special formatting rules:
- Always use maximum precision (15 significant digits)
- Never use scientific notation
- Trim trailing zeros for cleaner display
UnitConverter.cpp:890
Usage Example
Mock Data Behavior
As documented in the README (~/workspace/source/README.md:64):
Developer Build Behavior
- Currency data uses planets instead of countries
- Exchange rates are static and do not change
- Network calls return mock data immediately
- Data “timestamp” reflects mock data generation, not real market data
- Test currency conversion UI without requiring API keys
- Work offline without network dependencies
- Have consistent, predictable test data
Related
- UnitConverter - Main converter class documentation
- Unit Struct - Currency units use the same Unit struct
- Category Struct - Currency is represented as a Category