2017-11-23 02:11:41 +00:00
|
|
|
package cy.agorise.crystalwallet.fragments;
|
|
|
|
|
2017-11-29 01:50:46 +00:00
|
|
|
import android.app.Dialog;
|
2017-11-23 02:11:41 +00:00
|
|
|
import android.arch.lifecycle.LiveData;
|
|
|
|
import android.arch.lifecycle.Observer;
|
2017-11-29 01:50:46 +00:00
|
|
|
import android.content.DialogInterface;
|
2017-11-23 02:11:41 +00:00
|
|
|
import android.os.Bundle;
|
2017-11-29 01:50:46 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2017-11-23 02:11:41 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2017-12-07 04:55:58 +00:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-11-23 02:11:41 +00:00
|
|
|
import android.support.v4.app.DialogFragment;
|
|
|
|
import android.support.v4.app.Fragment;
|
2017-11-29 01:50:46 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2017-11-23 02:11:41 +00:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2017-12-07 04:55:58 +00:00
|
|
|
import android.view.animation.LinearInterpolator;
|
2017-11-23 02:11:41 +00:00
|
|
|
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;
|
|
|
|
|
2017-12-06 04:14:48 +00:00
|
|
|
@BindView(R.id.spFrom)
|
|
|
|
Spinner spFrom;
|
2017-11-23 02:11:41 +00:00
|
|
|
@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;
|
2017-12-07 04:55:58 +00:00
|
|
|
@BindView(R.id.btnSend)
|
|
|
|
FloatingActionButton btnSend;
|
2017-12-06 04:14:48 +00:00
|
|
|
@BindView(R.id.btnCancel)
|
|
|
|
TextView btnCancel;
|
2017-11-23 02:11:41 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-29 01:50:46 +00:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
2017-12-04 05:18:53 +00:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.SendTransactionTheme);
|
2017-12-06 04:14:48 +00:00
|
|
|
//builder.setTitle("Send");
|
2017-11-23 02:11:41 +00:00
|
|
|
|
2017-11-29 01:50:46 +00:00
|
|
|
LayoutInflater inflater = getActivity().getLayoutInflater();
|
|
|
|
View view = inflater.inflate(R.layout.send_transaction, null);
|
|
|
|
ButterKnife.bind(this, view);
|
2017-11-23 02:11:41 +00:00
|
|
|
|
|
|
|
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<List<CryptoCoinBalance>> balancesList = db.cryptoCoinBalanceDao().getBalancesFromAccount(cryptoNetAccountId);
|
|
|
|
balancesList.observe(this, new Observer<List<CryptoCoinBalance>>() {
|
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<CryptoCoinBalance> cryptoCoinBalances) {
|
|
|
|
ArrayList<Long> assetIds = new ArrayList<Long>();
|
|
|
|
for (CryptoCoinBalance nextBalance : balancesList.getValue()) {
|
|
|
|
assetIds.add(nextBalance.getCryptoCurrencyId());
|
|
|
|
}
|
|
|
|
List<CryptoCurrency> cryptoCurrencyList = db.cryptoCurrencyDao().getByIds(assetIds);
|
|
|
|
|
|
|
|
CryptoCurrencyAdapter assetAdapter = new CryptoCurrencyAdapter(getContext(), android.R.layout.simple_spinner_item, cryptoCurrencyList);
|
|
|
|
spAsset.setAdapter(assetAdapter);
|
|
|
|
}
|
|
|
|
});
|
2017-12-06 04:14:48 +00:00
|
|
|
// TODO SendTransactionValidator to accept spFrom
|
|
|
|
//sendTransactionValidator = new SendTransactionValidator(this.getContext(), this.cryptoNetAccount, spFrom, etTo, spAsset, etAmount, etMemo);
|
2017-11-23 02:11:41 +00:00
|
|
|
sendTransactionValidator.setListener(this);
|
2017-12-06 04:14:48 +00:00
|
|
|
// etFrom.setText(this.grapheneAccount.getName());
|
2017-11-23 02:11:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 01:50:46 +00:00
|
|
|
builder.setView(view);
|
|
|
|
|
2017-12-06 04:14:48 +00:00
|
|
|
/*builder.setPositiveButton("Send", new DialogInterface.OnClickListener() {
|
2017-11-29 01:50:46 +00:00
|
|
|
@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();
|
|
|
|
}
|
2017-12-06 04:14:48 +00:00
|
|
|
});*/
|
2017-11-29 01:50:46 +00:00
|
|
|
|
|
|
|
AlertDialog dialog = builder.create();
|
|
|
|
|
2017-12-06 05:41:44 +00:00
|
|
|
/*dialog.setOnShowListener(new DialogInterface.OnShowListener() {
|
2017-11-29 01:50:46 +00:00
|
|
|
@Override
|
|
|
|
public void onShow(DialogInterface dialog) {
|
|
|
|
btnSend = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE);
|
|
|
|
btnCancel = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
|
|
|
|
|
|
|
|
btnSend.setEnabled(false);
|
|
|
|
}
|
2017-12-06 05:41:44 +00:00
|
|
|
});*/
|
2017-11-29 01:50:46 +00:00
|
|
|
|
|
|
|
return dialog;
|
2017-11-23 02:11:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 04:33:31 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
// Force dialog fragment to use the full width of the screen
|
|
|
|
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
|
|
}
|
|
|
|
|
2017-11-29 01:50:46 +00:00
|
|
|
/*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<List<CryptoCoinBalance>> balancesList = db.cryptoCoinBalanceDao().getBalancesFromAccount(cryptoNetAccountId);
|
|
|
|
balancesList.observe(this, new Observer<List<CryptoCoinBalance>>() {
|
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<CryptoCoinBalance> cryptoCoinBalances) {
|
|
|
|
ArrayList<Long> assetIds = new ArrayList<Long>();
|
|
|
|
for (CryptoCoinBalance nextBalance : balancesList.getValue()) {
|
|
|
|
assetIds.add(nextBalance.getCryptoCurrencyId());
|
|
|
|
}
|
|
|
|
List<CryptoCurrency> 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;
|
|
|
|
}*/
|
|
|
|
|
2017-12-06 04:14:48 +00:00
|
|
|
@OnItemSelected(R.id.spFrom)
|
|
|
|
public void afterFromSelected(Spinner spinner, int position) {
|
2017-11-23 02:11:41 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-12-06 04:14:48 +00:00
|
|
|
@OnClick(R.id.btnCancel)
|
|
|
|
public void cancel(){
|
|
|
|
this.dismiss();
|
|
|
|
}
|
2017-11-23 02:11:41 +00:00
|
|
|
|
2017-11-29 01:50:46 +00:00
|
|
|
//@OnClick(R.id.btnSend)
|
|
|
|
public void sendTransaction(){
|
2017-11-23 02:11:41 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2017-12-06 04:14:48 +00:00
|
|
|
if (field.getView() == spFrom) {
|
2017-11-23 02:11:41 +00:00
|
|
|
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("");
|
|
|
|
}
|
|
|
|
|
2017-11-29 01:50:46 +00:00
|
|
|
if (btnSend != null) {
|
|
|
|
if (sendTransactionValidator.isValid()) {
|
|
|
|
btnSend.setEnabled(true);
|
|
|
|
} else {
|
|
|
|
btnSend.setEnabled(false);
|
|
|
|
}
|
2017-11-23 02:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onValidationFailed(ValidationField field) {
|
2017-12-06 04:14:48 +00:00
|
|
|
if (field.getView() == spFrom) {
|
2017-11-23 02:11:41 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|