Skip to content

Commit

Permalink
Merge pull request #12213 from brave/pr12204_android_evm_fixes_1.36.x
Browse files Browse the repository at this point in the history
Various fixes for Android EVM networks feature (uplift to 1.36.x)
  • Loading branch information
kjozwiak authored Feb 14, 2022
2 parents 68714ab + 9805b24 commit a2c4042
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,12 @@ public void finishNativeInitialization() {
Spinner spinner = findViewById(R.id.network_spinner);
spinner.setOnItemSelectedListener(this);
mJsonRpcService.getAllNetworks(chains -> {
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
EthereumChain[] customNetworks = new EthereumChain[0];
// We want to hide custom networks for BUY and SWAP screens. We are
// going to add a support for SWAP at least in the near future.
if (mActivityType == ActivityType.SEND) {
customNetworks = Utils.getCustomNetworks(chains);
}
// Creating adapter for spinner
NetworkSpinnerAdapter dataAdapter = new NetworkSpinnerAdapter(this,
Utils.getNetworksList(this, customNetworks),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface Launcher {
/**
* Launches BraveWalletAddNetworksFragment.
*/
void launchAddNetwork(String chainId);
void launchAddNetwork(String chainId, boolean activeNetwork);
void setRefresher(Refresher refresher);
}

Expand All @@ -64,12 +64,17 @@ public View onCreateView(
final View view = inflater.inflate(R.layout.brave_wallet_add_network, container, false);

Button btAdd = view.findViewById(R.id.add);
btAdd.setOnClickListener(v -> {
if (!validateInputsAddChain(view)) {
// We have some errors in inputs
return;
}
});
boolean activeNetwork = getActivity().getIntent().getBooleanExtra("activeNetwork", false);
if (!activeNetwork) {
btAdd.setOnClickListener(v -> {
if (!validateInputsAddChain(view)) {
// We have some errors in inputs
return;
}
});
} else {
btAdd.setVisibility(View.GONE);
}
mChainId = getActivity().getIntent().getStringExtra("chainId");
if (!mChainId.isEmpty()) {
btAdd.setText(R.string.brave_wallet_add_network_submit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onBindViewHolder(PreferenceViewHolder holder) {
TintedDrawable.constructTintedDrawable(getContext(), R.drawable.plus,
R.color.default_control_color_active_baseline),
null, null, null);
mbtAddNetwork.setOnClickListener(view -> { mLauncher.launchAddNetwork(""); });
mbtAddNetwork.setOnClickListener(view -> { mLauncher.launchAddNetwork("", false); });
mLauncher.setRefresher(this);

InitJsonRpcService();
Expand All @@ -71,8 +71,8 @@ public void onConnectionError(MojoException e) {
}

@Override
public void onItemClicked(EthereumChain chain) {
mLauncher.launchAddNetwork(chain.chainId);
public void onItemClicked(EthereumChain chain, boolean activeNetwork) {
mLauncher.launchAddNetwork(chain.chainId, activeNetwork);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ public void setSettingsLauncher(SettingsLauncher settingsLauncher) {
}

@Override
public void launchAddNetwork(String chainId) {
public void launchAddNetwork(String chainId, boolean activeNetwork) {
Intent intent = mSettingsLauncher.createSettingsActivityIntent(
getActivity(), BraveWalletAddNetworksFragment.class.getName());
intent.putExtra("chainId", chainId);
intent.putExtra("activeNetwork", activeNetwork);
startActivityForResult(intent, REQUEST_CODE_ADD_NETWORK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
public class NetworkListBaseAdapter extends RecyclerView.Adapter<ViewHolder> {
interface ItemClickListener {
void onItemClicked(EthereumChain chain);
void onItemClicked(EthereumChain chain, boolean activeNetwork);
void onItemRemove(EthereumChain chain);
void onItemSetAsActive(EthereumChain chain);
}
Expand Down Expand Up @@ -68,7 +68,7 @@ static class RowViewHolder extends ViewHolder {
mListener = listener;
}

protected void updateNetworkInfo(EthereumChain item) {
void updateNetworkInfo(EthereumChain item, boolean activeNetwork) {
mTitle.setText(item.chainName);
String description = item.chainId;
if (item.rpcUrls.length > 0) {
Expand All @@ -80,7 +80,7 @@ protected void updateNetworkInfo(EthereumChain item) {

// The more button will become visible if setMenuButtonDelegate is called.
mMoreButton.setVisibility(View.GONE);
mItem.setOnClickListener(view -> mListener.onItemClicked(item));
mItem.setOnClickListener(view -> mListener.onItemClicked(item, activeNetwork));
}

/**
Expand Down Expand Up @@ -112,18 +112,22 @@ public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
@SuppressLint("NotifyDataSetChanged")
public void onBindViewHolder(ViewHolder viewHolder, int i) {
final EthereumChain info = mElements.get(i);
((RowViewHolder) viewHolder).updateNetworkInfo(info);
boolean activeNetwork = info.chainId.equals(mActiveChainId);
((RowViewHolder) viewHolder).updateNetworkInfo(info, activeNetwork);

if (activeNetwork) {
return;
}

ModelList menuItems = new ModelList();
menuItems.add(buildMenuListItem(R.string.edit, 0, 0));
if (!info.chainId.equals(mActiveChainId)) {
menuItems.add(buildMenuListItem(R.string.remove, 0, 0));
menuItems.add(buildMenuListItem(R.string.brave_wallet_add_network_set_as_active, 0, 0));
}
menuItems.add(buildMenuListItem(R.string.remove, 0, 0));
menuItems.add(buildMenuListItem(R.string.brave_wallet_add_network_set_as_active, 0, 0));

ListMenu.Delegate delegate = (model) -> {
int textId = model.get(ListMenuItemProperties.TITLE_ID);
if (textId == R.string.edit) {
mListener.onItemClicked(info);
mListener.onItemClicked(info, activeNetwork);
} else if (textId == R.string.remove) {
mListener.onItemRemove(info);
} else if (textId == R.string.brave_wallet_add_network_set_as_active) {
Expand Down
Loading

0 comments on commit a2c4042

Please sign in to comment.