working on the send dialog

still need to add a button to scan QR code
This commit is contained in:
pokkst 2022-09-12 11:54:45 -05:00
parent 3c4b1de1df
commit fcadb39b76
No known key found for this signature in database
GPG Key ID: 90C2ED85E67A50FF
11 changed files with 263 additions and 52 deletions

View File

@ -1,8 +1,11 @@
package com.m2049r.xmrwallet.fragment.dialog;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.m2049r.xmrwallet.R;
import com.m2049r.xmrwallet.model.PendingTransaction;
import com.m2049r.xmrwallet.model.Wallet;
import com.m2049r.xmrwallet.service.BalanceService;
import com.m2049r.xmrwallet.service.TxService;
@ -25,6 +28,19 @@ import androidx.lifecycle.MutableLiveData;
public class SendBottomSheetDialog extends BottomSheetDialogFragment {
private MutableLiveData<Boolean> _sendingMax = new MutableLiveData<>(false);
public LiveData<Boolean> sendingMax = _sendingMax;
private MutableLiveData<PendingTransaction> _pendingTransaction = new MutableLiveData<>(null);
public LiveData<PendingTransaction> pendingTransaction = _pendingTransaction;
private EditText addressEditText;
private EditText amountEditText;
private TextView sendAllTextView;
private TextView feeTextView;
private TextView addressTextView;
private TextView amountTextView;
private Button createButton;
private Button sendButton;
private Button sendMaxButton;
private ImageButton pasteAddressImageButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -34,12 +50,16 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageButton pasteAddressImageButton = view.findViewById(R.id.paste_address_imagebutton);
Button sendMaxButton = view.findViewById(R.id.send_max_button);
EditText addressEditText = view.findViewById(R.id.address_edittext);
EditText amountEditText = view.findViewById(R.id.amount_edittext);
Button sendButton = view.findViewById(R.id.send_button);
TextView sendAllTextView = view.findViewById(R.id.sending_all_textview);
pasteAddressImageButton = view.findViewById(R.id.paste_address_imagebutton);
sendMaxButton = view.findViewById(R.id.send_max_button);
addressEditText = view.findViewById(R.id.address_edittext);
amountEditText = view.findViewById(R.id.amount_edittext);
sendButton = view.findViewById(R.id.send_tx_button);
createButton = view.findViewById(R.id.create_tx_button);
sendAllTextView = view.findViewById(R.id.sending_all_textview);
feeTextView = view.findViewById(R.id.fee_textview);
addressTextView = view.findViewById(R.id.address_pending_textview);
amountTextView = view.findViewById(R.id.amount_pending_textview);
pasteAddressImageButton.setOnClickListener(view1 -> {
addressEditText.setText(Helper.getClipBoardText(view.getContext()));
@ -50,7 +70,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
_sendingMax.postValue(!currentValue);
});
sendButton.setOnClickListener(view1 -> {
createButton.setOnClickListener(view1 -> {
boolean sendAll = sendingMax.getValue() != null ? sendingMax.getValue() : false;
String address = addressEditText.getText().toString().trim();
String amount = amountEditText.getText().toString().trim();
@ -62,13 +82,9 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
Toast.makeText(getActivity(), getString(R.string.send_amount_invalid), Toast.LENGTH_SHORT).show();
return;
}
sendButton.setEnabled(false);
boolean success = TxService.getInstance().sendTx(address, amount, sendAll);
if(success) {
dismiss();
} else {
Toast.makeText(getActivity(), getString(R.string.error_sending_tx), Toast.LENGTH_SHORT).show();
}
Toast.makeText(getActivity(), getString(R.string.creating_tx), Toast.LENGTH_SHORT).show();
createButton.setEnabled(false);
createTx(address, amount, sendAll);
} else if (!validAddress) {
Toast.makeText(getActivity(), getString(R.string.send_address_invalid), Toast.LENGTH_SHORT).show();
} else {
@ -76,16 +92,99 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
}
});
sendButton.setOnClickListener(view1 -> {
PendingTransaction pendingTx = pendingTransaction.getValue();
if(pendingTx != null) {
Toast.makeText(getActivity(), getString(R.string.sending_tx), Toast.LENGTH_SHORT).show();
sendButton.setEnabled(false);
sendTx(pendingTx);
}
});
sendingMax.observe(getViewLifecycleOwner(), sendingMax -> {
if(sendingMax) {
amountEditText.setVisibility(View.INVISIBLE);
sendAllTextView.setVisibility(View.VISIBLE);
sendMaxButton.setText(getText(R.string.undo));
} else {
amountEditText.setVisibility(View.VISIBLE);
sendAllTextView.setVisibility(View.GONE);
sendMaxButton.setText(getText(R.string.send_max));
if(pendingTransaction.getValue() == null) {
if (sendingMax) {
amountEditText.setVisibility(View.INVISIBLE);
sendAllTextView.setVisibility(View.VISIBLE);
sendMaxButton.setText(getText(R.string.undo));
} else {
amountEditText.setVisibility(View.VISIBLE);
sendAllTextView.setVisibility(View.GONE);
sendMaxButton.setText(getText(R.string.send_max));
}
}
});
pendingTransaction.observe(getViewLifecycleOwner(), pendingTx -> {
showConfirmationLayout(pendingTx != null);
if(pendingTx != null) {
String address = addressEditText.getText().toString();
addressTextView.setText(getString(R.string.tx_address_text, address));
amountTextView.setText(getString(R.string.tx_amount_text, Helper.getDisplayAmount(pendingTx.getAmount())));
feeTextView.setText(getString(R.string.tx_fee_text, Helper.getDisplayAmount(pendingTx.getFee())));
}
});
}
private void sendTx(PendingTransaction pendingTx) {
AsyncTask.execute(() -> {
boolean success = TxService.getInstance().sendTx(pendingTx);
Activity activity = getActivity();
if(activity != null) {
activity.runOnUiThread(() -> {
if (success) {
Toast.makeText(getActivity(), getString(R.string.sent_tx), Toast.LENGTH_SHORT).show();
dismiss();
} else {
sendButton.setEnabled(true);
Toast.makeText(getActivity(), getString(R.string.error_sending_tx), Toast.LENGTH_SHORT).show();
}
});
}
});
}
private void createTx(String address, String amount, boolean sendAll) {
AsyncTask.execute(() -> {
PendingTransaction pendingTx = TxService.getInstance().createTx(address, amount, sendAll);
if(pendingTx != null) {
_pendingTransaction.postValue(pendingTx);
} else {
Activity activity = getActivity();
if(activity != null) {
activity.runOnUiThread(() -> {
createButton.setEnabled(true);
Toast.makeText(getActivity(), getString(R.string.error_creating_tx), Toast.LENGTH_SHORT).show();
});
}
}
});
}
private void showConfirmationLayout(boolean show) {
if(show) {
sendButton.setVisibility(View.VISIBLE);
addressEditText.setVisibility(View.GONE);
amountEditText.setVisibility(View.GONE);
sendAllTextView.setVisibility(View.GONE);
createButton.setVisibility(View.GONE);
sendMaxButton.setVisibility(View.GONE);
pasteAddressImageButton.setVisibility(View.GONE);
feeTextView.setVisibility(View.VISIBLE);
addressTextView.setVisibility(View.VISIBLE);
amountTextView.setVisibility(View.VISIBLE);
} else {
sendButton.setVisibility(View.GONE);
addressEditText.setVisibility(View.VISIBLE);
amountEditText.setVisibility(Boolean.TRUE.equals(sendingMax.getValue()) ? View.GONE : View.VISIBLE);
sendAllTextView.setVisibility(Boolean.TRUE.equals(sendingMax.getValue()) ? View.VISIBLE : View.GONE);
createButton.setVisibility(View.VISIBLE);
sendMaxButton.setVisibility(View.VISIBLE);
pasteAddressImageButton.setVisibility(View.VISIBLE);
feeTextView.setVisibility(View.GONE);
addressTextView.setVisibility(View.GONE);
amountTextView.setVisibility(View.GONE);
}
}
}

