support for using already cached data on first wallet launch

This commit is contained in:
noumantahir 2022-02-25 00:15:19 +05:00
parent 744935d8db
commit 114014dbcb
2 changed files with 52 additions and 39 deletions

View File

@ -46,10 +46,14 @@ const WalletScreen = ({navigation}) => {
const isDarkTheme = useAppSelector((state) => state.application.isDarkTheme);
const currency = useAppSelector((state)=>state.application.currency);
const selectedCoins = useAppSelector((state)=>state.wallet.selectedCoins);
const priceHistories = useAppSelector((state)=>state.wallet.priceHistories);
const coinsData = useAppSelector((state)=>state.wallet.coinsData);
const updateTimestamp = useAppSelector((state)=>state.wallet.updateTimestamp);
const {
selectedCoins,
priceHistories,
coinsData,
updateTimestamp,
quotes
} = useAppSelector((state)=>state.wallet);
const globalProps = useAppSelector((state)=>state.account.globalProps);
const currentAccount = useAppSelector((state)=>state.account.currentAccount);
const pinHash = useAppSelector((state)=>state.application.pin);
@ -64,10 +68,10 @@ const WalletScreen = ({navigation}) => {
_fetchData();
},[])
const _fetchData = () => {
const _fetchData = (refresh?:boolean) => {
if(!isLoading){
_fetchPriceHistory();
_fetchCoinsData();
_fetchCoinsData(refresh);
}
}
@ -75,7 +79,7 @@ const WalletScreen = ({navigation}) => {
const _fetchPriceHistory = () => {
selectedCoins.forEach(async (token:CoinBase)=>{
if(token.id !== 'ececny'){
if(!token.notCrypto){
const marketChart = await fetchMarketChart(token.id, currency.currency, CHART_DAYS_RANGE, INTERVAL_HOURLY);
const priceData = marketChart.prices.map(item=>item.yValue);
dispatch(setPriceHistory(token.id, currency.currency, priceData));
@ -84,14 +88,16 @@ const WalletScreen = ({navigation}) => {
})
}
const _fetchCoinsData = async () => {
const _fetchCoinsData = async (refresh?:boolean) => {
setIsLoading(true);
const coinData = await fetchCoinsData(
selectedCoins,
currentAccount.name,
currency.currency,
globalProps
);
const coinData = await fetchCoinsData({
coins:selectedCoins,
currentAccount,
vsCurrency:currency.currency,
globalProps,
quotes,
refresh
});
console.log("Coins Data", coinData)
dispatch(setCoinsData(coinData))
@ -103,7 +109,7 @@ const WalletScreen = ({navigation}) => {
setIsClaiming(true);
try{
await claimPoints()
await _fetchCoinsData();
await _fetchCoinsData(true);
}catch(error){
Alert.alert(`${error.message}\nTry again or write to support@ecency.com`);
}
@ -122,7 +128,7 @@ const WalletScreen = ({navigation}) => {
account.reward_hbd_balance,
account.reward_vesting_balance,
)
await _fetchCoinsData();
await _fetchCoinsData(true);
dispatch(
toastNotification(
intl.formatMessage({
@ -217,7 +223,7 @@ const WalletScreen = ({navigation}) => {
const _refreshControl = (
<RefreshControl
refreshing={refreshing}
onRefresh={() => {_fetchData(); setRefreshing(true)}}
onRefresh={() => {_fetchData(true); setRefreshing(true)}}
progressBackgroundColor="#357CE6"
tintColor={!isDarkTheme ? '#357ce6' : '#96c0ff'}
titleColor="#fff"
@ -235,7 +241,7 @@ const WalletScreen = ({navigation}) => {
<View style={styles.listWrapper}>
<FlatList
data={updateTimestamp ? selectedCoins : []}
extraData={coinsData}
extraData={[coinsData, priceHistories]}
style={globalStyles.tabBarBottom}
ListEmptyComponent={<PostCardPlaceHolder />}
ListHeaderComponent={_renderHeader}

View File

@ -3,8 +3,8 @@ import parseDate from './parseDate';
import parseToken from './parseToken';
import { vestsToHp } from './conversions';
import { getAccount, getAccountHistory } from '../providers/hive/dhive';
import { getCurrencyTokenRate } from '../providers/ecency/ecency';
import { CoinBase, CoinData } from '../redux/reducers/walletReducer';
import { getCurrencyTokenRate, getLatestQuotes } from '../providers/ecency/ecency';
import { CoinBase, CoinData, QuoteItem } from '../redux/reducers/walletReducer';
import { GlobalProps } from '../redux/reducers/accountReducer';
import { getEstimatedAmount } from './vote';
import { getUser as getEcencyUser, getUserPoints } from '../providers/ecency/ePoint';
@ -311,14 +311,24 @@ export const fetchCoinActivities = async (username:string, coinId:string, coinSy
}
export const fetchCoinsData = async (
export const fetchCoinsData = async ({
coins,
currentAccount,
vsCurrency,
globalProps,
refresh,
quotes,
}:{
coins:CoinBase[],
username:string,
currentAccount:any,
vsCurrency:string,
globalProps:GlobalProps
)
globalProps:GlobalProps,
quotes:{[key:string]:QuoteItem}
refresh:boolean,
})
: Promise<{[key:string]:CoinData}> => {
const username = currentAccount.username;
const {base, quote, hivePerMVests} = globalProps
const coinData = {} as {[key:string]:CoinData};
@ -330,20 +340,20 @@ export const fetchCoinsData = async (
}
//TODO: Use already available accoutn for frist wallet start
const userdata = await getAccount(username);
const ecencyUser = await getEcencyUser(username)
const userdata = refresh ? await getAccount(username) : currentAccount;
const _ecencyUserData = refresh ? await getEcencyUser(username) : currentAccount.ecencyUserData
//TODO: cache data in redux or fetch once on wallet startup
const ppHbd = await getCurrencyTokenRate(vsCurrency, 'hbd');
const ppHive = await getCurrencyTokenRate(vsCurrency, 'hive');
const ppEstm = await getCurrencyTokenRate(vsCurrency, 'estm');
const _prices = !refresh && quotes ? quotes : await getLatestQuotes(); //TODO: figure out a way to handle other currencies
coins.forEach((coinBase)=>{
switch(coinBase.id){
case COIN_IDS.ECENCY:{
const balance = ecencyUser.points ? parseFloat(ecencyUser.points) : 0;
const unclaimedFloat = parseFloat(ecencyUser.unclaimed_points || '0');
const balance = _ecencyUserData.points ? parseFloat(_ecencyUserData.points) : 0;
const unclaimedFloat = parseFloat(_ecencyUserData.unclaimed_points || '0');
const unclaimedBalance = unclaimedFloat ? unclaimedFloat + ' Points' : '';
const ppEstm = _prices[coinBase.id].price;
coinData[coinBase.id] = {
balance : Math.round(balance * 1000) / 1000,
@ -358,7 +368,8 @@ export const fetchCoinsData = async (
case COIN_IDS.HIVE:{
const balance = parseToken(userdata.balance);
const savings = parseToken(userdata.savings_balance);
const ppHive = _prices[coinBase.id].price;
coinData[coinBase.id] = {
balance: Math.round(balance * 1000) / 1000,
estimateValue: (balance + savings) * ppHive,
@ -374,6 +385,7 @@ export const fetchCoinsData = async (
case COIN_IDS.HBD:{
const balance = parseToken(userdata.hbd_balance);
const savings = parseToken(userdata.savings_hbd_balance);
const ppHbd = _prices[coinBase.id].price;
coinData[coinBase.id] = {
balance: Math.round(balance * 1000) / 1000,
@ -411,7 +423,7 @@ export const fetchCoinsData = async (
//TODO: assess how we can make this value change live.
const estimateVoteValueStr = '$ ' + getEstimatedAmount(userdata, globalProps);
const ppHive = _prices[COIN_IDS.HIVE].price;
coinData[coinBase.id] = {
balance: Math.round(balance * 1000) / 1000,
estimateValue: balance * ppHive,
@ -479,11 +491,6 @@ export const fetchCoinsData = async (
walletData.estimatedHiveValue = (walletData.balance + walletData.savingBalance) * ppHive;
walletData.estimatedHbdValue = totalHbd * ppHbd;
walletData.estimatedHpValue =
vestsToHp(walletData.vestingShares, walletData.hivePerMVests) * ppHive;
walletData.showPowerDown = userdata.next_vesting_withdrawal !== '1969-12-31T23:59:59';
const timeDiff = Math.abs(parseDate(userdata.next_vesting_withdrawal) - new Date());
walletData.nextVestingWithdrawal = Math.round(timeDiff / (1000 * 3600));