2020-03-23 19:11:46 +04:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-05-24 12:17:26 +03:00
|
|
|
import { FlatList, TouchableOpacity, ActivityIndicator, View, StyleSheet } from 'react-native';
|
2020-09-21 21:53:28 -04:00
|
|
|
import { SafeBlueArea, BlueListItem, BlueTextHooks, BlueCard, BlueNavigationStyle } from '../../BlueComponents';
|
2018-12-24 01:14:53 -05:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { FiatUnit } from '../../models/fiatUnit';
|
2020-07-20 16:38:46 +03:00
|
|
|
import loc from '../../loc';
|
2020-07-15 13:32:59 -04:00
|
|
|
import { useTheme } from '@react-navigation/native';
|
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 = () => {
|
|
|
|
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 (
|
2020-05-24 12:17:26 +03:00
|
|
|
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={styles.flex}>
|
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}
|
|
|
|
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})`}
|
|
|
|
{...(selectedCurrency.endPointKey === item.endPointKey
|
2020-09-21 21:53:28 -04:00
|
|
|
? { rightIcon: { name: 'check', type: 'octaicon', color: '#0070FF' } }
|
2020-05-03 14:17:49 -04:00
|
|
|
: { hideChevron: true })}
|
|
|
|
Component={TouchableOpacity}
|
2020-03-23 19:11:46 +04:00
|
|
|
onPress={async () => {
|
|
|
|
setIsSavingNewPreferredCurrency(true);
|
|
|
|
setSelectedCurrency(item);
|
|
|
|
await currency.setPrefferedCurrency(item);
|
|
|
|
await currency.startUpdater();
|
|
|
|
setIsSavingNewPreferredCurrency(false);
|
|
|
|
}}
|
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-07-20 16:38:46 +03:00
|
|
|
<BlueTextHooks>{loc.settings.currency_source}</BlueTextHooks>
|
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
|
|
|
|
|
|
|
Currency.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
|
|
|
navigate: PropTypes.func,
|
|
|
|
goBack: PropTypes.func,
|
|
|
|
}),
|
|
|
|
};
|
2020-03-23 19:11:46 +04:00
|
|
|
|
|
|
|
Currency.navigationOptions = () => ({
|
|
|
|
...BlueNavigationStyle(),
|
|
|
|
title: loc.settings.currency,
|
|
|
|
});
|
|
|
|
export default Currency;
|