From 0072842855f3912f1d3a900c36f2f5ddae905695 Mon Sep 17 00:00:00 2001 From: Asif Ghanchi Date: Sat, 19 Feb 2022 15:51:22 +0530 Subject: [PATCH 1/4] Replaced WalletConnect request popup with ActionSheet. --- .../app/ui/WalletConnectActivity.java | 93 +++++++++++-------- .../app/widget/DialogInfoItem.java | 64 +++++++++++++ .../app/widget/WalletConnectInfoSheet.kt | 89 ++++++++++++++++++ .../app/widget/WalletConnectSheetCallback.kt | 7 ++ .../layout/dialog_wallet_connect_sheet.xml | 91 ++++++++++++++++++ app/src/main/res/layout/item_dialog_info.xml | 59 ++++++++++++ app/src/main/res/values/attrs.xml | 5 + 7 files changed, 370 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/com/alphawallet/app/widget/DialogInfoItem.java create mode 100644 app/src/main/java/com/alphawallet/app/widget/WalletConnectInfoSheet.kt create mode 100644 app/src/main/java/com/alphawallet/app/widget/WalletConnectSheetCallback.kt create mode 100644 app/src/main/res/layout/dialog_wallet_connect_sheet.xml create mode 100644 app/src/main/res/layout/item_dialog_info.xml diff --git a/app/src/main/java/com/alphawallet/app/ui/WalletConnectActivity.java b/app/src/main/java/com/alphawallet/app/ui/WalletConnectActivity.java index 2a428608b7..f8191fd1ea 100644 --- a/app/src/main/java/com/alphawallet/app/ui/WalletConnectActivity.java +++ b/app/src/main/java/com/alphawallet/app/ui/WalletConnectActivity.java @@ -14,7 +14,6 @@ import android.text.Spannable; import android.text.TextUtils; import android.text.format.DateUtils; -import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; @@ -59,6 +58,8 @@ import com.alphawallet.app.widget.FunctionButtonBar; import com.alphawallet.app.widget.SignTransactionDialog; import com.alphawallet.app.widget.TokenIcon; +import com.alphawallet.app.widget.WalletConnectInfoSheet; +import com.alphawallet.app.widget.WalletConnectSheetCallback; import com.alphawallet.token.entity.EthereumMessage; import com.alphawallet.token.entity.EthereumTypedMessage; import com.alphawallet.token.entity.SignMessageType; @@ -77,8 +78,6 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; -import javax.inject.Inject; - import dagger.hilt.android.AndroidEntryPoint; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; @@ -104,6 +103,7 @@ public class WalletConnectActivity extends BaseActivity implements ActionSheetCa private WCPeerMeta remotePeerMeta; private ActionSheetDialog confirmationDialog; + private WalletConnectInfoSheet walletConnectInfoSheet; private ImageView icon; private TextView peerName; @@ -658,7 +658,14 @@ private void displaySessionStatus(String sessionId) if (result.getData() == null) return; chainIdOverride = result.getData().getLongExtra(C.EXTRA_CHAIN_ID, MAINNET_ID); Toast.makeText(this, getText(R.string.hint_network_name) + " " + EthereumNetworkBase.getShortChainName(chainIdOverride), Toast.LENGTH_LONG).show(); - onSessionRequest(0L, remotePeerMeta, chainIdOverride); +// onSessionRequest(0L, remotePeerMeta, chainIdOverride); + if (walletConnectInfoSheet != null) { + walletConnectInfoSheet.onUpdateChain(chainIdOverride); + } else { + // something wrong + finish(); + } + }); private void onSessionRequest(Long id, WCPeerMeta peer, long chainId) @@ -684,41 +691,51 @@ private void onSessionRequest(Long id, WCPeerMeta peer, long chainId) chainIcon.bindData(viewModel.getTokensService().getServiceToken(chainIdOverride)); remotePeerMeta = peer; - AlertDialog.Builder builder = new AlertDialog.Builder(WalletConnectActivity.this); - AlertDialog dialog = builder - .setIcon(icon.getDrawable()) - .setTitle(peer.getName()) - .setMessage(buildMessage(peer.getUrl(), chainIdOverride)) - .setPositiveButton(R.string.dialog_approve, (d, w) -> { - client.approveSession(Arrays.asList(accounts), chainIdOverride); - //update client in service - viewModel.putClient(this, getSessionId(), client); - viewModel.createNewSession(getSessionId(), client.getPeerId(), client.getRemotePeerId(), - new Gson().toJson(session), new Gson().toJson(peer), chainIdOverride); - progressBar.setVisibility(View.GONE); - functionBar.setVisibility(View.VISIBLE); - infoLayout.setVisibility(View.VISIBLE); - setupClient(getSessionId()); - if (fromDappBrowser) - { - //switch back to dappBrowser - switchToDappBrowser(); + walletConnectInfoSheet = new WalletConnectInfoSheet(this, + displayIcon, + peer, + chainIdOverride, + new WalletConnectSheetCallback() { + @Override + public void onClickApprove(long selectedChain) { + client.approveSession(Arrays.asList(accounts), selectedChain); + //update client in service + viewModel.putClient(WalletConnectActivity.this, getSessionId(), client); + viewModel.createNewSession(getSessionId(), client.getPeerId(), client.getRemotePeerId(), + new Gson().toJson(session), new Gson().toJson(peer), selectedChain); + progressBar.setVisibility(View.GONE); + functionBar.setVisibility(View.VISIBLE); + infoLayout.setVisibility(View.VISIBLE); + setupClient(getSessionId()); + if (fromDappBrowser) + { + //switch back to dappBrowser + switchToDappBrowser(); + } } - }) - .setNeutralButton(R.string.hint_network_chain_id, (d, w) -> { - //pop open the selection dialog - Intent intent = new Intent(this, SelectNetworkActivity.class); - intent.putExtra(C.EXTRA_SINGLE_ITEM, true); - intent.putExtra(C.EXTRA_CHAIN_ID, chainIdOverride); - getNetwork.launch(intent); - }) - .setNegativeButton(R.string.dialog_reject, (d, w) -> { - client.rejectSession(getString(R.string.message_reject_request)); - finish(); - }) - .setCancelable(false) - .create(); - dialog.show(); + + @Override + public void onClickReject() { + client.rejectSession(getString(R.string.message_reject_request)); + finish(); + } + + @Override + public void onClickChainId() { + //pop open the selection dialog + Intent intent = new Intent(WalletConnectActivity.this, SelectNetworkActivity.class); + intent.putExtra(C.EXTRA_SINGLE_ITEM, true); + intent.putExtra(C.EXTRA_CHAIN_ID, chainIdOverride); + getNetwork.launch(intent); + } + } + ); + walletConnectInfoSheet.setOnDismissListener( (dialog -> { + client.rejectSession(getString(R.string.message_reject_request)); + finish(); + })); + walletConnectInfoSheet.show(); + } private Spannable buildMessage(String url, long networkId) diff --git a/app/src/main/java/com/alphawallet/app/widget/DialogInfoItem.java b/app/src/main/java/com/alphawallet/app/widget/DialogInfoItem.java new file mode 100644 index 0000000000..573476da2a --- /dev/null +++ b/app/src/main/java/com/alphawallet/app/widget/DialogInfoItem.java @@ -0,0 +1,64 @@ +package com.alphawallet.app.widget; + +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.annotation.ColorRes; +import androidx.core.content.ContextCompat; + +import com.alphawallet.app.R; + +import org.w3c.dom.Text; + +import javax.annotation.Nullable; + +public class DialogInfoItem extends LinearLayout { + + private final TextView label; + private final TextView message; + private final TextView actionText; + + public DialogInfoItem(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + inflate(context, R.layout.item_dialog_info, this); + label = findViewById(R.id.text_label); + message = findViewById(R.id.text_message); + actionText = findViewById(R.id.text_action); + getAttrs(context, attrs); + } + + private void getAttrs(Context context, AttributeSet attrs) { + TypedArray a = context.getTheme().obtainStyledAttributes(attrs, + R.styleable.DialogInfoItem, + 0, 0); + + boolean showAction = a.getBoolean(R.styleable.DialogInfoItem_showActionText, false); + setLabel(a.getString(R.styleable.DialogInfoItem_title)); + setMessage(a.getString(R.styleable.DialogInfoItem_text)); + actionText.setVisibility( showAction ? VISIBLE : GONE); + } + + public void setLabel(String label) { + this.label.setText(label); + } + + public void setMessage(String msg) { + this.message.setText(msg); + } + + public void setMessageTextColor(@ColorRes int color) { + this.message.setTextColor(ContextCompat.getColor(getContext(), color)); + } + + public void setActionText(String text) { + this.actionText.setText(text); + } + + public void setActionListener(View.OnClickListener listener) { + actionText.setOnClickListener(listener); + } +} diff --git a/app/src/main/java/com/alphawallet/app/widget/WalletConnectInfoSheet.kt b/app/src/main/java/com/alphawallet/app/widget/WalletConnectInfoSheet.kt new file mode 100644 index 0000000000..5337628a1c --- /dev/null +++ b/app/src/main/java/com/alphawallet/app/widget/WalletConnectInfoSheet.kt @@ -0,0 +1,89 @@ +package com.alphawallet.app.widget + +import android.app.Activity +import android.text.Spannable +import android.view.View +import android.widget.Button +import android.widget.ImageView +import android.widget.TextView +import com.alphawallet.app.R +import com.alphawallet.app.entity.StandardFunctionInterface +import com.alphawallet.app.repository.EthereumNetworkBase +import com.alphawallet.app.walletconnect.entity.WCPeerMeta +import com.bumptech.glide.Glide +import com.google.android.material.bottomsheet.BottomSheetBehavior +import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED +import com.google.android.material.bottomsheet.BottomSheetDialog +import timber.log.Timber +import java.util.* + +class WalletConnectInfoSheet( + private val activity: Activity, + private val iconUrl: String?, + private val peer: WCPeerMeta, + private var chainIdOverride: Long, + private val hostListener: WalletConnectSheetCallback, + ) : BottomSheetDialog(activity, R.style.FullscreenBottomSheetDialogStyle), StandardFunctionInterface { + + init { + val view: View = View.inflate(context, R.layout.dialog_wallet_connect_sheet, null) + setContentView(view) + val behaviour: BottomSheetBehavior = BottomSheetBehavior.from(view.parent as View) + behaviour.state = STATE_EXPANDED + behaviour.skipCollapsed = true + + view.findViewById(R.id.logo)?.apply { + Glide.with(activity) + .load(iconUrl) + .circleCrop() + .into(this) + } + view.findViewById(R.id.text_title)?.apply { text = peer.name } + view.findViewById(R.id.image_close)?.apply { setOnClickListener { hostListener.onClickReject() } } + view.findViewById(R.id.info_website)?.apply { + setLabel("Website") + setMessage(peer.url) + } + view.findViewById(R.id.info_network)?.apply { + setLabel("Network") + setMessage(EthereumNetworkBase.getShortChainName(chainIdOverride)) + setMessageTextColor(EthereumNetworkBase.getChainColour(chainIdOverride)) + setActionText(context.getString(R.string.edit)) + setActionListener { + hostListener.onClickChainId() + } + } + Timber.d("Peer url: %s\nPeer: %s",peer.url, peer.description) + + view.findViewById