Skip to content

Commit

Permalink
Fix issue with wallet balances not updating in wallets view and Dapp …
Browse files Browse the repository at this point in the history
…browser.
  • Loading branch information
JamesSmartCell committed Jun 8, 2020
1 parent 6875a04 commit 4575e4e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import com.alphawallet.app.repository.entity.RealmToken;
import com.alphawallet.app.repository.entity.RealmTokenTicker;
import com.alphawallet.app.service.RealmManager;
import com.alphawallet.token.entity.MagicLinkInfo;

import org.web3j.crypto.WalletUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -173,7 +175,16 @@ private void saveTokenLocal(Wallet wallet, Token token)

@Override
public Single<Token> fetchEnabledToken(int chainId, Wallet wallet, String address) {
return Single.fromCallable(() -> fetchToken(chainId, wallet, address));
return Single.fromCallable(() -> {
Token t = fetchToken(chainId, wallet, address);
if (t == null)
{
t = new Token(new TokenInfo(address, "", "", 18, false, chainId),
BigDecimal.ZERO, System.currentTimeMillis(), MagicLinkInfo.getNetworkNameById(chainId), ContractType.NOT_SET);
t.setTokenWallet(wallet.address);
}
return t;
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ public void onHistoryItemRemoved(DApp dApp) {
public void onDestroy()
{
super.onDestroy();
viewModel.onDestroy();
}

private void setupMenu(View baseView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Intent;
import android.os.TransactionTooLargeException;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;

import com.alphawallet.app.entity.Operation;
import com.alphawallet.app.entity.QrUrlResult;
Expand Down Expand Up @@ -37,6 +38,7 @@

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import com.alphawallet.token.tools.Numeric;
import com.alphawallet.app.interact.CreateTransactionInteract;
Expand All @@ -53,6 +55,7 @@
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.alphawallet.app.C.Key.WALLET;

Expand All @@ -62,6 +65,8 @@ public class DappBrowserViewModel extends BaseViewModel {
private final MutableLiveData<GasSettings> gasSettings = new MutableLiveData<>();
private final MutableLiveData<Token> token = new MutableLiveData<>();

private static final int BALANCE_CHECK_INTERVAL_SECONDS = 20;

private final FindDefaultNetworkInteract findDefaultNetworkInteract;
private final GenericWalletInteract genericWalletInteract;
private final AssetDefinitionService assetDefinitionService;
Expand All @@ -73,6 +78,8 @@ public class DappBrowserViewModel extends BaseViewModel {
private final KeyService keyService;

private ArrayList<String> bookmarks;
@Nullable
private Disposable balanceTimerDisposable;

DappBrowserViewModel(
FindDefaultNetworkInteract findDefaultNetworkInteract,
Expand Down Expand Up @@ -143,9 +150,18 @@ private void onDefaultNetwork(NetworkInfo networkInfo) {
.subscribe(this::onDefaultWallet, this::onError);
}

private void onDefaultWallet(Wallet wallet) {
private void onDefaultWallet(final Wallet wallet) {
defaultWallet.setValue(wallet);
//get the balance token

if (balanceTimerDisposable != null && !balanceTimerDisposable.isDisposed()) balanceTimerDisposable.dispose();

balanceTimerDisposable = Observable.interval(0, BALANCE_CHECK_INTERVAL_SECONDS, TimeUnit.SECONDS)
.doOnNext(l -> checkBalance(wallet)).subscribe();
}

private void checkBalance(final Wallet wallet)
{
Token blank = ethereumNetworkRepository.getBlankOverrideToken(defaultNetwork.getValue());
String address = blank.getAddress().equals(Address.DEFAULT.toString()) ? wallet.address.toLowerCase() : blank.getAddress().toLowerCase();
disposable = fetchTokensInteract.fetchStoredToken(defaultNetwork.getValue(), wallet, address)
Expand Down Expand Up @@ -316,4 +332,9 @@ public void showMyAddress(Context ctx)
intent.putExtra(WALLET, defaultWallet.getValue());
ctx.startActivity(intent);
}

public void onDestroy()
{
if (balanceTimerDisposable != null && !balanceTimerDisposable.isDisposed()) balanceTimerDisposable.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,11 @@ private void onFetchError(Wallet wallet, NetworkInfo networkInfo)

private void updateWallet(Token token)
{
if (walletBalances.containsKey(token.getWallet().toLowerCase()))
Wallet wallet = walletBalances.get(token.getWallet().toLowerCase());
if (wallet != null)
{
Wallet wallet = walletBalances.get(token.getWallet().toLowerCase());
if (wallet != null)
{
if (wallet.setWalletBalance(token))
{
//update wallet balance
updateBalance.postValue(wallet);
}
}
wallet.setWalletBalance(token);
updateBalance.postValue(wallet);
}
storeWallets();
}
Expand Down

0 comments on commit 4575e4e

Please sign in to comment.