Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show test networks separately #19467

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class NetworkModel implements JsonRpcServiceObserver {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSubTestNetworks method is unused and can be safely removed now.

Copy link
Author

@Pavneet-Sing Pavneet-Sing Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated d194853

Expand All @@ -65,6 +64,7 @@ public class NetworkModel implements JsonRpcServiceObserver {
private final MediatorLiveData<List<NetworkInfo>> _mPrimaryNetworks;
private final MediatorLiveData<List<NetworkInfo>> _mSecondaryNetworks;
private Map<String, NetworkSelectorModel> mNetworkSelectorMap;
private MutableLiveData<NetworkLists> _mNetworkLists;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be final.

Copy link
Author

@Pavneet-Sing Pavneet-Sing Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated d194853

private OriginInfo mOriginInfo;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mOriginInfo is unused and can be safely removed.

Copy link
Author

@Pavneet-Sing Pavneet-Sing Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated d194853

public final LiveData<String[]> mCustomNetworkIds;
public LiveData<NetworkInfo> mNeedToCreateAccountForNetwork;
Expand All @@ -76,6 +76,7 @@ public class NetworkModel implements JsonRpcServiceObserver {
public final LiveData<NetworkInfo> mDefaultNetwork;
public final LiveData<List<NetworkInfo>> mPrimaryNetworks;
public final LiveData<List<NetworkInfo>> mSecondaryNetworks;
public LiveData<NetworkLists> mNetworkLists;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be final as well.

Copy link
Author

@Pavneet-Sing Pavneet-Sing Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated d194853

private Origin mOrigin;

public NetworkModel(BraveWalletService braveWalletService, JsonRpcService jsonRpcService,
Expand Down Expand Up @@ -109,6 +110,8 @@ public NetworkModel(BraveWalletService braveWalletService, JsonRpcService jsonRp
mSecondaryNetworks = _mSecondaryNetworks;
jsonRpcService.addObserver(this);
mNetworkSelectorMap = new HashMap<>();
_mNetworkLists = new MutableLiveData<>();
mNetworkLists = _mNetworkLists;
_mPairChainAndNetwork.setValue(Pair.create("", Collections.emptyList()));
_mPairChainAndNetwork.addSource(_mChainId, chainId -> {
_mPairChainAndNetwork.setValue(
Expand Down Expand Up @@ -271,23 +274,23 @@ public void refreshNetworks() {
}

static void getAllNetworks(JsonRpcService jsonRpcService, List<Integer> supportedCoins,
Callbacks.Callback1<Set<NetworkInfo>> callback) {
Callbacks.Callback1<List<NetworkInfo>> callback) {
if (jsonRpcService == null) {
callback.call(Collections.emptySet());
callback.call(Collections.emptyList());
return;
}

NetworkResponsesCollector networkResponsesCollector =
new NetworkResponsesCollector(jsonRpcService, supportedCoins);
networkResponsesCollector.getNetworks(networkInfoSet -> {
networkResponsesCollector.getNetworks(networkInfoList -> {
if (!AndroidUtils.isDebugBuild()) {
networkInfoSet =
networkInfoSet.stream()
networkInfoList =
networkInfoList.stream()
.filter(networkInfo
-> !NetworkUtils.Filters.isLocalNetwork(networkInfo))
.collect(Collectors.toSet());
.collect(Collectors.toList());
}
callback.call(networkInfoSet);
callback.call(networkInfoList);
});
}

Expand All @@ -311,8 +314,31 @@ public void init() {
}
}

getAllNetworks(mJsonRpcService, mSharedData.getSupportedCryptoCoins(),
allNetworks -> { _mCryptoNetworks.postValue(new ArrayList<>(allNetworks)); });
getAllNetworks(
mJsonRpcService, mSharedData.getSupportedCryptoCoins(), cryptoNetworks -> {
_mCryptoNetworks.postValue(cryptoNetworks);

List<NetworkInfo> primary = new ArrayList<>();
List<NetworkInfo> secondary = new ArrayList<>();
List<NetworkInfo> test = new ArrayList<>();
NetworkLists networkLists =
new NetworkLists(cryptoNetworks, primary, secondary, test);
for (NetworkInfo networkInfo : cryptoNetworks) {
if (WalletConstants.SUPPORTED_TOP_LEVEL_CHAIN_IDS.contains(
networkInfo.chainId)) {
primary.add(networkInfo);
} else if (WalletConstants.KNOWN_TEST_CHAIN_IDS.contains(
networkInfo.chainId)) {
test.add(networkInfo);
} else if (!WalletConstants.SUPPORTED_TOP_LEVEL_CHAIN_IDS.contains(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can just do else { ... without conditions check as they always pass on that stage because of 2 conditions above.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a matter of taste, but I would probably add a forth else where we log an error (as it should never happen), or throw an exception.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced with else d194853

networkInfo.chainId)
&& !WalletConstants.KNOWN_TEST_CHAIN_IDS.contains(
networkInfo.chainId)) {
secondary.add(networkInfo);
}
}
_mNetworkLists.postValue(networkLists);
});
}
}

Expand Down Expand Up @@ -455,4 +481,33 @@ public void onConnectionError(MojoException e) {}

@Override
public void close() {}

public static class NetworkLists {
// Networks from core.
public List<NetworkInfo> mCoreNetworks;
public List<NetworkInfo> mPrimaryNetworkList;
public List<NetworkInfo> mSecondaryNetworkList;
public List<NetworkInfo> mTestNetworkList;

public NetworkLists() {
mCoreNetworks = Collections.emptyList();
mPrimaryNetworkList = Collections.emptyList();
mSecondaryNetworkList = Collections.emptyList();
mTestNetworkList = Collections.emptyList();
}
public NetworkLists(List<NetworkInfo> mCoreNetworks, List<NetworkInfo> mPrimaryNetworkList,
List<NetworkInfo> mSecondaryNetworkList, List<NetworkInfo> mTestNetworkList) {
this.mCoreNetworks = mCoreNetworks;
this.mPrimaryNetworkList = mPrimaryNetworkList;
this.mSecondaryNetworkList = mSecondaryNetworkList;
this.mTestNetworkList = mTestNetworkList;
}

public NetworkLists(NetworkLists networkLists) {
this.mCoreNetworks = new ArrayList<>(networkLists.mCoreNetworks);
this.mPrimaryNetworkList = new ArrayList<>(networkLists.mPrimaryNetworkList);
this.mSecondaryNetworkList = new ArrayList<>(networkLists.mSecondaryNetworkList);
this.mTestNetworkList = new ArrayList<>(networkLists.mTestNetworkList);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
import androidx.lifecycle.Transformations;

import org.chromium.brave_wallet.mojom.NetworkInfo;
import org.chromium.chrome.browser.crypto_wallet.presenters.NetworkInfoPresenter;
import org.chromium.chrome.browser.crypto_wallet.util.NetworkUtils;
import org.chromium.mojo.bindings.Callbacks;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -32,8 +30,7 @@ public class NetworkSelectorModel {
private final NetworkModel mNetworkModel;
private final MutableLiveData<NetworkInfo> _mSelectedNetwork;
private Mode mMode;
public LiveData<List<NetworkInfoPresenter>> mPrimaryNetworks;
public LiveData<List<NetworkInfoPresenter>> mSecondaryNetworks;
public LiveData<NetworkModel.NetworkLists> mNetworkListsLd;
private final LiveData<NetworkInfo> mSelectedNetwork;
private String mSelectedChainId;

Expand Down Expand Up @@ -61,27 +58,19 @@ public void init() {
_mSelectedNetwork.postValue(NetworkUtils.getAllNetworkOption(mContext));
}
}
mPrimaryNetworks = Transformations.map(mNetworkModel.mPrimaryNetworks, networkInfos -> {
List<NetworkInfoPresenter> list = new ArrayList<>();
mNetworkListsLd = Transformations.map(mNetworkModel.mNetworkLists, networkLists -> {
if (networkLists == null) return new NetworkModel.NetworkLists();
NetworkModel.NetworkLists networkListsCopy =
new NetworkModel.NetworkLists(networkLists);
List<NetworkInfo> allNetworkList = new ArrayList<>(networkLists.mCoreNetworks);
if (mMode == Mode.LOCAL_NETWORK_FILTER) {
list.add(0,
new NetworkInfoPresenter(NetworkUtils.getAllNetworkOption(mContext), true,
Collections.emptyList()));
NetworkInfo allNetwork = NetworkUtils.getAllNetworkOption(mContext);
networkListsCopy.mPrimaryNetworkList.add(0, allNetwork);
// Selected local network can be "All networks"
allNetworkList.add(0, allNetwork);
}
for (NetworkInfo networkInfo : networkInfos) {
list.add(new NetworkInfoPresenter(
networkInfo, true, mNetworkModel.getSubTestNetworks(networkInfo)));
}
updateLocalNetwork(networkInfos, mSelectedChainId);
return list;
});
mSecondaryNetworks = Transformations.map(mNetworkModel.mSecondaryNetworks, networkInfos -> {
List<NetworkInfoPresenter> list = new ArrayList<>();
for (NetworkInfo networkInfo : networkInfos) {
list.add(new NetworkInfoPresenter(networkInfo, false, Collections.emptyList()));
}
updateLocalNetwork(networkInfos, mSelectedChainId);
return list;
updateLocalNetwork(allNetworkList, mSelectedChainId);
return networkListsCopy;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void fetchAssetsByType(TokenUtils.TokenType type, NetworkInfo selectedNet
}
NetworkModel.getAllNetworks(
mJsonRpcService, mSharedData.getSupportedCryptoCoins(), allNetworks -> {
mAllNetworkInfos = new ArrayList<>(allNetworks);
mAllNetworkInfos = allNetworks;
if (selectedNetwork.chainId.equals(
NetworkUtils.getAllNetworkOption(mContext).chainId)) {
mKeyringService.getAllAccounts(allAccounts -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void update(WeakReference<BraveWalletBaseActivity> activityRef) {
mJsonRpcService, mKeyringService, mActivityRef, mActivityRef.get()))
return;
NetworkModel.getAllNetworks(mJsonRpcService, mSharedData.getSupportedCryptoCoins(), allNetworks -> {
mAllNetworkInfoList = new ArrayList<>(allNetworks);
mAllNetworkInfoList = allNetworks;
mKeyringService.getAllAccounts(allAccounts -> {
mAllAccountInfoList = Arrays.asList(allAccounts.accounts);
var allAccountsArray = allAccounts.accounts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.chromium.chrome.browser.crypto_wallet.util.TokenUtils;
import org.chromium.chrome.browser.crypto_wallet.util.WalletConstants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -59,7 +58,7 @@ public void fetchAssets(boolean nftsOnly, NetworkInfo selectedNetwork) {
if (JavaUtils.anyNull(mBraveWalletService, mJsonRpcService, mSelectedNetwork)) return;
NetworkModel.getAllNetworks(
mJsonRpcService, mSharedData.getSupportedCryptoCoins(), allNetworks -> {
mCryptoNetworks = new ArrayList<>(allNetworks);
mCryptoNetworks = allNetworks;
if (mType == WalletCoinAdapter.AdapterType.EDIT_VISIBLE_ASSETS_LIST) {
if (NetworkUtils.isAllNetwork(mSelectedNetwork)) {
fetchAllNetworksAssets(nftsOnly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ private void initState() {
networkSelectorAdapter = new NetworkSelectorAdapter(this, new ArrayList<>());
mRVNetworkSelector.setAdapter(networkSelectorAdapter);
networkSelectorAdapter.setOnNetworkItemSelected(this);
mNetworkSelectorModel.mPrimaryNetworks.observe(this, primaryNetworkInfos -> {
networkSelectorAdapter.addPrimaryNetwork(primaryNetworkInfos);
});
mNetworkSelectorModel.mSecondaryNetworks.observe(this, secondaryNetworkInfos -> {
networkSelectorAdapter.addSecondaryNetwork(secondaryNetworkInfos);
mNetworkSelectorModel.mNetworkListsLd.observe(this, networkLists -> {
if (networkLists == null) return;
networkSelectorAdapter.addNetworks(networkLists);
});
setSelectedNetworkObserver();
}
Expand Down
Loading