mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-03-13 11:09:20 +01:00
FIX: Added catch for preferredCurrency and loading indicator in Settings -> Currency
This commit is contained in:
parent
dc3f782a54
commit
054b92d7f7
3 changed files with 65 additions and 22 deletions
21
currency.js
21
currency.js
|
@ -14,7 +14,15 @@ const STRUCT = {
|
|||
};
|
||||
|
||||
async function updateExchangeRate() {
|
||||
preferredFiatCurrency = JSON.parse(await AsyncStorage.getItem(AppStorage.PREFERREDCURRENCY));
|
||||
let preferredFiatCurrency;
|
||||
try {
|
||||
preferredFiatCurrency = JSON.parse(await AsyncStorage.getItem(AppStorage.PREFERREDCURRENCY));
|
||||
if (preferredFiatCurrency === null) {
|
||||
throw Error();
|
||||
}
|
||||
} catch (_error) {
|
||||
preferredFiatCurrency = FiatUnit.USD;
|
||||
}
|
||||
if (+new Date() - lang[STRUCT.LAST_UPDATED] <= 30 * 60 * 1000) {
|
||||
// not updating too often
|
||||
return;
|
||||
|
@ -42,9 +50,16 @@ async function updateExchangeRate() {
|
|||
async function startUpdater(force = false) {
|
||||
if (force) {
|
||||
const lang = JSON.parse(await AsyncStorage.getItem(AppStorage.CURRENCY));
|
||||
delete lang[STRUCT.LAST_UPDATED]
|
||||
delete lang[STRUCT.LAST_UPDATED];
|
||||
await AsyncStorage.setItem(AppStorage.CURRENCY, JSON.stringify(lang));
|
||||
preferredFiatCurrency = JSON.parse(await AsyncStorage.getItem(AppStorage.PREFERREDCURRENCY));
|
||||
try {
|
||||
preferredFiatCurrency = JSON.parse(await AsyncStorage.getItem(AppStorage.PREFERREDCURRENCY));
|
||||
if (preferredFiatCurrency === null) {
|
||||
throw Error();
|
||||
}
|
||||
} catch (_error) {
|
||||
preferredFiatCurrency = FiatUnit.USD;
|
||||
}
|
||||
}
|
||||
lang = await AsyncStorage.getItem(AppStorage.CURRENCY);
|
||||
try {
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
</dict>
|
||||
</array>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>167</string>
|
||||
<string>168</string>
|
||||
<key>ITSAppUsesNonExemptEncryption</key>
|
||||
<false/>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import React, { Component } from 'react';
|
||||
import { FlatList, TouchableOpacity, AsyncStorage } from 'react-native';
|
||||
import { FlatList, TouchableOpacity, AsyncStorage, ActivityIndicator, View } from 'react-native';
|
||||
import { SafeBlueArea, BlueNavigationStyle, BlueListItem } from '../../BlueComponents';
|
||||
import PropTypes from 'prop-types';
|
||||
import { Icon } from 'react-native-elements';
|
||||
import { AppStorage } from '../../class';
|
||||
import { FiatUnit } from '../../models/fiatUnit';
|
||||
/** @type {AppStorage} */
|
||||
|
@ -16,39 +17,66 @@ export default class Currency extends Component {
|
|||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { data: Object.values(FiatUnit) };
|
||||
this.state = { data: Object.values(FiatUnit), isSavingNewPreferredCurrency: false };
|
||||
}
|
||||
|
||||
async componentDidMount() {
|
||||
const preferredCurrency = (await AsyncStorage.getItem(AppStorage.PREFERREDCURRENCY)) || FiatUnit.USD;
|
||||
this.setState({ selectedCurrency: JSON.parse(preferredCurrency) });
|
||||
try {
|
||||
const preferredCurrency = await AsyncStorage.getItem(AppStorage.PREFERREDCURRENCY);
|
||||
if (preferredCurrency === null) {
|
||||
throw Error();
|
||||
}
|
||||
this.setState({ selectedCurrency: JSON.parse(preferredCurrency) });
|
||||
} catch (_error) {
|
||||
this.setState({ selectedCurrency: FiatUnit.USD });
|
||||
}
|
||||
}
|
||||
|
||||
renderItem = ({ item }) => {
|
||||
return (
|
||||
<TouchableOpacity
|
||||
onPress={async() => {
|
||||
await AsyncStorage.setItem(AppStorage.PREFERREDCURRENCY, JSON.stringify(item));
|
||||
await currency.startUpdater(true)
|
||||
this.props.navigation.goBack(null);
|
||||
onPress={() => {
|
||||
this.setState({ isSavingNewPreferredCurrency: true, selectedCurrency: item }, async () => {
|
||||
await AsyncStorage.setItem(AppStorage.PREFERREDCURRENCY, JSON.stringify(item));
|
||||
await currency.startUpdater(true);
|
||||
this.setState({ isSavingNewPreferredCurrency: false });
|
||||
});
|
||||
}}
|
||||
>
|
||||
<BlueListItem title={item.symbol + ' ' + item.formatterValue} hideChevron />
|
||||
<BlueListItem
|
||||
title={item.symbol + ' ' + item.formatterValue}
|
||||
{...(this.state.selectedCurrency.formatterValue === item.formatterValue
|
||||
? {
|
||||
rightIcon: this.state.selectedNewCurrency ? (
|
||||
<ActivityIndicator />
|
||||
) : (
|
||||
<Icon name="check" type="font-awesome" color="#0c2550" />
|
||||
),
|
||||
}
|
||||
: { hideChevron: true })}
|
||||
/>
|
||||
</TouchableOpacity>
|
||||
);
|
||||
};
|
||||
|
||||
render() {
|
||||
if (this.state.selectedCurrency !== null && this.state.selectedCurrency !== undefined) {
|
||||
return (
|
||||
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
|
||||
<FlatList
|
||||
style={{ flex: 1 }}
|
||||
keyExtractor={(_item, index) => `${index}`}
|
||||
data={this.state.data}
|
||||
extraData={this.state.data}
|
||||
renderItem={this.renderItem}
|
||||
/>
|
||||
</SafeBlueArea>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
|
||||
<FlatList
|
||||
style={{ flex: 1 }}
|
||||
keyExtractor={(_item, index) => `${index}`}
|
||||
data={this.state.data}
|
||||
extraData={this.state.data}
|
||||
renderItem={this.renderItem}
|
||||
/>
|
||||
</SafeBlueArea>
|
||||
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
||||
<ActivityIndicator />
|
||||
</View>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue