2023-10-24 03:28:44 +02:00
|
|
|
import { useNavigation } from '@react-navigation/native';
|
2021-10-13 20:00:58 +02:00
|
|
|
import dayjs from 'dayjs';
|
2024-04-18 03:05:48 +02:00
|
|
|
import React, { useLayoutEffect, useState } from 'react';
|
2024-02-24 12:27:17 +01:00
|
|
|
import { FlatList, NativeSyntheticEvent, StyleSheet, View } from 'react-native';
|
|
|
|
import { BlueCard, BlueSpacing10, BlueText } from '../../BlueComponents';
|
2024-01-28 16:11:08 +01:00
|
|
|
import {
|
|
|
|
CurrencyRate,
|
|
|
|
getPreferredCurrency,
|
|
|
|
initCurrencyDaemon,
|
|
|
|
mostRecentFetchedRate,
|
|
|
|
setPreferredCurrency,
|
|
|
|
} from '../../blue_modules/currency';
|
2024-02-24 12:27:17 +01:00
|
|
|
import presentAlert from '../../components/Alert';
|
|
|
|
import ListItem from '../../components/ListItem';
|
|
|
|
import { useTheme } from '../../components/themes';
|
|
|
|
import loc from '../../loc';
|
|
|
|
import { FiatUnit, FiatUnitSource, FiatUnitType, getFiatRate } from '../../models/fiatUnit';
|
2024-04-18 03:05:48 +02:00
|
|
|
import { useSettings } from '../../components/Context/SettingsContext';
|
2021-10-13 20:00:58 +02:00
|
|
|
dayjs.extend(require('dayjs/plugin/calendar'));
|
2018-12-24 07:14:53 +01:00
|
|
|
|
2024-02-24 12:27:17 +01:00
|
|
|
const Currency = () => {
|
2024-04-18 03:05:48 +02:00
|
|
|
const { setPreferredFiatCurrencyStorage } = useSettings();
|
2020-03-23 16:11:46 +01:00
|
|
|
const [isSavingNewPreferredCurrency, setIsSavingNewPreferredCurrency] = useState(false);
|
2024-01-27 14:44:11 +01:00
|
|
|
const [selectedCurrency, setSelectedCurrency] = useState<FiatUnitType>(FiatUnit.USD);
|
|
|
|
const [currencyRate, setCurrencyRate] = useState<CurrencyRate>({ LastUpdated: null, Rate: null });
|
2020-07-15 19:32:59 +02:00
|
|
|
const { colors } = useTheme();
|
2024-01-27 14:44:11 +01:00
|
|
|
const { setOptions } = useNavigation<any>();
|
|
|
|
|
2023-04-11 22:29:36 +02:00
|
|
|
const [search, setSearch] = useState('');
|
|
|
|
const data = Object.values(FiatUnit).filter(item => item.endPointKey.toLowerCase().includes(search.toLowerCase()));
|
2024-01-27 14:44:11 +01:00
|
|
|
|
2020-07-15 19:32:59 +02:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
flex: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: colors.background,
|
|
|
|
},
|
|
|
|
});
|
2018-12-24 07:14:53 +01:00
|
|
|
|
2021-10-13 20:00:58 +02:00
|
|
|
const fetchCurrency = async () => {
|
2024-01-28 16:11:08 +01:00
|
|
|
let preferredCurrency;
|
2021-10-13 20:00:58 +02:00
|
|
|
try {
|
2024-01-28 16:11:08 +01:00
|
|
|
preferredCurrency = await getPreferredCurrency();
|
2021-10-13 20:00:58 +02:00
|
|
|
if (preferredCurrency === null) {
|
|
|
|
throw Error();
|
2018-12-24 08:40:26 +01:00
|
|
|
}
|
2021-10-13 20:00:58 +02:00
|
|
|
setSelectedCurrency(preferredCurrency);
|
|
|
|
} catch (_error) {
|
2024-01-28 16:11:08 +01:00
|
|
|
setSelectedCurrency(FiatUnit.USD);
|
2021-10-13 20:00:58 +02:00
|
|
|
}
|
2024-01-28 16:11:08 +01:00
|
|
|
const mostRecentFetchedRateValue = await mostRecentFetchedRate();
|
|
|
|
setCurrencyRate(mostRecentFetchedRateValue);
|
2021-10-13 20:00:58 +02:00
|
|
|
};
|
|
|
|
|
2023-04-11 22:29:36 +02:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
setOptions({
|
2024-01-20 18:27:48 +01:00
|
|
|
headerSearchBarOptions: {
|
2024-01-27 14:44:11 +01:00
|
|
|
onChangeText: (event: NativeSyntheticEvent<{ text: string }>) => setSearch(event.nativeEvent.text),
|
2023-04-11 22:29:36 +02:00
|
|
|
},
|
|
|
|
});
|
2020-03-23 16:11:46 +01:00
|
|
|
fetchCurrency();
|
2023-04-11 22:29:36 +02:00
|
|
|
}, [setOptions]);
|
2018-12-24 07:14:53 +01:00
|
|
|
|
2024-01-27 14:44:11 +01:00
|
|
|
const renderItem = ({ item }: { item: FiatUnitType }) => (
|
|
|
|
<ListItem
|
|
|
|
disabled={isSavingNewPreferredCurrency || selectedCurrency.endPointKey === item.endPointKey}
|
|
|
|
title={`${item.endPointKey} (${item.symbol})`}
|
2024-02-25 22:45:49 +01:00
|
|
|
containerStyle={StyleSheet.flatten([styles.flex, { minHeight: 60 }])}
|
2024-01-27 14:44:11 +01:00
|
|
|
checkmark={selectedCurrency.endPointKey === item.endPointKey}
|
|
|
|
onPress={async () => {
|
|
|
|
setIsSavingNewPreferredCurrency(true);
|
|
|
|
try {
|
|
|
|
await getFiatRate(item.endPointKey);
|
2024-01-28 16:11:08 +01:00
|
|
|
await setPreferredCurrency(item);
|
|
|
|
await initCurrencyDaemon(true);
|
2024-01-27 14:44:11 +01:00
|
|
|
await fetchCurrency();
|
|
|
|
setSelectedCurrency(item);
|
2024-04-18 03:05:48 +02:00
|
|
|
setPreferredFiatCurrencyStorage(FiatUnit[item.endPointKey]);
|
2024-01-27 14:44:11 +01:00
|
|
|
} catch (error: any) {
|
|
|
|
console.log(error);
|
2024-02-07 20:24:24 +01:00
|
|
|
presentAlert({
|
|
|
|
message: error.message ? `${loc.settings.currency_fetch_error}: ${error.message}}` : loc.settings.currency_fetch_error,
|
|
|
|
});
|
2024-01-27 14:44:11 +01:00
|
|
|
} finally {
|
|
|
|
setIsSavingNewPreferredCurrency(false);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2020-03-23 16:11:46 +01:00
|
|
|
return (
|
2024-01-13 15:56:29 +01:00
|
|
|
<View style={styles.flex}>
|
2023-04-11 22:29:36 +02:00
|
|
|
<FlatList
|
2024-01-13 15:56:29 +01:00
|
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
|
|
automaticallyAdjustContentInsets
|
2023-04-11 22:29:36 +02:00
|
|
|
keyExtractor={(_item, index) => `${index}`}
|
|
|
|
data={data}
|
2024-01-27 14:44:11 +01:00
|
|
|
initialNumToRender={30}
|
2023-04-11 22:29:36 +02:00
|
|
|
extraData={data}
|
2024-01-27 14:44:11 +01:00
|
|
|
renderItem={renderItem}
|
2023-04-11 22:29:36 +02:00
|
|
|
/>
|
|
|
|
<BlueCard>
|
|
|
|
<BlueText>
|
|
|
|
{loc.settings.currency_source} {selectedCurrency?.source ?? FiatUnitSource.CoinDesk}
|
|
|
|
</BlueText>
|
|
|
|
<BlueSpacing10 />
|
|
|
|
<BlueText>
|
|
|
|
{loc.settings.rate}: {currencyRate.Rate ?? loc._.never}
|
|
|
|
</BlueText>
|
|
|
|
<BlueSpacing10 />
|
|
|
|
<BlueText>
|
2024-01-27 14:44:11 +01:00
|
|
|
{/* @ts-ignore TODO: fix typescript error later */}
|
2023-04-11 22:29:36 +02:00
|
|
|
{loc.settings.last_updated}: {dayjs(currencyRate.LastUpdated).calendar() ?? loc._.never}
|
|
|
|
</BlueText>
|
|
|
|
</BlueCard>
|
2024-01-13 15:56:29 +01:00
|
|
|
</View>
|
2020-03-23 16:11:46 +01:00
|
|
|
);
|
|
|
|
};
|
2018-12-24 07:14:53 +01:00
|
|
|
|
2020-03-23 16:11:46 +01:00
|
|
|
export default Currency;
|