Skip to content

Commit

Permalink
(Wallet) Improve null checks in NetworkModel class (uplift to 1.61.x) (
Browse files Browse the repository at this point in the history
…#21052)

Uplift of #21047 (squashed) to beta
  • Loading branch information
brave-builds authored Nov 20, 2023
1 parent c5d19b3 commit 38950d8
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,7 +132,7 @@ public NetworkModel(BraveWalletService braveWalletService, JsonRpcService jsonRp
}
}
});
_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));
Expand All @@ -159,10 +159,11 @@ public NetworkModel(BraveWalletService braveWalletService, JsonRpcService jsonRp
}
_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) {
Expand All @@ -185,16 +186,27 @@ public NetworkModel(BraveWalletService braveWalletService, JsonRpcService jsonRp
}

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);
Expand Down

0 comments on commit 38950d8

Please sign in to comment.