From 04b3d5a4ca308e99d0b7a273856795d9347dc4d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Rodriguez=20V=C3=A9lez?= Date: Wed, 13 Oct 2021 14:00:58 -0400 Subject: [PATCH] ADD: Rate information --- blue_modules/currency.js | 9 +++---- loc/en.json | 2 ++ screen/settings/currency.js | 52 +++++++++++++++++++++++++++---------- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/blue_modules/currency.js b/blue_modules/currency.js index cfb7ac73e..478b9e183 100644 --- a/blue_modules/currency.js +++ b/blue_modules/currency.js @@ -85,14 +85,13 @@ async function updateExchangeRate() { let rate; try { rate = await getFiatRate(preferredFiatCurrency.endPointKey); + exchangeRates[LAST_UPDATED] = +new Date(); + exchangeRates['BTC_' + preferredFiatCurrency.endPointKey] = rate; + await AsyncStorage.setItem(EXCHANGE_RATES_STORAGE_KEY, JSON.stringify(exchangeRates)); } catch (Err) { + console.log('Error encountered when attempting to update exchange rate...'); console.warn(Err.message); - return; } - - exchangeRates[LAST_UPDATED] = +new Date(); - exchangeRates['BTC_' + preferredFiatCurrency.endPointKey] = rate; - await AsyncStorage.setItem(EXCHANGE_RATES_STORAGE_KEY, JSON.stringify(exchangeRates)); } /** diff --git a/loc/en.json b/loc/en.json index 2bd02741f..213be13ac 100644 --- a/loc/en.json +++ b/loc/en.json @@ -337,6 +337,7 @@ "groundcontrol_explanation": "GroundControl is a free, open-source push notifications server for Bitcoin wallets. You can install your own GroundControl server and put its URL here to not rely on BlueWallet’s infrastructure. Leave blank to use GroundControl’s default server.", "header": "Settings", "language": "Language", + "last_updated": "Last Updated", "language_isRTL": "Restarting BlueWallet is required for the language orientation to take effect.", "lightning_error_lndhub_uri": "Invalid LNDHub URI", "lightning_saved": "Your changes have been saved successfully.", @@ -363,6 +364,7 @@ "privacy_do_not_track": "Disable Analytics", "privacy_do_not_track_explanation": "Performance and reliability information will not be submitted for analysis.", "push_notifications": "Push Notifications", + "rate": "Rate", "retype_password": "Re-type password", "selfTest": "Self-Test", "save": "Save", diff --git a/screen/settings/currency.js b/screen/settings/currency.js index a654bfd7a..c52108ce2 100644 --- a/screen/settings/currency.js +++ b/screen/settings/currency.js @@ -3,18 +3,21 @@ import { FlatList, ActivityIndicator, View, StyleSheet } from 'react-native'; import { useTheme } from '@react-navigation/native'; import navigationStyle from '../../components/navigationStyle'; -import { SafeBlueArea, BlueListItem, BlueText, BlueCard } from '../../BlueComponents'; +import { SafeBlueArea, BlueListItem, BlueText, BlueCard, BlueSpacing10 } from '../../BlueComponents'; import { FiatUnit, FiatUnitSource } from '../../models/fiatUnit'; import loc from '../../loc'; import { BlueStorageContext } from '../../blue_modules/storage-context'; +import AsyncStorage from '@react-native-async-storage/async-storage'; +import dayjs from 'dayjs'; +dayjs.extend(require('dayjs/plugin/calendar')); const currency = require('../../blue_modules/currency'); - const data = Object.values(FiatUnit); const Currency = () => { const { setPreferredFiatCurrency } = useContext(BlueStorageContext); const [isSavingNewPreferredCurrency, setIsSavingNewPreferredCurrency] = useState(false); const [selectedCurrency, setSelectedCurrency] = useState(null); + const [currencyRate, setCurrencyRate] = useState({ LastUpdated: null, Rate: null }); const { colors } = useTheme(); const styles = StyleSheet.create({ flex: { @@ -29,18 +32,32 @@ const Currency = () => { }, }); - useEffect(() => { - const fetchCurrency = async () => { - try { - const preferredCurrency = await currency.getPreferredCurrency(); - if (preferredCurrency === null) { - throw Error(); - } - setSelectedCurrency(preferredCurrency); - } catch (_error) { - setSelectedCurrency(FiatUnit.USD); + const fetchCurrency = async () => { + let preferredCurrency = FiatUnit.USD; + try { + preferredCurrency = await currency.getPreferredCurrency(); + if (preferredCurrency === null) { + throw Error(); } - }; + setSelectedCurrency(preferredCurrency); + } catch (_error) { + setSelectedCurrency(preferredCurrency); + } + AsyncStorage.getItem(currency.EXCHANGE_RATES).then(currencyInformation => { + const formatter = new Intl.NumberFormat(preferredCurrency.locale, { + style: 'currency', + currency: preferredCurrency.endPointKey, + minimumFractionDigits: 2, + maximumFractionDigits: 8, + }); + setCurrencyRate({ + LastUpdated: currencyInformation[currency.LAST_UPDATED], + Rate: formatter.format(JSON.parse(currencyInformation)[`BTC_${preferredCurrency.endPointKey}`]), + }); + }); + }; + + useEffect(() => { fetchCurrency(); }, []); @@ -66,6 +83,7 @@ const Currency = () => { await currency.init(true); setIsSavingNewPreferredCurrency(false); setPreferredFiatCurrency(); + fetchCurrency(); }} /> ); @@ -75,6 +93,14 @@ const Currency = () => { {loc.settings.currency_source} {selectedCurrency.source ?? FiatUnitSource.CoinDesk} + + + {loc.settings.rate}: {currencyRate.Rate ?? loc._.never} + + + + {loc.settings.last_updated}: {dayjs(currencyRate.LastUpdated).calendar() ?? loc._.never} + );