(Wallet) Improve null checks in NetworkModel class (#21047)

This commit is contained in:
Simone Arpe
2023-11-20 17:13:28 +01:00
committed by GitHub
parent 4f67262430
commit 40802116d2
@@ -64,7 +64,7 @@ public class NetworkModel implements JsonRpcServiceObserver {
private final MediatorLiveData<String[]> _mCustomNetworkIds;
private final MediatorLiveData<List<NetworkInfo>> _mPrimaryNetworks;
private final MediatorLiveData<List<NetworkInfo>> _mSecondaryNetworks;
private Map<String, NetworkSelectorModel> mNetworkSelectorMap;
private final Map<String, NetworkSelectorModel> mNetworkSelectorMap;
public final LiveData<String> mChainId;
private final MutableLiveData<NetworkLists> _mNetworkLists;
@@ -132,7 +132,7 @@ public class NetworkModel implements JsonRpcServiceObserver {
}
}
});
_mChainId.addSource(mSharedData.getCoinTypeLd(), coinType -> { updateChainId(); });
_mChainId.addSource(mSharedData.getCoinTypeLd(), coinType -> updateChainId());
_mDefaultCoinCryptoNetworks.addSource(mSharedData.getCoinTypeLd(), coinType -> {
getAllNetworks(mJsonRpcService, Collections.singletonList(coinType), networkInfoSet -> {
_mDefaultCoinCryptoNetworks.postValue(new ArrayList<>(networkInfoSet));
@@ -159,10 +159,11 @@ public class NetworkModel implements JsonRpcServiceObserver {
}
_mChainNetworkAllNetwork.postValue(Triple.create(chainId, networkInfo, networkInfos));
});
_mCustomNetworkIds.addSource(mSharedData.getCoinTypeLd(), coinType -> {
mJsonRpcService.getCustomNetworks(
coinType, customNetworks -> { _mCustomNetworkIds.postValue(customNetworks); });
});
_mCustomNetworkIds.addSource(
mSharedData.getCoinTypeLd(),
coinType -> {
mJsonRpcService.getCustomNetworks(coinType, _mCustomNetworkIds::postValue);
});
_mPrimaryNetworks.addSource(mCryptoNetworks, networkInfos -> {
List<NetworkInfo> primaryNws = new ArrayList<>();
for (NetworkInfo networkInfo : networkInfos) {
@@ -185,16 +186,27 @@ public class NetworkModel implements JsonRpcServiceObserver {
}
private void updateChainId() {
@CoinType.EnumType
int coin = mSharedData.getCoinTypeLd().getValue();
if (mMode == Mode.WALLET_MODE) {
if (mJsonRpcService == null) {
return;
}
Integer coinBoxed = mSharedData.getCoinTypeLd().getValue();
// Unboxing a live data value directly may
// produce a null pointer exception, so it's required
// checking against null.
if (coinBoxed == null) {
return;
}
@CoinType.EnumType int coin = coinBoxed;
mJsonRpcService.getNetwork(coin, null, networkInfo -> {
if (networkInfo != null) {
_mChainId.postValue(networkInfo.chainId);
}
});
} else if (mMode == Mode.PANEL_MODE) {
if (mBraveWalletService == null) {
return;
}
mBraveWalletService.getNetworkForSelectedAccountOnActiveOrigin(networkInfo -> {
if (networkInfo != null) {
_mChainId.postValue(networkInfo.chainId);