2020-12-12 20:33:42 -05:00
|
|
|
import React, { useState, useEffect, useContext } from 'react';
|
2020-11-20 21:47:13 -05:00
|
|
|
import { FlatList, ActivityIndicator, View, StyleSheet } from 'react-native';
|
2020-12-25 19:09:53 +03:00
|
|
|
import { useTheme } from '@react-navigation/native';
|
|
|
|
|
|
|
|
import navigationStyle from '../../components/navigationStyle';
|
|
|
|
import { SafeBlueArea, BlueListItem, BlueText, BlueCard } from '../../BlueComponents';
|
2020-11-21 19:16:20 -05:00
|
|
|
import { FiatUnit, FiatUnitSource } from '../../models/fiatUnit';
|
2020-07-20 16:38:46 +03:00
|
|
|
import loc from '../../loc';
|
2020-12-12 20:33:42 -05:00
|
|
|
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
2020-06-09 15:08:18 +01:00
|
|
|
const currency = require('../../blue_modules/currency');
|
2018-12-24 01:14:53 -05:00
|
|
|
|
2020-03-23 19:11:46 +04:00
|
|
|
const data = Object.values(FiatUnit);
|
2018-12-24 01:14:53 -05:00
|
|
|
|
2020-03-23 19:11:46 +04:00
|
|
|
const Currency = () => {
|
2020-12-12 20:33:42 -05:00
|
|
|
const { setPreferredFiatCurrency } = useContext(BlueStorageContext);
|
2020-03-23 19:11:46 +04:00
|
|
|
const [isSavingNewPreferredCurrency, setIsSavingNewPreferredCurrency] = useState(false);
|
|
|
|
const [selectedCurrency, setSelectedCurrency] = useState(null);
|
2020-07-15 13:32:59 -04:00
|
|
|
const { colors } = useTheme();
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
flex: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: colors.background,
|
|
|
|
},
|
|
|
|
activity: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: colors.background,
|
|
|
|
},
|
|
|
|
});
|
2018-12-24 01:14:53 -05:00
|
|
|
|
2020-03-23 19:11:46 +04:00
|
|
|
useEffect(() => {
|
|
|
|
const fetchCurrency = async () => {
|
|
|
|
try {
|
|
|
|
const preferredCurrency = await currency.getPreferredCurrency();
|
|
|
|
if (preferredCurrency === null) {
|
|
|
|
throw Error();
|
|
|
|
}
|
|
|
|
setSelectedCurrency(preferredCurrency);
|
|
|
|
} catch (_error) {
|
|
|
|
setSelectedCurrency(FiatUnit.USD);
|
2018-12-24 02:40:26 -05:00
|
|
|
}
|
2020-03-23 19:11:46 +04:00
|
|
|
};
|
|
|
|
fetchCurrency();
|
|
|
|
}, []);
|
2018-12-24 01:14:53 -05:00
|
|
|
|
2020-03-23 19:11:46 +04:00
|
|
|
if (selectedCurrency !== null && selectedCurrency !== undefined) {
|
2018-12-24 01:14:53 -05:00
|
|
|
return (
|
2021-03-22 07:54:17 -04:00
|
|
|
<SafeBlueArea>
|
2020-03-23 19:11:46 +04:00
|
|
|
<FlatList
|
2020-05-24 12:17:26 +03:00
|
|
|
style={styles.flex}
|
2020-03-23 19:11:46 +04:00
|
|
|
keyExtractor={(_item, index) => `${index}`}
|
|
|
|
data={data}
|
2020-11-20 21:47:13 -05:00
|
|
|
initialNumToRender={25}
|
2020-03-23 19:11:46 +04:00
|
|
|
extraData={data}
|
|
|
|
renderItem={({ item }) => {
|
|
|
|
return (
|
2020-09-21 21:53:28 -04:00
|
|
|
<BlueListItem
|
2020-03-23 19:11:46 +04:00
|
|
|
disabled={isSavingNewPreferredCurrency}
|
2020-05-03 14:17:49 -04:00
|
|
|
title={`${item.endPointKey} (${item.symbol})`}
|
2020-11-20 21:47:13 -05:00
|
|
|
checkmark={selectedCurrency.endPointKey === item.endPointKey}
|
2020-03-23 19:11:46 +04:00
|
|
|
onPress={async () => {
|
|
|
|
setIsSavingNewPreferredCurrency(true);
|
|
|
|
setSelectedCurrency(item);
|
|
|
|
await currency.setPrefferedCurrency(item);
|
|
|
|
await currency.startUpdater();
|
|
|
|
setIsSavingNewPreferredCurrency(false);
|
2020-12-12 20:33:42 -05:00
|
|
|
setPreferredFiatCurrency();
|
2020-03-23 19:11:46 +04:00
|
|
|
}}
|
2020-05-03 14:17:49 -04:00
|
|
|
/>
|
2020-03-23 19:11:46 +04:00
|
|
|
);
|
|
|
|
}}
|
2018-12-24 02:40:26 -05:00
|
|
|
/>
|
2020-03-23 19:11:46 +04:00
|
|
|
<BlueCard>
|
2020-11-22 04:42:21 -05:00
|
|
|
<BlueText>
|
2020-11-21 19:16:20 -05:00
|
|
|
{loc.settings.currency_source} {selectedCurrency.source ?? FiatUnitSource.CoinDesk}
|
2020-11-22 04:42:21 -05:00
|
|
|
</BlueText>
|
2020-03-23 19:11:46 +04:00
|
|
|
</BlueCard>
|
|
|
|
</SafeBlueArea>
|
2018-12-24 01:14:53 -05:00
|
|
|
);
|
|
|
|
}
|
2020-03-23 19:11:46 +04:00
|
|
|
return (
|
2020-05-24 12:17:26 +03:00
|
|
|
<View style={styles.activity}>
|
2020-03-23 19:11:46 +04:00
|
|
|
<ActivityIndicator />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
2018-12-24 01:14:53 -05:00
|
|
|
|
2021-02-15 11:03:54 +03:00
|
|
|
Currency.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.settings.currency }));
|
2020-12-25 19:09:53 +03:00
|
|
|
|
2020-03-23 19:11:46 +04:00
|
|
|
export default Currency;
|