View File

@ -60,7 +60,7 @@ public class MoneroHandlerThread extends Thread implements WalletListener {
if(usesTor) {
String proxy = "127.0.0.1:9050";
WalletManager.getInstance().setProxy(proxy);
WalletManager.getInstance().setDaemon(Node.fromString(DefaultNodes.MONERUJO_ONION.getUri()));
WalletManager.getInstance().setDaemon(Node.fromString(DefaultNodes.boldsuck.getUri()));
wallet.setProxy(proxy);
} else {
WalletManager.getInstance().setDaemon(Node.fromString(DefaultNodes.XMRTW.getUri()));
@ -118,9 +118,12 @@ public class MoneroHandlerThread extends Thread implements WalletListener {
listener.onRefresh();
}
public boolean sendTx(String address, String amountStr, boolean sendAll) {
public PendingTransaction createTx(String address, String amountStr, boolean sendAll) {
long amount = sendAll ? SWEEP_ALL : Wallet.getAmountFromString(amountStr);
PendingTransaction pendingTx = wallet.createTransaction(new TxData(address, amount, 0, PendingTransaction.Priority.Priority_Default));
return wallet.createTransaction(new TxData(address, amount, 0, PendingTransaction.Priority.Priority_Default));
}
public boolean sendTx(PendingTransaction pendingTx) {
return pendingTx.commit("", true);
}

View File

@ -2,6 +2,7 @@ package com.m2049r.xmrwallet.service;
import com.m2049r.xmrwallet.MainActivity;
import com.m2049r.xmrwallet.livedata.SingleLiveEvent;
import com.m2049r.xmrwallet.model.PendingTransaction;
public class TxService extends ServiceBase {
public static TxService instance = null;
@ -15,7 +16,11 @@ public class TxService extends ServiceBase {
instance = this;
}
public boolean sendTx(String address, String amount, boolean sendAll) {
return this.getThread().sendTx(address, amount, sendAll);
public PendingTransaction createTx(String address, String amount, boolean sendAll) {
return this.getThread().createTx(address, amount, sendAll);
}
public boolean sendTx(PendingTransaction pendingTransaction) {
return this.getThread().sendTx(pendingTransaction);
}
}

View File

@ -1,13 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp" />
<solid
android:color="@color/oled_colorSecondary"/>
<corners
android:radius="8dp"/>
</shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Color when the row is selected -->
<item android:state_enabled="true" android:drawable="@drawable/button_bg_enabled" />
<!-- Standard background color -->
<item android:drawable="@drawable/button_bg_disabled" />
</selector>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp" />
<solid
android:color="@color/button_disabled_bg_color"/>
<corners
android:radius="8dp"/>
</shape>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp" />
<solid
android:color="@color/oled_colorSecondary"/>
<corners
android:radius="8dp"/>
</shape>

View File

@ -62,7 +62,7 @@
android:layout_marginEnd="24dp"
android:layout_marginBottom="16dp"
android:background="@drawable/button_bg"
android:text="Receive"
android:text="@string/receive"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/send_button"
app:layout_constraintBottom_toBottomOf="parent"/>
@ -75,7 +75,7 @@
android:layout_marginEnd="24dp"
android:layout_marginBottom="16dp"
android:background="@drawable/button_bg"
android:text="Send"
android:text="@string/send"
app:layout_constraintStart_toEndOf="@id/receive_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

View File

@ -21,6 +21,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/display_recovery_phrase"
android:background="@drawable/button_bg"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

View File

@ -12,7 +12,7 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- CREATE LAYOUT -->
<TextView
android:id="@+id/send_monero_textview"
android:layout_width="match_parent"
@ -41,7 +41,8 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/send_monero_textview"
app:layout_constraintEnd_toStartOf="@id/paste_address_imagebutton"
app:layout_constraintBottom_toTopOf="@id/amount_edittext"/>
app:layout_constraintBottom_toTopOf="@id/amount_edittext"
tools:visibility="gone" />
<ImageButton
android:id="@+id/paste_address_imagebutton"
@ -57,7 +58,8 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/address_edittext"
app:layout_constraintTop_toTopOf="@id/address_edittext"
tools:ignore="SpeakableTextPresentCheck" />
tools:ignore="SpeakableTextPresentCheck"
tools:visibility="gone" />
<EditText
android:id="@+id/amount_edittext"
android:layout_width="0dp"
@ -69,8 +71,8 @@
android:inputType="numberDecimal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/send_max_button"
app:layout_constraintBottom_toTopOf="@id/send_button"
tools:visibility="visible"/>
app:layout_constraintBottom_toTopOf="@id/create_tx_button"
tools:visibility="gone"/>
<TextView
android:id="@+id/sending_all_textview"
android:layout_width="0dp"
@ -85,7 +87,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/send_max_button"
app:layout_constraintBottom_toBottomOf="@id/amount_edittext"
tools:visibility="visible"/>
tools:visibility="gone"/>
<Button
android:id="@+id/send_max_button"
android:layout_width="wrap_content"
@ -96,9 +98,79 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/amount_edittext"
app:layout_constraintBottom_toBottomOf="@id/amount_edittext"
app:layout_constraintStart_toEndOf="@id/amount_edittext"/>
app:layout_constraintStart_toEndOf="@id/amount_edittext"
tools:visibility="gone"/>
<Button
android:id="@+id/send_button"
android:id="@+id/create_tx_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginTop="32dp"
android:background="@drawable/button_bg"
android:text="@string/create"
app:layout_constraintTop_toBottomOf="@id/amount_edittext"
app:layout_constraintStart_toStartOf="parent"
tools:visibility="gone"/>
<!-- SEND LAYOUT -->
<TextView
android:id="@+id/address_pending_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tx_address_text"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginTop="32dp"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="gone"
android:singleLine="true"
android:ellipsize="middle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/send_monero_textview"
app:layout_constraintBottom_toTopOf="@id/amount_pending_textview"
tools:visibility="visible"/>
<TextView
android:id="@+id/amount_pending_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tx_amount_text"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginTop="32dp"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/address_pending_textview"
app:layout_constraintBottom_toTopOf="@id/fee_textview"
tools:visibility="visible"/>
<TextView
android:id="@+id/fee_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tx_fee_text"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginTop="32dp"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/amount_pending_textview"
app:layout_constraintBottom_toTopOf="@id/send_tx_button"
tools:visibility="visible"/>
<Button
android:id="@+id/send_tx_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
@ -106,9 +178,10 @@
android:layout_marginTop="32dp"
android:background="@drawable/button_bg"
android:text="@string/send"
app:layout_constraintTop_toBottomOf="@id/amount_edittext"
app:layout_constraintStart_toStartOf="parent"/>
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/fee_textview"
app:layout_constraintStart_toStartOf="parent"
tools:visibility="visible"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

View File

@ -29,6 +29,7 @@
<color name="oled_colorError">@color/oled_negativeColor</color>
<color name="oled_colorOnError">@color/oled_colorBackground</color>
<color name="edittext_bg_color">#CCCCCC</color>
<color name="button_disabled_bg_color">#454545</color>
<!-- CLASSIC -->
<color name="classic_textColorPrimary">#0C080C</color>

View File

@ -542,6 +542,7 @@
<string name="send_max">Send Max</string>
<string name="undo">Undo</string>
<string name="error_sending_tx">Error sending tx</string>
<string name="error_creating_tx">Error creating tx</string>
<string name="create_wallet">Create wallet</string>
<string name="invalid_mnemonic_code">Invalid mnemonic</string>
<string name="copied_to_clipboard">Copied to clipboard</string>
@ -553,6 +554,7 @@
<string name="amount">0.00</string>
<string name="sending_all">SENDING ALL</string>
<string name="send">Send</string>
<string name="create">Create</string>
<string name="send_monero">Send Monero</string>
<string name="recv_monero">Receive Monero</string>
<string name="more_options">More options</string>
@ -562,4 +564,11 @@
<string name="password">Password</string>
<string name="unlock">Unlock</string>
<string name="enter_password">Enter password</string>
<string name="tx_address_text">Address: %1$s</string>
<string name="tx_amount_text">Amount: %1$s XMR</string>
<string name="tx_fee_text">Fee: %1$s XMR</string>
<string name="receive">Receive</string>
<string name="creating_tx">Creating transaction…</string>
<string name="sending_tx">Sending transaction…</string>
<string name="sent_tx">Sent transaction!</string>
</resources>