From a541a70c1a6a808bf34aaa6848299748197caa63 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Tue, 28 Nov 2017 21:50:46 -0400 Subject: [PATCH] - Adding send and cancel button to Send DialogFragment - Adding ReceiveTransaction DialogFragment. - Generating QrCode to receive transactions --- app/build.gradle | 2 + .../ReceiveTransactionActivity.java | 2 +- .../activities/SendTransactionActivity.java | 6 +- .../fragments/ReceiveTransactionFragment.java | 278 ++++++++++++++++++ .../fragments/SendTransactionFragment.java | 103 ++++++- .../ReceiveTransactionValidator.java | 34 +++ .../AssetValidationField.java | 4 + .../ReceiveAmountValidationField.java | 41 +++ .../validationfields/ToValidationField.java | 40 ++- .../views/CryptoNetBalanceViewHolder.java | 30 ++ .../main/res/layout/receive_transaction.xml | 25 +- app/src/main/res/layout/send_transaction.xml | 4 +- app/src/main/res/values/colors.xml | 2 + 13 files changed, 533 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ReceiveTransactionValidator.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/ReceiveAmountValidationField.java diff --git a/app/build.gradle b/app/build.gradle index dcf9839..ce4faeb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -49,6 +49,8 @@ dependencies { compile 'org.tukaani:xz:1.6' compile 'com.jakewharton:butterknife:8.8.1' compile 'com.github.bilthon:graphenej:0.4.6-alpha1' + compile 'com.google.zxing:core:3.3.1'; + testCompile 'junit:junit:4.12' testCompile 'org.mockito:mockito-core:1.10.19' annotationProcessor 'android.arch.lifecycle:compiler:1.0.0' diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/ReceiveTransactionActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/ReceiveTransactionActivity.java index 93e0c4a..6e4f266 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/ReceiveTransactionActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/ReceiveTransactionActivity.java @@ -33,7 +33,7 @@ import cy.agorise.crystalwallet.views.CryptoCurrencyAdapter; public class ReceiveTransactionActivity extends AppCompatActivity { - @BindView(R.id.tvReceiveAddress) + //@BindView(R.id.tvReceiveAddress) TextView tvReceiveAddress; private long cryptoNetAccountId; diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/SendTransactionActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/SendTransactionActivity.java index 578daa8..e2d7c9f 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/SendTransactionActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/SendTransactionActivity.java @@ -64,9 +64,9 @@ public class SendTransactionActivity extends AppCompatActivity implements UIVali EditText etMemo; @BindView(R.id.tvMemoError) TextView tvMemoError; - @BindView(R.id.btnSend) + //@BindView(R.id.btnSend) Button btnSend; - @BindView(R.id.btnCancel) + //@BindView(R.id.btnCancel) Button btnCancel; private long cryptoNetAccountId; @@ -153,7 +153,7 @@ public class SendTransactionActivity extends AppCompatActivity implements UIVali this.finish(); } - @OnClick(R.id.btnSend) + //@OnClick(R.id.btnSend) public void importSend(){ if (this.sendTransactionValidator.isValid()) { //TODO convert the amount to long type using the precision of the currency diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java new file mode 100644 index 0000000..bb17cb6 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java @@ -0,0 +1,278 @@ +package cy.agorise.crystalwallet.fragments; + +import android.app.Dialog; +import android.app.LauncherActivity; +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.Observer; +import android.content.DialogInterface; +import android.graphics.Bitmap; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.DialogFragment; +import android.support.v7.app.AlertDialog; +import android.text.Editable; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.Spinner; +import android.widget.TextView; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.MultiFormatWriter; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; + +import cy.agorise.graphenej.Invoice; +import java.util.ArrayList; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnItemSelected; +import butterknife.OnTextChanged; +import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.cryptonetinforequests.ValidateBitsharesSendRequest; +import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.models.CryptoCoinBalance; +import cy.agorise.crystalwallet.models.CryptoCurrency; +import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.models.GrapheneAccount; +import cy.agorise.crystalwallet.viewmodels.validators.ReceiveTransactionValidator; +import cy.agorise.crystalwallet.viewmodels.validators.SendTransactionValidator; +import cy.agorise.crystalwallet.viewmodels.validators.UIValidatorListener; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.ValidationField; +import cy.agorise.crystalwallet.views.CryptoCurrencyAdapter; +import cy.agorise.graphenej.LineItem; + +public class ReceiveTransactionFragment extends DialogFragment implements UIValidatorListener { + + ReceiveTransactionValidator receiveTransactionValidator; + + @BindView(R.id.etAmount) + EditText etAmount; + @BindView(R.id.tvAmountError) + TextView tvAmountError; + @BindView(R.id.spAsset) + Spinner spAsset; + @BindView(R.id.tvAssetError) + TextView tvAssetError; + @BindView(R.id.ivQrCode) + ImageView ivQrCode; + + private Button btnShareQrCode; + private Button btnClose; + + private long cryptoNetAccountId; + private CryptoNetAccount cryptoNetAccount; + private CryptoCurrency cryptoCurrency; + private GrapheneAccount grapheneAccount; + private CrystalDatabase db; + + private Invoice invoice; + private ArrayList invoiceItems; + + public static ReceiveTransactionFragment newInstance(long cryptoNetAccountId) { + ReceiveTransactionFragment f = new ReceiveTransactionFragment(); + + // Supply num input as an argument. + Bundle args = new Bundle(); + args.putLong("CRYPTO_NET_ACCOUNT_ID", cryptoNetAccountId); + f.setArguments(args); + + f.invoiceItems = new ArrayList(); + f.invoice = new Invoice("","","","",null,"",""); + + return f; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle("Receive Assets"); + + LayoutInflater inflater = getActivity().getLayoutInflater(); + View view = inflater.inflate(R.layout.receive_transaction, null); + ButterKnife.bind(this, view); + + this.cryptoNetAccountId = getArguments().getLong("CRYPTO_NET_ACCOUNT_ID",-1); + + if (this.cryptoNetAccountId != -1) { + db = CrystalDatabase.getAppDatabase(this.getContext()); + this.cryptoNetAccount = db.cryptoNetAccountDao().getById(this.cryptoNetAccountId); + + /* + * this is only for graphene accounts. + * + **/ + this.grapheneAccount = new GrapheneAccount(this.cryptoNetAccount); + this.grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getByAccountId(this.cryptoNetAccountId)); + + final LiveData> balancesList = db.cryptoCoinBalanceDao().getBalancesFromAccount(cryptoNetAccountId); + balancesList.observe(this, new Observer>() { + @Override + public void onChanged(@Nullable List cryptoCoinBalances) { + ArrayList assetIds = new ArrayList(); + for (CryptoCoinBalance nextBalance : balancesList.getValue()) { + assetIds.add(nextBalance.getCryptoCurrencyId()); + } + List cryptoCurrencyList = db.cryptoCurrencyDao().getByIds(assetIds); + + CryptoCurrencyAdapter assetAdapter = new CryptoCurrencyAdapter(getContext(), android.R.layout.simple_spinner_item, cryptoCurrencyList); + spAsset.setAdapter(assetAdapter); + } + }); + + receiveTransactionValidator = new ReceiveTransactionValidator(this.getContext(), this.cryptoNetAccount, spAsset, etAmount); + receiveTransactionValidator.setListener(this); + } + + builder.setView(view); + + builder.setPositiveButton("Share this QR", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + shareQrCode(); + } + }); + builder.setNegativeButton("Close", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + + AlertDialog dialog = builder.create(); + + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + btnShareQrCode = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE); + btnClose = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_NEGATIVE); + + btnShareQrCode.setEnabled(false); + } + }); + + return dialog; + } + + @OnTextChanged(value = R.id.etAmount, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterAmountChanged(Editable editable) { + this.receiveTransactionValidator.validate(); + } + + @OnItemSelected(R.id.spAsset) + public void afterAssetSelected(Spinner spinner, int position) { + this.cryptoCurrency = (CryptoCurrency)spinner.getSelectedItem(); + this.receiveTransactionValidator.validate(); + } + + public void shareQrCode(){ + if (this.receiveTransactionValidator.isValid()) { + //Share Qr Code + } + } + + @Override + public void onValidationSucceeded(final ValidationField field) { + final ReceiveTransactionFragment fragment = this; + + + if (field.getView() == etAmount){ + tvAmountError.setText(""); + } else if (field.getView() == spAsset){ + tvAssetError.setText(""); + } + + if (btnShareQrCode != null) { + if (receiveTransactionValidator.isValid()) { + createQrCode(); + btnShareQrCode.setEnabled(true); + } else { + btnShareQrCode.setEnabled(false); + } + } + } + + @Override + public void onValidationFailed(ValidationField field) { + if (field.getView() == spAsset){ + tvAssetError.setText(field.getMessage()); + } else if (field.getView() == etAmount){ + tvAmountError.setText(field.getMessage()); + } + + ivQrCode.setImageResource(android.R.color.transparent); + } + + public void createQrCode(){ + Double amount = 0.0; + try{ + amount = Double.valueOf(this.etAmount.getText().toString()); + } catch(NumberFormatException e){ + Log.e("ReceiveFragment","Amount casting error."); + } + + + this.invoiceItems.clear(); + this.invoiceItems.add( + new LineItem("transfer", 1, amount) + ); + + LineItem items[] = new LineItem[this.invoiceItems.size()]; + items = this.invoiceItems.toArray(items); + this.invoice.setLineItems(items); + this.invoice.setTo(this.grapheneAccount.getName()); + this.invoice.setCurrency(this.cryptoCurrency.getName()); + + try { + Bitmap bitmap = textToImageEncode(Invoice.toQrCode(invoice)); + ivQrCode.setImageBitmap(bitmap); + } catch (WriterException e) { + Log.e("ReceiveFragment", "Error creating QrCode"); + } + } + + Bitmap textToImageEncode(String Value) throws WriterException { + //TODO: do this in another thread + + BitMatrix bitMatrix; + try { + bitMatrix = new MultiFormatWriter().encode( + Value, + BarcodeFormat.DATA_MATRIX.QR_CODE, + ivQrCode.getWidth(), ivQrCode.getHeight(), null + ); + + } catch (IllegalArgumentException Illegalargumentexception) { + return null; + } + int bitMatrixWidth = bitMatrix.getWidth(); + int bitMatrixHeight = bitMatrix.getHeight(); + int[] pixels = new int[bitMatrixWidth * bitMatrixHeight]; + + for (int y = 0; y < bitMatrixHeight; y++) { + int offset = y * bitMatrixWidth; + + for (int x = 0; x < bitMatrixWidth; x++) { + pixels[offset + x] = bitMatrix.get(x, y) ? + getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor); + } + } + + Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444); + bitmap.setPixels(pixels, 0, ivQrCode.getWidth(), 0, 0, bitMatrixWidth, bitMatrixHeight); + return bitmap; + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java index 968e54d..ee00954 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java @@ -1,11 +1,15 @@ package cy.agorise.crystalwallet.fragments; +import android.app.Dialog; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.Observer; +import android.content.DialogInterface; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; +import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.view.LayoutInflater; @@ -60,9 +64,9 @@ public class SendTransactionFragment extends DialogFragment implements UIValidat EditText etMemo; @BindView(R.id.tvMemoError) TextView tvMemoError; - @BindView(R.id.btnSend) + //@BindView(R.id.btnSend) Button btnSend; - @BindView(R.id.btnCancel) + //@BindView(R.id.btnCancel) Button btnCancel; private long cryptoNetAccountId; @@ -86,12 +90,15 @@ public class SendTransactionFragment extends DialogFragment implements UIValidat super.onCreate(savedInstanceState); } - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState){ - View view = inflater.inflate(R.layout.send_transaction, container, false); - ButterKnife.bind(this, view); + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle("Send"); - btnSend.setEnabled(false); + LayoutInflater inflater = getActivity().getLayoutInflater(); + View view = inflater.inflate(R.layout.send_transaction, null); + ButterKnife.bind(this, view); this.cryptoNetAccountId = getArguments().getLong("CRYPTO_NET_ACCOUNT_ID",-1); @@ -126,9 +133,75 @@ public class SendTransactionFragment extends DialogFragment implements UIValidat etFrom.setText(this.grapheneAccount.getName()); } - return view; + builder.setView(view); + + builder.setPositiveButton("Send", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + sendTransaction(); + } + }); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + + AlertDialog dialog = builder.create(); + + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + btnSend = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE); + btnCancel = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_NEGATIVE); + + btnSend.setEnabled(false); + } + }); + + return dialog; } + /*public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState){ + View view = inflater.inflate(R.layout.send_transaction, container, false); + ButterKnife.bind(this, view); + + btnSend.setEnabled(false); + + this.cryptoNetAccountId = getArguments().getLong("CRYPTO_NET_ACCOUNT_ID",-1); + + if (this.cryptoNetAccountId != -1) { + db = CrystalDatabase.getAppDatabase(this.getContext()); + this.cryptoNetAccount = db.cryptoNetAccountDao().getById(this.cryptoNetAccountId); + + this.grapheneAccount = new GrapheneAccount(this.cryptoNetAccount); + this.grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getByAccountId(this.cryptoNetAccountId)); + + final LiveData> balancesList = db.cryptoCoinBalanceDao().getBalancesFromAccount(cryptoNetAccountId); + balancesList.observe(this, new Observer>() { + @Override + public void onChanged(@Nullable List cryptoCoinBalances) { + ArrayList assetIds = new ArrayList(); + for (CryptoCoinBalance nextBalance : balancesList.getValue()) { + assetIds.add(nextBalance.getCryptoCurrencyId()); + } + List cryptoCurrencyList = db.cryptoCurrencyDao().getByIds(assetIds); + + CryptoCurrencyAdapter assetAdapter = new CryptoCurrencyAdapter(getContext(), android.R.layout.simple_spinner_item, cryptoCurrencyList); + spAsset.setAdapter(assetAdapter); + } + }); + + sendTransactionValidator = new SendTransactionValidator(this.getContext(), this.cryptoNetAccount, etFrom, etTo, spAsset, etAmount, etMemo); + sendTransactionValidator.setListener(this); + etFrom.setText(this.grapheneAccount.getName()); + } + + return view; + }*/ + @OnTextChanged(value = R.id.etFrom, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) void afterFromChanged(Editable editable) { @@ -164,8 +237,8 @@ public class SendTransactionFragment extends DialogFragment implements UIValidat // this.finish(); //} - @OnClick(R.id.btnSend) - public void importSend(){ + //@OnClick(R.id.btnSend) + public void sendTransaction(){ if (this.sendTransactionValidator.isValid()) { //TODO convert the amount to long type using the precision of the currency ValidateBitsharesSendRequest sendRequest = new ValidateBitsharesSendRequest( @@ -198,10 +271,12 @@ public class SendTransactionFragment extends DialogFragment implements UIValidat tvMemoError.setText(""); } - if (sendTransactionValidator.isValid()){ - btnSend.setEnabled(true); - } else { - btnSend.setEnabled(false); + if (btnSend != null) { + if (sendTransactionValidator.isValid()) { + btnSend.setEnabled(true); + } else { + btnSend.setEnabled(false); + } } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ReceiveTransactionValidator.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ReceiveTransactionValidator.java new file mode 100644 index 0000000..017f087 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ReceiveTransactionValidator.java @@ -0,0 +1,34 @@ +package cy.agorise.crystalwallet.viewmodels.validators; + +import android.content.Context; +import android.widget.EditText; +import android.widget.Spinner; + +import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.AmountValidationField; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.AssetValidationField; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.FromValidationField; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.MemoValidationField; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.ReceiveAmountValidationField; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.ToValidationField; + +/** + * Created by Henry Varona on 2/10/2017. + */ + +public class ReceiveTransactionValidator extends UIValidator { + + private CryptoNetAccount account; + + + public ReceiveTransactionValidator(Context context, CryptoNetAccount account, Spinner assetSpinner, EditText amountEdit){ + super(context); + this.account = account; + this.addField(new AssetValidationField(assetSpinner)); + this.addField(new ReceiveAmountValidationField(amountEdit)); + } + + public CryptoNetAccount getAccount() { + return account; + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/AssetValidationField.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/AssetValidationField.java index a836c07..54691f7 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/AssetValidationField.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/AssetValidationField.java @@ -27,9 +27,13 @@ public class AssetValidationField extends ValidationField { if (cryptoCurrencySelected != null) { final String newValue = "" + cryptoCurrencySelected.getId(); this.setLastValue(newValue); + setValidForValue(newValue, true); validator.validationSucceeded(this); } else { + final String newValue = ""+-1; setMessage("Select a currency"); + this.setLastValue(newValue); + setValidForValue(newValue, false); validator.validationFailed(this); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/ReceiveAmountValidationField.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/ReceiveAmountValidationField.java new file mode 100644 index 0000000..673067a --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/ReceiveAmountValidationField.java @@ -0,0 +1,41 @@ +package cy.agorise.crystalwallet.viewmodels.validators.validationfields; + +import android.widget.EditText; +import android.widget.Spinner; + +import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.models.CryptoCoinBalance; +import cy.agorise.crystalwallet.models.CryptoCurrency; +import cy.agorise.crystalwallet.models.CryptoNetAccount; + +/** + * Created by Henry Varona on 26/11/2017. + */ + +public class ReceiveAmountValidationField extends ValidationField { + + private EditText amountField; + + public ReceiveAmountValidationField(EditText amountField) { + super(amountField); + this.amountField = amountField; + } + + public void validate(){ + String newAmountText = amountField.getText().toString(); + try { + final float newAmountValue = Float.parseFloat(newAmountText); + } catch (NumberFormatException e){ + this.setLastValue(newAmountText); + setValidForValue(newAmountText, false); + validator.validationFailed(this); + return; + } + this.setLastValue(newAmountText); + this.startValidating(); + + setValidForValue(newAmountText, true); + validator.validationSucceeded(this); + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/ToValidationField.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/ToValidationField.java index ca2cd8b..f8b676c 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/ToValidationField.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/ToValidationField.java @@ -17,7 +17,7 @@ public class ToValidationField extends ValidationField { private EditText toField; public ToValidationField(EditText fromField, EditText toField){ - super(fromField); + super(toField); this.fromField = fromField; this.toField = toField; } @@ -25,24 +25,32 @@ public class ToValidationField extends ValidationField { public void validate(){ final String fromNewValue = fromField.getText().toString(); final String toNewValue = toField.getText().toString(); - this.setLastValue(toNewValue); + final String mixedValue = fromNewValue+"_"+toNewValue; + this.setLastValue(mixedValue); this.startValidating(); final ValidationField field = this; - final ValidateExistBitsharesAccountRequest request = new ValidateExistBitsharesAccountRequest(toNewValue); - request.setListener(new CryptoNetInfoRequestListener() { - @Override - public void onCarryOut() { - if (!request.getAccountExists()){ - setValidForValue(toNewValue, false); - setMessage(validator.getContext().getResources().getString(R.string.account_name_not_exist)); - validator.validationFailed(field); - } else { - setValidForValue(toNewValue, true); - validator.validationSucceeded(field); + if (fromNewValue.equals(toNewValue)){ + setValidForValue(mixedValue, false); + setMessage(validator.getContext().getResources().getString(R.string.warning_msg_same_account)); + validator.validationFailed(field); + } else { + + final ValidateExistBitsharesAccountRequest request = new ValidateExistBitsharesAccountRequest(toNewValue); + request.setListener(new CryptoNetInfoRequestListener() { + @Override + public void onCarryOut() { + if (!request.getAccountExists()) { + setValidForValue(mixedValue, false); + setMessage(validator.getContext().getResources().getString(R.string.account_name_not_exist)); + validator.validationFailed(field); + } else { + setValidForValue(mixedValue, true); + validator.validationSucceeded(field); + } } - } - }); - CryptoNetInfoRequests.getInstance().addRequest(request); + }); + CryptoNetInfoRequests.getInstance().addRequest(request); + } } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java index d984803..5fe8fc2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java @@ -23,6 +23,7 @@ import butterknife.ButterKnife; import butterknife.OnClick; import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.activities.SendTransactionActivity; +import cy.agorise.crystalwallet.fragments.ReceiveTransactionFragment; import cy.agorise.crystalwallet.fragments.SendTransactionFragment; import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; @@ -58,6 +59,12 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.btnSendFromThisAccount) Button btnSendFromThisAccount; + /* + * The button for receiving transactions to this crypto net balance account + */ + @BindView(R.id.btnReceiveWithThisAccount) + Button btnReceiveToThisAccount; + Context context; /* @@ -81,6 +88,7 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { cryptoNetName = (TextView) itemView.findViewById(R.id.tvCryptoNetName); cryptoCoinBalanceListView = (CryptoCoinBalanceListView) itemView.findViewById(R.id.cryptoCoinBalancesListView); btnSendFromThisAccount = (Button) itemView.findViewById(R.id.btnSendFromThisAccount); + btnReceiveToThisAccount = (Button) itemView.findViewById(R.id.btnReceiveWithThisAccount); //Setting the send button btnSendFromThisAccount.setOnClickListener(new View.OnClickListener() { @@ -89,6 +97,12 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { sendFromThisAccount(); } }); + btnReceiveToThisAccount.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + receiveToThisAccount(); + } + }); this.fragment = fragment; this.context = itemView.getContext(); } @@ -127,6 +141,22 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { newFragment.show(ft, "SendDialog"); } + /* + * dispatch the user to the receive activity using this account + */ + public void receiveToThisAccount(){ + FragmentTransaction ft = fragment.getFragmentManager().beginTransaction(); + Fragment prev = fragment.getFragmentManager().findFragmentByTag("ReceiveDialog"); + if (prev != null) { + ft.remove(prev); + } + ft.addToBackStack(null); + + // Create and show the dialog. + ReceiveTransactionFragment newFragment = ReceiveTransactionFragment.newInstance(this.cryptoNetAccountId); + newFragment.show(ft, "ReceiveDialog"); + } + /* * Binds this view with the data of an element of the list */ diff --git a/app/src/main/res/layout/receive_transaction.xml b/app/src/main/res/layout/receive_transaction.xml index bb2946e..1ead746 100644 --- a/app/src/main/res/layout/receive_transaction.xml +++ b/app/src/main/res/layout/receive_transaction.xml @@ -5,8 +5,29 @@ android:orientation="vertical" android:weightSum="1"> - + + + android:gravity="top" + android:inputType="textMultiLine" + android:textColor="@color/white" /> + + \ No newline at end of file diff --git a/app/src/main/res/layout/send_transaction.xml b/app/src/main/res/layout/send_transaction.xml index 03eec09..539c465 100644 --- a/app/src/main/res/layout/send_transaction.xml +++ b/app/src/main/res/layout/send_transaction.xml @@ -90,7 +90,7 @@ android:layout_height="wrap_content" android:id="@+id/tvMemoError" android:textColor="@color/red" /> - + --> \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 2a7fee2..e2f6b22 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -36,4 +36,6 @@ #000000 #000000 #ff56a9c7 + #000000 + #ffffff