fix(wallet): show pending requests of default accounts on dapps panel

- update badge icon and transactions state
This commit is contained in:
Pavneet-Sing
2022-08-25 16:31:09 +05:30
parent 512d26052f
commit 2cb015d1ca
8 changed files with 108 additions and 90 deletions
@@ -1326,6 +1326,10 @@ public abstract class BraveActivity<C extends ChromeActivityComponent> extends C
openBraveWalletDAppsActivity(
BraveWalletDAppsActivity.ActivityType.CONFIRM_TRANSACTION);
}
// update badge if there's a pending tx
if (transactionInfo != null && keyringInfo != null && !keyringInfo.isLocked) {
updateWalletBadgeVisibility();
}
});
mWalletModel.getDappsModel().mWalletIconNotificationVisible.observe(
this, visible -> { setWalletBadgeVisibility(visible); });
@@ -210,7 +210,8 @@ public class CryptoModel {
WalletUtils.getAccountInfosFromKeyrings(keyringInfos);
new SelectedAccountResponsesCollector(mKeyringService, coins, accountInfos)
.getAccounts(defaultAccountPerCoin -> {
mPendingTxHelper.setAccountInfos(accountInfos);
mPendingTxHelper.setAccountInfos(
new ArrayList<>(defaultAccountPerCoin));
});
});
}
@@ -5,7 +5,6 @@
package org.chromium.chrome.browser.app.domain;
import androidx.annotation.NonNull;
import androidx.annotation.UiThread;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
@@ -28,11 +27,11 @@ import org.chromium.mojo.system.MojoException;
import org.chromium.mojo.system.Pair;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class KeyringModel implements KeyringServiceObserver {
private KeyringService mKeyringService;
@@ -90,6 +89,28 @@ public class KeyringModel implements KeyringServiceObserver {
}
}
public void getDefaultAccountPerCoin(
Callbacks.Callback1<Set<AccountInfo>> defaultAccountPerCoins) {
synchronized (mLock) {
if (mKeyringService == null) {
return;
}
List<Integer> coins = new ArrayList<>();
for (CryptoAccountTypeInfo cryptoAccountTypeInfo :
mSharedData.getSupportedCryptoAccountTypes()) {
coins.add(cryptoAccountTypeInfo.getCoinType());
}
mKeyringService.getKeyringsInfo(mSharedData.getEnabledKeyrings(), keyringInfos -> {
List<AccountInfo> accountInfos =
WalletUtils.getAccountInfosFromKeyrings(keyringInfos);
new SelectedAccountResponsesCollector(mKeyringService, coins, accountInfos)
.getAccounts(defaultAccountPerCoin -> {
defaultAccountPerCoins.call(defaultAccountPerCoin);
});
});
}
}
private void update(int coinType) {
synchronized (mLock) {
if (mKeyringService == null) {
@@ -113,7 +134,9 @@ public class KeyringModel implements KeyringServiceObserver {
}
_mSelectedAccount.postValue(selectedAccountInfo);
} else if (accountInfos.size() > 0) {
_mSelectedAccount.postValue(accountInfos.get(0));
AccountInfo accountInfo = accountInfos.get(0);
_mSelectedAccount.postValue(accountInfo);
setSelectedAccount(accountInfo.address, accountInfo.coin);
}
});
});
@@ -59,7 +59,7 @@ public class WalletModel {
mDappsModel = new DappsModel(
mJsonRpcService, mBraveWalletService, mCryptoModel.getPendingTxHelper());
mKeyringModel = new KeyringModel(
keyringService, mCryptoModel.getSharedData(), braveWalletService, mCryptoActions);
mKeyringService, mCryptoModel.getSharedData(), mBraveWalletService, mCryptoActions);
// be careful with dependencies, must avoid cycles
mCryptoModel.setAccountInfosFromKeyRingModel(mKeyringModel.mAccountInfos);
init();
@@ -201,6 +201,8 @@ public class WalletModel {
mKeyringModel.update();
// update network per selected coin
mCryptoModel.getNetworkModel().init();
// update transactions
mCryptoModel.refreshTransactions();
});
}
@@ -19,6 +19,7 @@ import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.TransactionInfo;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.app.domain.WalletModel;
import org.chromium.chrome.browser.crypto_wallet.fragments.ApproveTxBottomSheetDialogFragment;
import org.chromium.chrome.browser.crypto_wallet.fragments.dapps.AddSwitchChainNetworkFragment;
import org.chromium.chrome.browser.crypto_wallet.fragments.dapps.AddTokenFragment;
@@ -27,8 +28,10 @@ import org.chromium.chrome.browser.crypto_wallet.fragments.dapps.EncryptionKeyFr
import org.chromium.chrome.browser.crypto_wallet.fragments.dapps.SignMessageFragment;
import org.chromium.chrome.browser.crypto_wallet.listeners.TransactionConfirmationListener;
import org.chromium.chrome.browser.crypto_wallet.util.PendingTxHelper;
import org.chromium.chrome.browser.crypto_wallet.util.TransactionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BraveWalletDAppsActivity extends BraveWalletBaseActivity
@@ -39,6 +42,7 @@ public class BraveWalletDAppsActivity extends BraveWalletBaseActivity
private ApproveTxBottomSheetDialogFragment approveTxBottomSheetDialogFragment;
private PendingTxHelper mPendingTxHelper;
private Fragment mFragment;
private WalletModel mWalletModel;
public enum ActivityType {
SIGN_MESSAGE(0),
@@ -83,21 +87,21 @@ public class BraveWalletDAppsActivity extends BraveWalletBaseActivity
intent.getIntExtra("activityType", ActivityType.ADD_ETHEREUM_CHAIN.getValue()));
BraveActivity activity = BraveActivity.getBraveActivity();
if (activity != null) {
activity.getWalletModel().getCryptoModel().mProcessNextDAppsRequest.observe(
this, activityType -> {
if (activityType == null) return;
switch (activityType) {
case GET_ENCRYPTION_PUBLIC_KEY_REQUEST:
case DECRYPT_REQUEST:
processPendingDappsRequest();
break;
case FINISH:
finish();
break;
default:
break;
}
});
mWalletModel = activity.getWalletModel();
mWalletModel.getCryptoModel().mProcessNextDAppsRequest.observe(this, activityType -> {
if (activityType == null) return;
switch (activityType) {
case GET_ENCRYPTION_PUBLIC_KEY_REQUEST:
case DECRYPT_REQUEST:
processPendingDappsRequest();
break;
case FINISH:
finish();
break;
default:
break;
}
});
}
onInitialLayoutInflationComplete();
}
@@ -129,11 +133,13 @@ public class BraveWalletDAppsActivity extends BraveWalletBaseActivity
@Override
public void onRejectAllTransactions() {
for (TransactionInfo transactionInfo : mPendingTxHelper.getPendingTransactions()) {
getTxService().rejectTransaction(CoinType.ETH, transactionInfo.id, success -> {
if (!success) {
Log.e(TAG, "Transaction failed " + transactionInfo.id);
}
});
getTxService().rejectTransaction(
TransactionUtils.getCoinFromTxDataUnion(transactionInfo.txDataUnion),
transactionInfo.id, success -> {
if (!success) {
Log.e(TAG, "Transaction failed " + transactionInfo.id);
}
});
}
mPendingTxHelper.destroy();
finish();
@@ -163,54 +169,48 @@ public class BraveWalletDAppsActivity extends BraveWalletBaseActivity
} else if (mActivityType == ActivityType.ADD_TOKEN) {
mFragment = new AddTokenFragment();
} else if (mActivityType == ActivityType.CONFIRM_TRANSACTION) {
getKeyringService().getKeyringInfo(
BraveWalletConstants.DEFAULT_KEYRING_ID, keyringInfo -> {
AccountInfo[] accountInfosTemp = keyringInfo.accountInfos;
if (accountInfosTemp == null) {
accountInfosTemp = new AccountInfo[] {};
mWalletModel.getKeyringModel().getDefaultAccountPerCoin(defaultAccountPerCoin -> {
AccountInfo[] accountInfos = defaultAccountPerCoin.toArray(new AccountInfo[0]);
if (mPendingTxHelper != null) {
mPendingTxHelper.destroy();
}
mPendingTxHelper = new PendingTxHelper(getTxService(), accountInfos, false, true);
mPendingTxHelper.mHasNoPendingTxAfterProcessing.observe(this, hasNoPendingTx -> {
if (hasNoPendingTx) {
finish();
}
});
final AccountInfo[] accounts = accountInfos;
mPendingTxHelper.mSelectedPendingRequest.observe(this, transactionInfo -> {
if (transactionInfo == null
|| mPendingTxHelper.getPendingTransactions().size() == 0) {
return;
}
if (approveTxBottomSheetDialogFragment != null
&& approveTxBottomSheetDialogFragment.isVisible()) {
// TODO: instead of dismiss, show the details of new Tx once
// onNextTransaction implementation is done
approveTxBottomSheetDialogFragment.dismiss();
}
String accountName = "";
for (AccountInfo accountInfo : accounts) {
if (accountInfo.address.equals(transactionInfo.fromAddress)) {
accountName = accountInfo.name;
break;
}
if (mPendingTxHelper != null) {
mPendingTxHelper.destroy();
}
mPendingTxHelper =
new PendingTxHelper(getTxService(), accountInfosTemp, false, true);
mPendingTxHelper.mHasNoPendingTxAfterProcessing.observe(
this, hasNoPendingTx -> {
if (hasNoPendingTx) {
finish();
}
});
final AccountInfo[] accounts = accountInfosTemp;
mPendingTxHelper.mSelectedPendingRequest.observe(this, transactionInfo -> {
if (transactionInfo == null
|| mPendingTxHelper.getPendingTransactions().size() == 0) {
return;
}
if (approveTxBottomSheetDialogFragment != null
&& approveTxBottomSheetDialogFragment.isVisible()) {
// TODO: instead of dismiss, show the details of new Tx once
// onNextTransaction implementation is done
approveTxBottomSheetDialogFragment.dismiss();
}
String accountName = "";
for (AccountInfo accountInfo : accounts) {
if (accountInfo.address.equals(transactionInfo.fromAddress)) {
accountName = accountInfo.name;
break;
}
}
approveTxBottomSheetDialogFragment =
ApproveTxBottomSheetDialogFragment.newInstance(
mPendingTxHelper.getPendingTransactions(),
transactionInfo, accountName, this);
approveTxBottomSheetDialogFragment.show(getSupportFragmentManager(),
ApproveTxBottomSheetDialogFragment.TAG_FRAGMENT);
mPendingTxHelper.mTransactionInfoLd.observe(this, transactionInfos -> {
approveTxBottomSheetDialogFragment.setTxList(transactionInfos);
});
});
mPendingTxHelper.fetchTransactions(() -> {});
}
approveTxBottomSheetDialogFragment =
ApproveTxBottomSheetDialogFragment.newInstance(
mPendingTxHelper.getPendingTransactions(), transactionInfo,
accountName, this);
approveTxBottomSheetDialogFragment.show(getSupportFragmentManager(),
ApproveTxBottomSheetDialogFragment.TAG_FRAGMENT);
mPendingTxHelper.mTransactionInfoLd.observe(this, transactionInfos -> {
approveTxBottomSheetDialogFragment.setTxList(transactionInfos);
});
});
mPendingTxHelper.fetchTransactions(() -> {});
});
} else if (mActivityType == ActivityType.ADD_ETHEREUM_CHAIN
|| mActivityType == ActivityType.SWITCH_ETHEREUM_CHAIN) {
mFragment = new AddSwitchChainNetworkFragment(mActivityType, this);
@@ -465,15 +465,6 @@ public class PortfolioFragment
return mCurrentPendingTx != null;
}
private void getPendingTx(AccountInfo[] accountInfos, @Nullable Runnable callback) {
PendingTxHelper pendingTxHelper = new PendingTxHelper(getTxService(), accountInfos, false);
pendingTxHelper.fetchTransactions(() -> {
mPendingTxInfos = pendingTxHelper.getTransactions();
pendingTxHelper.destroy();
if (callback != null) callback.run();
});
}
private void updatePendingTxNotification() {
Activity activity = getActivity();
if (activity instanceof BraveWalletActivity)
@@ -279,12 +279,9 @@ public class PendingTxHelper implements TxServiceObserverImplDelegate {
}
private void postTxUpdates() {
if (mTransactionInfos.size() > 0) {
_mSelectedPendingRequest.postValue(getFirstUnapprovedTx());
} else {
_mSelectedPendingRequest.postValue(null);
_mHasNoPendingTxAfterProcessing.postValue(true);
}
TransactionInfo mFirstPendingInfo = getFirstUnapprovedTx();
_mSelectedPendingRequest.postValue(mFirstPendingInfo);
_mHasNoPendingTxAfterProcessing.postValue(mFirstPendingInfo == null);
}
private void updatePending(List<TransactionInfo> transactionInfos) {
@@ -43,8 +43,7 @@ public class SelectedAccountResponsesCollector {
selectedAccountInfosPerCoinMultiResponse.singleResponseComplete, coin);
accountsPermissionsContexts.add(accountContext);
if (CoinType.FIL == coin) {
} else {
if (CoinType.FIL != coin) {
mKeyringService.getSelectedAccount(coin, accountContext);
}
}
@@ -63,8 +62,9 @@ public class SelectedAccountResponsesCollector {
break;
}
}
mSelectedAccountsPerCoin.add(selectedAccountInfo);
if (selectedAccountInfo != null) {
mSelectedAccountsPerCoin.add(selectedAccountInfo);
}
}
runWhenDone.call(mSelectedAccountsPerCoin);
});