From cef71b382b659aa9240b813e30cf02cc9f562f58 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 22 Nov 2017 22:11:41 -0400 Subject: [PATCH] - Creating the send fragment - Fixing Asset validation when sending --- .../fragments/SendTransactionFragment.java | 222 ++++++++++++++++++ .../AssetValidationField.java | 11 +- .../views/CryptoNetBalanceViewHolder.java | 14 ++ app/src/main/res/layout/send_transaction.xml | 3 +- 4 files changed, 246 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java new file mode 100644 index 0000000..968e54d --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/SendTransactionFragment.java @@ -0,0 +1,222 @@ +package cy.agorise.crystalwallet.fragments; + +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.Observer; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; +import android.support.v7.app.AppCompatActivity; +import android.text.Editable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Spinner; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; +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.SendTransactionValidator; +import cy.agorise.crystalwallet.viewmodels.validators.UIValidatorListener; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.ValidationField; +import cy.agorise.crystalwallet.views.CryptoCurrencyAdapter; + +public class SendTransactionFragment extends DialogFragment implements UIValidatorListener { + + SendTransactionValidator sendTransactionValidator; + + @BindView(R.id.etFrom) + EditText etFrom; + @BindView(R.id.tvFromError) + TextView tvFromError; + @BindView(R.id.etTo) + EditText etTo; + @BindView(R.id.tvToError) + TextView tvToError; + @BindView(R.id.spAsset) + Spinner spAsset; + @BindView(R.id.tvAssetError) + TextView tvAssetError; + @BindView(R.id.etAmount) + EditText etAmount; + @BindView(R.id.tvAmountError) + TextView tvAmountError; + @BindView (R.id.etMemo) + EditText etMemo; + @BindView(R.id.tvMemoError) + TextView tvMemoError; + @BindView(R.id.btnSend) + Button btnSend; + @BindView(R.id.btnCancel) + Button btnCancel; + + private long cryptoNetAccountId; + private CryptoNetAccount cryptoNetAccount; + private GrapheneAccount grapheneAccount; + private CrystalDatabase db; + + public static SendTransactionFragment newInstance(long cryptoNetAccountId) { + SendTransactionFragment f = new SendTransactionFragment(); + + // Supply num input as an argument. + Bundle args = new Bundle(); + args.putLong("CRYPTO_NET_ACCOUNT_ID", cryptoNetAccountId); + f.setArguments(args); + + return f; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + 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); + + 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 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); + } + }); + + 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) { + this.sendTransactionValidator.validate(); + } + + @OnTextChanged(value = R.id.etTo, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterToChanged(Editable editable) { + this.sendTransactionValidator.validate(); + } + + @OnItemSelected(R.id.spAsset) + public void afterAssetSelected(Spinner spinner, int position) { + this.sendTransactionValidator.validate(); + } + + @OnTextChanged(value = R.id.etAmount, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterAmountChanged(Editable editable) { + this.sendTransactionValidator.validate(); + } + + + @OnTextChanged(value = R.id.etMemo, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterMemoChanged(Editable editable) { + this.sendTransactionValidator.validate(); + } + + //@OnClick(R.id.btnCancel) + //public void cancel(){ + // this.finish(); + //} + + @OnClick(R.id.btnSend) + public void importSend(){ + if (this.sendTransactionValidator.isValid()) { + //TODO convert the amount to long type using the precision of the currency + ValidateBitsharesSendRequest sendRequest = new ValidateBitsharesSendRequest( + this.getContext(), + this.grapheneAccount, + this.etTo.getText().toString(), + Long.parseLong(this.etAmount.getText().toString()), + ((CryptoCurrency)spAsset.getSelectedItem()).getName(), + etMemo.getText().toString() + ); + + //this.finish(); + } + } + + @Override + public void onValidationSucceeded(final ValidationField field) { + final SendTransactionFragment fragment = this; + + + if (field.getView() == etFrom) { + tvFromError.setText(""); + } else if (field.getView() == etTo){ + tvToError.setText(""); + } else if (field.getView() == etAmount){ + tvAmountError.setText(""); + } else if (field.getView() == spAsset){ + tvAssetError.setText(""); + } else if (field.getView() == etMemo){ + tvMemoError.setText(""); + } + + if (sendTransactionValidator.isValid()){ + btnSend.setEnabled(true); + } else { + btnSend.setEnabled(false); + } + } + + @Override + public void onValidationFailed(ValidationField field) { + if (field.getView() == etFrom) { + tvFromError.setText(field.getMessage()); + } else if (field.getView() == etTo){ + tvToError.setText(field.getMessage()); + } else if (field.getView() == spAsset){ + tvAssetError.setText(field.getMessage()); + } else if (field.getView() == etAmount){ + tvAmountError.setText(field.getMessage()); + } else if (field.getView() == etMemo){ + tvMemoError.setText(field.getMessage()); + } + } +} 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 846df03..a836c07 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 @@ -24,8 +24,13 @@ public class AssetValidationField extends ValidationField { public void validate(){ final CryptoCurrency cryptoCurrencySelected = (CryptoCurrency) this.assetField.getSelectedItem(); - final String newValue = ""+cryptoCurrencySelected.getId(); - this.setLastValue(newValue); - validator.validationSucceeded(this); + if (cryptoCurrencySelected != null) { + final String newValue = "" + cryptoCurrencySelected.getId(); + this.setLastValue(newValue); + validator.validationSucceeded(this); + } else { + setMessage("Select a currency"); + validator.validationFailed(this); + } } } 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 ac16d1c..d984803 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java @@ -9,6 +9,7 @@ import android.arch.lifecycle.LiveData; import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProviders; import android.arch.paging.PagedList; +import android.support.v4.app.FragmentTransaction; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; @@ -22,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.SendTransactionFragment; import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCurrencyEquivalence; @@ -99,6 +101,7 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { * dispatch the user to the send activity using this account */ public void sendFromThisAccount(){ + /* //if the crypto net account was loaded if (this.cryptoNetAccountId >= 0) { Intent startActivity = new Intent(); @@ -110,7 +113,18 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); context.startActivity(startActivity); + }*/ + + FragmentTransaction ft = fragment.getFragmentManager().beginTransaction(); + Fragment prev = fragment.getFragmentManager().findFragmentByTag("SendDialog"); + if (prev != null) { + ft.remove(prev); } + ft.addToBackStack(null); + + // Create and show the dialog. + SendTransactionFragment newFragment = SendTransactionFragment.newInstance(this.cryptoNetAccountId); + newFragment.show(ft, "SendDialog"); } /* diff --git a/app/src/main/res/layout/send_transaction.xml b/app/src/main/res/layout/send_transaction.xml index a749d9c..03eec09 100644 --- a/app/src/main/res/layout/send_transaction.xml +++ b/app/src/main/res/layout/send_transaction.xml @@ -9,12 +9,13 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/from_capital" + android:inputType="textFilter" android:textSize="15sp" />