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