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

Always show buy send swap action #18343

Merged
merged 2 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -11,7 +11,6 @@
import static org.chromium.chrome.browser.crypto_wallet.util.Utils.UNLOCK_WALLET_ACTION;

import android.os.Build;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -22,14 +21,15 @@
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import org.chromium.base.ActivityState;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.NetworkInfo;
import org.chromium.brave_wallet.mojom.OnboardingAction;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.BraveActivity;
Expand Down Expand Up @@ -71,7 +71,7 @@ public class BraveWalletActivity extends BraveWalletBaseActivity implements OnNe
private View mCryptoOnboardingLayout;
private View cryptoOnboardingLayout;
private ImageView mPendingTxNotification;
private ImageView mSwapButton;
private ImageView mBuySendSwapButton;
private ViewPager cryptoWalletOnboardingViewPager;
private CryptoFragmentPageAdapter mCryptoFragmentPageAdapter;
private ModalDialogManager mModalDialogManager;
Expand Down Expand Up @@ -125,12 +125,12 @@ protected void triggerLayoutInflation() {
ContextCompat.getDrawable(this, R.drawable.ic_baseline_more_vert_24));
setSupportActionBar(mToolbar);

mSwapButton = findViewById(R.id.swap_button);
mBuySendSwapButton = findViewById(R.id.buy_send_swap_button);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// For Android 7 and above use vector images for send/swap button.
// For Android 5 and 6 it is a bitmap specified in activity_brave_wallet.xml.
mSwapButton.setImageResource(R.drawable.ic_swap_icon);
mSwapButton.setBackgroundResource(R.drawable.ic_swap_bg);
mBuySendSwapButton.setImageResource(R.drawable.ic_swap_icon);
mBuySendSwapButton.setBackgroundResource(R.drawable.ic_swap_bg);
}

