Skip to content

Commit

Permalink
Merge pull request #9882 from brave/buy_send_lists
Browse files Browse the repository at this point in the history
Adds list for buy/send on Android
  • Loading branch information
SergeyZhukovsky authored Aug 27, 2021
2 parents d8ced6a + 42bdcfe commit d58b8df
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void onPageScrollStateChanged(int state) {}
@Override
public void onConnectionError(MojoException e) {
mKeyringController = null;
mErcTokenRegistry = null;
InitKeyringController();
InitErcTokenRegistry();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@

import androidx.appcompat.widget.Toolbar;

import org.chromium.brave_wallet.mojom.ErcTokenRegistry;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.crypto_wallet.ERCTokenRegistryFactory;
import org.chromium.chrome.browser.crypto_wallet.adapters.WalletCoinAdapter;
import org.chromium.chrome.browser.crypto_wallet.fragments.EditVisibleAssetsBottomSheetDialogFragment;
import org.chromium.chrome.browser.init.AsyncInitializationActivity;
import org.chromium.mojo.bindings.ConnectionErrorHandler;
import org.chromium.mojo.system.MojoException;

import java.util.HashMap;
import java.util.Map;

public class BuySendSwapActivity extends AsyncInitializationActivity {
public class BuySendSwapActivity
extends AsyncInitializationActivity implements ConnectionErrorHandler {
public enum ActivityType {
BUY(0),
SEND(1),
Expand Down Expand Up @@ -52,6 +59,7 @@ public int getValue() {
}
}

private ErcTokenRegistry mErcTokenRegistry;
private ActivityType mActivityType;

@Override
Expand Down Expand Up @@ -105,6 +113,7 @@ protected void triggerLayoutInflation() {
onInitialLayoutInflationComplete();

adjustControls();
InitErcTokenRegistry();
}

private void adjustControls() {
Expand All @@ -117,6 +126,7 @@ private void adjustControls() {
Button btnBuySendSwap = findViewById(R.id.btn_buy_send_swap);
TextView currencySign = findViewById(R.id.currency_sign);
TextView toEstimateText = findViewById(R.id.to_estimate_text);
TextView assetDropDown = findViewById(R.id.from_asset_text);
if (mActivityType == ActivityType.BUY) {
TextView fromBuyText = findViewById(R.id.from_buy_text);
fromBuyText.setText(getText(R.string.buy_wallet));
Expand All @@ -130,6 +140,13 @@ private void adjustControls() {
btnBuySendSwap.setText(getText(R.string.buy_wallet));
RadioGroup radioPerPercent = findViewById(R.id.per_percent_radiogroup);
radioPerPercent.setVisibility(View.GONE);
assetDropDown.setOnClickListener(v -> {
EditVisibleAssetsBottomSheetDialogFragment bottomSheetDialogFragment =
EditVisibleAssetsBottomSheetDialogFragment.newInstance(
WalletCoinAdapter.AdapterType.BUY_ASSETS_LIST);
bottomSheetDialogFragment.show(getSupportFragmentManager(),
EditVisibleAssetsBottomSheetDialogFragment.TAG_FRAGMENT);
});
} else if (mActivityType == ActivityType.SEND) {
currencySign.setVisibility(View.GONE);
toEstimateText.setText(getText(R.string.to_address));
Expand All @@ -141,6 +158,13 @@ private void adjustControls() {
btnBuySendSwap.setText(getText(R.string.send));
LinearLayout toBalanceSection = findViewById(R.id.to_balance_section);
toBalanceSection.setVisibility(View.GONE);
assetDropDown.setOnClickListener(v -> {
EditVisibleAssetsBottomSheetDialogFragment bottomSheetDialogFragment =
EditVisibleAssetsBottomSheetDialogFragment.newInstance(
WalletCoinAdapter.AdapterType.SEND_ASSETS_LIST);
bottomSheetDialogFragment.show(getSupportFragmentManager(),
EditVisibleAssetsBottomSheetDialogFragment.TAG_FRAGMENT);
});
} else if (mActivityType == ActivityType.SWAP) {
currencySign.setVisibility(View.GONE);
toValueText.setText("");
Expand Down Expand Up @@ -180,6 +204,29 @@ public void onClick(View v) {
}
}

public void updateBuySendAsset(String asset) {
TextView assetDropDown = findViewById(R.id.from_asset_text);
assetDropDown.setText(asset);
}

public ErcTokenRegistry getErcTokenRegistry() {
return mErcTokenRegistry;
}

@Override
public void onConnectionError(MojoException e) {
mErcTokenRegistry = null;
InitErcTokenRegistry();
}

private void InitErcTokenRegistry() {
if (mErcTokenRegistry != null) {
return;
}

mErcTokenRegistry = ERCTokenRegistryFactory.getInstance().getERCTokenRegistry(this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
Expand All @@ -31,7 +32,9 @@ public class WalletCoinAdapter extends RecyclerView.Adapter<WalletCoinAdapter.Vi
public enum AdapterType {
VISIBLE_ASSETS_LIST,
EDIT_VISIBLE_ASSETS_LIST,
ACCOUNTS_LIST;
ACCOUNTS_LIST,
BUY_ASSETS_LIST,
SEND_ASSETS_LIST;
}

private Context context;
Expand Down Expand Up @@ -75,34 +78,50 @@ public void onBindViewHolder(@NonNull WalletCoinAdapter.ViewHolder holder, int p
if (walletListItemType == Utils.TRANSACTION_ITEM) {
onWalletListItemClick.onTransactionClick();
} else if (walletListItemType == Utils.ASSET_ITEM) {
onWalletListItemClick.onAssetClick();
if (mType == AdapterType.BUY_ASSETS_LIST || mType == AdapterType.SEND_ASSETS_LIST) {
for (int i = 0; i < walletListItemModelListCopy.size(); i++) {
WalletListItemModel item = walletListItemModelListCopy.get(i);
if (item.getTitle().contains(holder.titleText.getText())
|| item.getSubTitle().contains(holder.subTitleText.getText())) {
mCheckedPositions.add((Integer) i);
break;
}
}
}
if (mType != AdapterType.EDIT_VISIBLE_ASSETS_LIST) {
onWalletListItemClick.onAssetClick();
}
} else {
onWalletListItemClick.onAccountClick();
}
});
if (mType == AdapterType.EDIT_VISIBLE_ASSETS_LIST) {
if (mType == AdapterType.EDIT_VISIBLE_ASSETS_LIST || mType == AdapterType.BUY_ASSETS_LIST
|| mType == AdapterType.SEND_ASSETS_LIST) {
holder.text1Text.setVisibility(View.GONE);
holder.text2Text.setVisibility(View.GONE);
holder.assetCheck.setVisibility(View.VISIBLE);
holder.assetCheck.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i <= walletListItemModelListCopy.size(); i++) {
WalletListItemModel item = walletListItemModelListCopy.get(i);
if (item.getTitle().contains(holder.titleText.getText())
|| item.getSubTitle().contains(
holder.subTitleText.getText())) {
if (isChecked) {
mCheckedPositions.add((Integer) i);
} else {
mCheckedPositions.remove((Integer) i);
if (mType == AdapterType.EDIT_VISIBLE_ASSETS_LIST) {
holder.assetCheck.setVisibility(View.VISIBLE);
holder.assetCheck.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i < walletListItemModelListCopy.size(); i++) {
WalletListItemModel item = walletListItemModelListCopy.get(i);
if (item.getTitle().contains(holder.titleText.getText())
|| item.getSubTitle().contains(
holder.subTitleText.getText())) {
if (isChecked) {
mCheckedPositions.add((Integer) i);
} else {
mCheckedPositions.remove((Integer) i);
}
break;
}
break;
}
}
}
});
});
}
}
}

Expand All @@ -113,7 +132,8 @@ public int getItemCount() {

public void setWalletListItemModelList(List<WalletListItemModel> walletListItemModelList) {
this.walletListItemModelList = walletListItemModelList;
if (mType == AdapterType.EDIT_VISIBLE_ASSETS_LIST) {
if (mType == AdapterType.EDIT_VISIBLE_ASSETS_LIST || mType == AdapterType.BUY_ASSETS_LIST
|| mType == AdapterType.SEND_ASSETS_LIST) {
walletListItemModelListCopy.addAll(walletListItemModelList);
mCheckedPositions.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.chromium.brave_wallet.mojom.ErcTokenRegistry;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.crypto_wallet.activities.BraveWalletActivity;
import org.chromium.chrome.browser.crypto_wallet.activities.BuySendSwapActivity;
import org.chromium.chrome.browser.crypto_wallet.adapters.WalletCoinAdapter;
import org.chromium.chrome.browser.crypto_wallet.listeners.OnWalletListItemClick;
import org.chromium.chrome.browser.crypto_wallet.model.WalletListItemModel;
Expand All @@ -49,15 +50,23 @@ public class EditVisibleAssetsBottomSheetDialogFragment
public static final String TAG_FRAGMENT =
EditVisibleAssetsBottomSheetDialogFragment.class.getName();
private WalletCoinAdapter walletCoinAdapter;
private WalletCoinAdapter.AdapterType mType;

public static EditVisibleAssetsBottomSheetDialogFragment newInstance() {
return new EditVisibleAssetsBottomSheetDialogFragment();
public static EditVisibleAssetsBottomSheetDialogFragment newInstance(
WalletCoinAdapter.AdapterType type) {
return new EditVisibleAssetsBottomSheetDialogFragment(type);
}

private EditVisibleAssetsBottomSheetDialogFragment(WalletCoinAdapter.AdapterType type) {
mType = type;
}

private ErcTokenRegistry getErcTokenRegistry() {
Activity activity = getActivity();
if (activity instanceof BraveWalletActivity) {
return ((BraveWalletActivity) activity).getErcTokenRegistry();
} else if (activity instanceof BuySendSwapActivity) {
return ((BuySendSwapActivity) activity).getErcTokenRegistry();
}

return null;
Expand Down Expand Up @@ -126,51 +135,34 @@ public void setupDialog(@NonNull Dialog dialog, int style) {
ViewParent parent = view.getParent();
((View) parent).getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
Button saveAssets = view.findViewById(R.id.saveAssets);
saveAssets.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View clickView) {
if (walletCoinAdapter != null) {
// TODO(sergz): Save selected assets walletCoinAdapter.getCheckedAssets()
if (mType == WalletCoinAdapter.AdapterType.EDIT_VISIBLE_ASSETS_LIST) {
saveAssets.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View clickView) {
if (walletCoinAdapter != null) {
// TODO(sergz): Save selected assets walletCoinAdapter.getCheckedAssets()
}
dismiss();
}
dismiss();
}
});
});
} else {
saveAssets.setVisibility(View.GONE);
}

ErcTokenRegistry ercTokenRegistry = getErcTokenRegistry();
if (ercTokenRegistry != null) {
ercTokenRegistry.getAllTokens(tokens -> {
setUpAssetsList(view, tokens);
SearchView searchView = (SearchView) view.findViewById(R.id.searchView);
searchView.setQueryHint(getText(R.string.search_tokens));
searchView.setIconified(false);
searchView.clearFocus();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (walletCoinAdapter != null) {
walletCoinAdapter.filter(query);
}

return true;
}

@Override
public boolean onQueryTextChange(String newText) {
if (walletCoinAdapter != null) {
walletCoinAdapter.filter(newText);
}

return true;
}
});
});
if (mType == WalletCoinAdapter.AdapterType.EDIT_VISIBLE_ASSETS_LIST
|| mType == WalletCoinAdapter.AdapterType.SEND_ASSETS_LIST) {
ercTokenRegistry.getAllTokens(tokens -> { setUpAssetsList(view, tokens); });
} else if (mType == WalletCoinAdapter.AdapterType.BUY_ASSETS_LIST) {
ercTokenRegistry.getBuyTokens(tokens -> { setUpAssetsList(view, tokens); });
}
}
}

private void setUpAssetsList(View view, ErcToken[] tokens) {
RecyclerView rvAssets = view.findViewById(R.id.rvAssets);
walletCoinAdapter =
new WalletCoinAdapter(WalletCoinAdapter.AdapterType.EDIT_VISIBLE_ASSETS_LIST);
walletCoinAdapter = new WalletCoinAdapter(mType);
List<WalletListItemModel> walletListItemModelList = new ArrayList<>();
for (int i = 0; i < tokens.length; i++) {
walletListItemModelList.add(new WalletListItemModel(
Expand All @@ -181,6 +173,29 @@ private void setUpAssetsList(View view, ErcToken[] tokens) {
walletCoinAdapter.setWalletListItemType(Utils.ASSET_ITEM);
rvAssets.setAdapter(walletCoinAdapter);
rvAssets.setLayoutManager(new LinearLayoutManager(getActivity()));
SearchView searchView = (SearchView) view.findViewById(R.id.searchView);
searchView.setQueryHint(getText(R.string.search_tokens));
searchView.setIconified(false);
searchView.clearFocus();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (walletCoinAdapter != null) {
walletCoinAdapter.filter(query);
}

return true;
}

@Override
public boolean onQueryTextChange(String newText) {
if (walletCoinAdapter != null) {
walletCoinAdapter.filter(newText);
}

return true;
}
});
}

@Override
Expand All @@ -189,5 +204,16 @@ public void onClick(View view) {
}

@Override
public void onAssetClick() {}
public void onAssetClick() {
if (mType == WalletCoinAdapter.AdapterType.SEND_ASSETS_LIST
|| mType == WalletCoinAdapter.AdapterType.BUY_ASSETS_LIST) {
List<WalletListItemModel> checkedAssets = walletCoinAdapter.getCheckedAssets();
Activity activity = getActivity();
if (activity instanceof BuySendSwapActivity && checkedAssets.size() > 0) {
((BuySendSwapActivity) activity)
.updateBuySendAsset(checkedAssets.get(0).getSubTitle());
}
}
dismiss();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
Button editVisibleAssets = view.findViewById(R.id.edit_visible_assets);
editVisibleAssets.setOnClickListener(v -> {
EditVisibleAssetsBottomSheetDialogFragment bottomSheetDialogFragment =
EditVisibleAssetsBottomSheetDialogFragment.newInstance();
EditVisibleAssetsBottomSheetDialogFragment.newInstance(
WalletCoinAdapter.AdapterType.EDIT_VISIBLE_ASSETS_LIST);
bottomSheetDialogFragment.show(
getFragmentManager(), EditVisibleAssetsBottomSheetDialogFragment.TAG_FRAGMENT);
});
Expand Down
Loading

0 comments on commit d58b8df

Please sign in to comment.