try {
Expand All @@ -140,21 +140,18 @@ protected void triggerLayoutInflation() {
Log.e(TAG, "triggerLayoutInflation " + e);
}

mSwapButton.setOnClickListener(v -> {
assert mJsonRpcService != null;
LiveDataUtil.observeOnce(
mWalletModel.getCryptoModel().getNetworkModel().mDefaultNetwork,
defaultNetwork -> {
LiveDataUtil.observeOnce(
mWalletModel.getCryptoModel().mIsSwapEnabled, isSwapSupported -> {
SwapBottomSheetDialogFragment swapBottomSheetDialogFragment =
SwapBottomSheetDialogFragment.newInstance(
isSwapSupported);
swapBottomSheetDialogFragment.setNetwork(defaultNetwork);
swapBottomSheetDialogFragment.show(getSupportFragmentManager(),
SwapBottomSheetDialogFragment.TAG_FRAGMENT);
});
});
mBuySendSwapButton.setOnClickListener(v -> {
NetworkInfo ethNetwork = null;
// Always show buy send swap with ETH
if (mWalletModel != null) {
ethNetwork = mWalletModel.getNetworkModel().getNetwork(
BraveWalletConstants.MAINNET_CHAIN_ID);
}
SwapBottomSheetDialogFragment swapBottomSheetDialogFragment =
SwapBottomSheetDialogFragment.newInstance();
swapBottomSheetDialogFragment.setNetwork(ethNetwork);
SergeyZhukovsky marked this conversation as resolved.
Show resolved Hide resolved
swapBottomSheetDialogFragment.show(
getSupportFragmentManager(), SwapBottomSheetDialogFragment.TAG_FRAGMENT);
});

mPendingTxNotification = findViewById(R.id.pending_tx_notification);
Expand Down Expand Up @@ -250,7 +247,7 @@ private void setNavigationFragments(int type) {
mShowBiometricPrompt = true;
mCryptoLayout.setVisibility(View.GONE);
mPendingTxNotification.setVisibility(View.GONE);
mSwapButton.setVisibility(View.GONE);
mBuySendSwapButton.setVisibility(View.GONE);
mCryptoOnboardingLayout.setVisibility(View.VISIBLE);
if (type == ONBOARDING_FIRST_PAGE_ACTION) {
SetupWalletFragment setupWalletFragment =
Expand Down Expand Up @@ -343,7 +340,7 @@ private void setCryptoLayout() {
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

if (mSwapButton != null) mSwapButton.setVisibility(View.VISIBLE);
if (mBuySendSwapButton != null) mBuySendSwapButton.setVisibility(View.VISIBLE);

if (mKeyringService != null)
mKeyringService.isWalletBackedUp(backed_up -> {
Expand Down Expand Up @@ -377,7 +374,7 @@ public void backupBannerOnClick() {
mCryptoOnboardingLayout.setVisibility(View.VISIBLE);
mCryptoLayout.setVisibility(View.GONE);
mPendingTxNotification.setVisibility(View.GONE);
mSwapButton.setVisibility(View.GONE);
mBuySendSwapButton.setVisibility(View.GONE);

List<NavigationItem> navigationItems = new ArrayList<>();
addBackupWalletSequence(navigationItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,33 @@
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.NetworkInfo;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.app.domain.WalletModel;
import org.chromium.chrome.browser.crypto_wallet.activities.BuySendSwapActivity;
import org.chromium.chrome.browser.crypto_wallet.util.JavaUtils;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.chrome.browser.crypto_wallet.util.WalletConstants;

public class SwapBottomSheetDialogFragment
extends BottomSheetDialogFragment implements View.OnClickListener {
public static final String TAG_FRAGMENT = SwapBottomSheetDialogFragment.class.getName();
private static final String TAG = "BSS-bottom-dialog";
private LinearLayout mBuyLayout;
private LinearLayout mSendLayout;
private LinearLayout mSwapLayout;
private NetworkInfo mNetworkInfo;
private boolean mIsSwapSupported;
private boolean mIsCustomNetwork;
private WalletModel mWalletModel;

public SwapBottomSheetDialogFragment(boolean isSwapSupported) {
mIsSwapSupported = isSwapSupported;
}

public static SwapBottomSheetDialogFragment newInstance(boolean isSwapSupported) {
return new SwapBottomSheetDialogFragment(isSwapSupported);
}

public void setIsCustomNetwork(boolean isCustomNetwork) {
mIsCustomNetwork = isCustomNetwork;
public static SwapBottomSheetDialogFragment newInstance() {
return new SwapBottomSheetDialogFragment();
}

public void setNetwork(NetworkInfo networkInfo) {
mNetworkInfo = networkInfo;
updateViewState();
}

@Override
Expand All @@ -73,6 +68,17 @@ public void show(FragmentManager manager, String tag) {
}
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mWalletModel = BraveActivity.getBraveActivity().getWalletModel();
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "onCreate ", e);
e.printStackTrace();
}
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
Expand All @@ -89,27 +95,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
return view;
}

@Override
public void onStart() {
super.onStart();
updateViewState();
}

private void updateViewState() {
if (getView() == null) return;
if (!mIsCustomNetwork
&& (mNetworkInfo.coin == CoinType.ETH || mNetworkInfo.coin == CoinType.SOL)
&& Utils.allowSwap(mNetworkInfo.chainId)) {
mSwapLayout.setVisibility(View.VISIBLE);
} else {
mSwapLayout.setVisibility(View.GONE);
}

if (!Utils.allowBuy(mNetworkInfo.chainId)) {
mBuyLayout.setVisibility(View.GONE);
}
}

@Override
public void onClick(View view) {
BuySendSwapActivity.ActivityType activityType = BuySendSwapActivity.ActivityType.BUY;
Expand All @@ -118,6 +103,18 @@ public void onClick(View view) {
} else if (view == mSwapLayout) {
activityType = BuySendSwapActivity.ActivityType.SWAP;
}

if (!JavaUtils.anyNull(mWalletModel, mNetworkInfo)) {
BuySendSwapActivity.ActivityType finalActivityType = activityType;
mWalletModel.getNetworkModel().setNetwork(
SergeyZhukovsky marked this conversation as resolved.
Show resolved Hide resolved
mNetworkInfo, isSelected -> { openBssAndDismiss(finalActivityType); });
return;
}
openBssAndDismiss(activityType);
}

// Open buy send swap (BSS)
private void openBssAndDismiss(BuySendSwapActivity.ActivityType activityType) {
Utils.openBuySendSwapActivity(getActivity(), activityType);
dismiss();
}
Expand Down
4 changes: 2 additions & 2 deletions android/java/res/layout/activity_brave_wallet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
android:visibility="visible"/>

<ImageView
android:id="@+id/swap_button"
android:id="@+id/buy_send_swap_button"
android:layout_width="56dp"
android:layout_height="56dp"
android:contentDescription="@null"
Expand All @@ -89,7 +89,7 @@
<!-- On Android 5 and 6 crash happens when vectored image contains gradient.
So here raster image is specified by default, but for Android 7 and above
vector image is used, see BraveWalletActivity.triggerLayoutInflation()
for swap_button.
for buy_send_swap_button.
-->
</LinearLayout>
</FrameLayout>