Fix transaction edition

- Fix crash when trying to edit a transaction
- Update the code when editing a transaction to match the new interface
This commit is contained in:
Tanguy Herbron 2018-08-15 02:51:28 +02:00
parent 74cae8fa67
commit df9ab11410
6 changed files with 343 additions and 114 deletions

View File

@ -180,7 +180,7 @@ public class Transactions extends Fragment {
private void drawTransactionList(ArrayList<Transaction> transactions)
{
TransactionListAdapter transactionListAdapter = new TransactionListAdapter(getActivity().getBaseContext(), transactions);
TransactionListAdapter transactionListAdapter = new TransactionListAdapter(getActivity(), transactions);
transactionLayout.setAdapter(transactionListAdapter);
transactionLayout.setTextFilterEnabled(false);

View File

@ -36,6 +36,7 @@ import com.herbron.moodl.CurrencyInfoUpdateNotifierInterface;
import com.herbron.moodl.CustomAdapters.PairRecordListAdapter;
import com.herbron.moodl.CustomLayouts.CustomRecordFragment;
import com.herbron.moodl.DataManagers.CurrencyData.Currency;
import com.herbron.moodl.DataManagers.CurrencyData.Transaction;
import com.herbron.moodl.DataManagers.ExchangeManager.Exchange;
import com.herbron.moodl.DataManagers.InfoAPIManagers.CryptocompareApiManager;
import com.herbron.moodl.DataManagers.DatabaseManager;
@ -84,55 +85,31 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
private boolean isGlobalLayoutVisible;
/*private boolean checkPriceText()
{
String purchasedPriceText = purchasedPriceEditText.getText().toString();
double purchasedPrice;
private TextWatcher coinTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
try {
purchasedPrice = Double.parseDouble(purchasedPriceText);
}
if(purchasedPrice < 0)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
currencyIconImageView.setImageBitmap(null);
exchange_autoCompleteTextView.setEnabled(false);
exchange_autoCompleteTextView.setText("");
((LinearLayout) tabLayout.getChildAt(0)).getChildAt(2).setEnabled(false);
if(isGlobalLayoutVisible && globalTabLayouts.getAnimation().hasEnded())
{
purchasedPriceEditText.setError(getResources().getString(R.string.field_negative));
globalTabLayouts.startAnimation(dismissAnimation);
}
} catch (NumberFormatException e) {
purchasedPriceEditText.setError(getResources().getString(R.string.field_nan));
return false;
}
if(purchasedPriceText.equals(""))
{
purchasedPriceEditText.setError(getResources().getString(R.string.field_empty));
@Override
public void afterTextChanged(Editable s) {
return false;
}
return true;
}
private boolean checkAmountText()
{
String amountText = amountTxtView.getText().toString();
try {
Double.parseDouble(amountText);
} catch (NumberFormatException e) {
amountTxtView.setError(getResources().getString(R.string.field_nan));
return false;
}
if(amountText.equals(""))
{
amountTxtView.setError(getResources().getString(R.string.field_empty));
return false;
}
return true;
}*/
};
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -165,6 +142,112 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
setupBackButton();
}
private void checkCallingIntent()
{
Intent intent = getIntent();
int transactionId = intent.getIntExtra("transactionId", -1);
if(transactionId != -1)
{
DatabaseManager databaseManager = new DatabaseManager(getBaseContext());
Transaction transaction = databaseManager.getCurrencyTransactionById(transactionId);
List<Currency> denominationList = cryptocompareApiManager.getCurrenciesDenomination();
boolean found = false;
int index = 0;
while(index < denominationList.size() && !found)
{
if(denominationList.get(index).getSymbol().equals(transaction.getSymbol()))
{
currency = denominationList.get(index);
found = true;
currency.setListener(RecordTransactionActivity.this);
updateExchangeAdapter(currency.getSymbol());
exchange_autoCompleteTextView.setEnabled(true);
IconDownloaderTask iconDownloaderTask = new IconDownloaderTask();
iconDownloaderTask.execute();
coin_autoCompleteTextView.removeTextChangedListener(coinTextWatcher);
coin_autoCompleteTextView.setText(PlaceholderManager.getDenomination(currency.getName(), currency.getSymbol(), getBaseContext()));
coin_autoCompleteTextView.setEnabled(false);
if(globalTabLayouts.getVisibility() == View.GONE)
{
globalTabLayouts.setVisibility(View.VISIBLE);
}
globalTabLayouts.startAnimation(revealAnimation);
isGlobalLayoutVisible = true;
updateCurrencyData();
}
index++;
}
found = false;
index = 0;
switch (transaction.getType())
{
case "b":
List<Exchange> exchangeList = cryptocompareApiManager.getExchangeList(currency.getSymbol());
while(index < exchangeList.size() && !found)
{
if(exchangeList.get(index).getName().equals(transaction.getSource()))
{
exchange = exchangeList.get(index);
exchange_autoCompleteTextView.setText(exchange.getName());
exchange_autoCompleteTextView.setEnabled(true);
updateExchangeData();
updatePairAdapter();
found = true;
}
index++;
}
List<Pair> pairList = exchange.getPairsFor(currency.getSymbol());
found = false;
index = 0;
while(index < pairList.size() && !found)
{
if(pairList.get(index).contains(currency.getSymbol()) && pairList.get(index).contains(transaction.getSymPair()))
{
pair = pairList.get(index);
pair_autoCompleteTextView.setText(PlaceholderManager.getPairString(pair.getFrom(), pair.getTo(), getBaseContext()));
pair_autoCompleteTextView.setEnabled(true);
updatePairData();
found = true;
}
index++;
}
tabLayout.getTabAt(0).select();
break;
case "s":
tabLayout.getTabAt(1).select();
break;
case "t":
tabLayout.getTabAt(2).select();
break;
}
}
}
public Currency getCurrency()
{
return currency;
@ -380,31 +463,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
coin_autoCompleteTextView.setThreshold(0);
coin_autoCompleteTextView.setAdapter(adapter);
coin_autoCompleteTextView.setTextColor(getResources().getColor(R.color.white));
coin_autoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
currencyIconImageView.setImageBitmap(null);
exchange_autoCompleteTextView.setEnabled(false);
exchange_autoCompleteTextView.setText("");
((LinearLayout) tabLayout.getChildAt(0)).getChildAt(2).setEnabled(false);
if(isGlobalLayoutVisible && globalTabLayouts.getAnimation().hasEnded())
{
globalTabLayouts.startAnimation(dismissAnimation);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
coin_autoCompleteTextView.addTextChangedListener(coinTextWatcher);
coin_autoCompleteTextView.setOnClickListener(new View.OnClickListener() {
@Override
@ -509,7 +568,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
@Override
public void onExchangesUpdated() {
checkCallingIntent();
}
private class IconDownloaderTask extends AsyncTask<Void, Void, Void> {

View File

@ -20,8 +20,10 @@ import android.widget.Spinner;
import android.widget.TimePicker;
import com.herbron.moodl.Activities.HomeActivity;
import com.herbron.moodl.Activities.RecordTransactionActivity;
import com.herbron.moodl.CustomLayouts.CustomRecordFragment;
import com.herbron.moodl.DataManagers.CurrencyData.Currency;
import com.herbron.moodl.DataManagers.CurrencyData.Transaction;
import com.herbron.moodl.DataManagers.DatabaseManager;
import com.herbron.moodl.DataManagers.ExchangeManager.Exchange;
import com.herbron.moodl.DataManagers.InfoAPIManagers.Pair;
@ -58,6 +60,9 @@ public class BuyFragment extends CustomRecordFragment {
private static Currency fragmentCurrency;
private static Exchange fragmentExchange;
private static Pair fragmentPair;
private List<String> symbolStrings;
private int transactionId;
@Nullable
@Override
@ -73,9 +78,39 @@ public class BuyFragment extends CustomRecordFragment {
initializeViewElements();
checkCallingIntent();
return view;
}
private void checkCallingIntent()
{
Intent intent = getActivity().getIntent();
transactionId = intent.getIntExtra("transactionId", -1);
if(transactionId != -1)
{
DatabaseManager databaseManager = new DatabaseManager(context);
Transaction transaction = databaseManager.getCurrencyTransactionById(transactionId);
if(transaction.getType().equals("b"))
{
fillFields(transaction);
}
}
}
private void fillFields(Transaction transaction)
{
amoutEditText.setText(String.valueOf(transaction.getAmount()));
buyPriceEditText.setText(String.valueOf(transaction.getPurchasePrice()));
calendar.setTimeInMillis(transaction.getTimestamp());
buyDateEditText.setText(sdf.format(calendar.getTime()));
totalValueEditText.setText(String.valueOf(transaction.getAmount() * transaction.getPurchasePrice()));
fees_editText.setText(String.valueOf(transaction.getFees()));
note_editText.setText(transaction.getNote());
}
private void initializeViewElements()
{
totalValueEditText = view.findViewById(R.id.totalValue_editText);
@ -141,34 +176,58 @@ public class BuyFragment extends CustomRecordFragment {
if(isFieldCorrectlyFilled(amoutEditText, true) && isFieldCorrectlyFilled(buyPriceEditText, true) && isFieldCorrectlyFilled(totalValueEditText, true))
{
double amount = Double.parseDouble(amoutEditText.getText().toString());
double purchasedPrice = Double.parseDouble(buyPriceEditText.getText().toString());
double purchasePrice = Double.parseDouble(buyPriceEditText.getText().toString());
double fees;
String feeCurrency;
if(fees_editText.getText().toString().equals(""))
if(feesCurrencySpinner.getSelectedItemPosition() < 1)
{
fees = 0;
feeCurrency = fragmentPair.getFrom();
}
else
{
fees = Double.parseDouble(fees_editText.getText().toString());
feeCurrency = fragmentPair.getTo();
}
fees = getFees(feeCurrency, amount, purchasePrice);
String note = note_editText.getText().toString();
DatabaseManager databaseManager = new DatabaseManager(getContext());
databaseManager.addTransaction(fragmentCurrency.getSymbol()
, amount
, calendar.getTime()
, purchasedPrice
, fees
, note
, fragmentPair.getFrom().equals(fragmentCurrency.getSymbol()) ? fragmentPair.getTo() : fragmentPair.getFrom());
preferenceManager.setMustUpdateSummary(true);
Intent intent = new Intent(getActivity(), HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
if(transactionId == -1)
{
databaseManager.addTransaction(fragmentCurrency.getSymbol()
, amount
, calendar.getTime()
, purchasePrice
, fees
, note
, fragmentPair.getFrom().equals(fragmentCurrency.getSymbol()) ? fragmentPair.getTo() : fragmentPair.getFrom()
, feeCurrency
, fragmentExchange.getName()
, "b");
Intent intent = new Intent(getActivity(), HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
else
{
databaseManager.updateTransactionWithId(transactionId
, amount
, calendar.getTime()
, purchasePrice
, fees
, note
, fragmentPair.getFrom().equals(fragmentCurrency.getSymbol()) ? fragmentPair.getTo() : fragmentPair.getFrom()
, feeCurrency
, fragmentExchange.getName()
, "b");
}
getActivity().finish();
}
}
@ -178,6 +237,34 @@ public class BuyFragment extends CustomRecordFragment {
note_editText = view.findViewById(R.id.note_editText);
}
private double getFees(String feeCurrency, double amount, double purchasedPrice)
{
double fees;
if(fees_editText.getText().toString().equals(""))
{
fees = 0;
}
else
{
fees = Double.parseDouble(fees_editText.getText().toString());
if(feesCurrencySpinner.getSelectedItemPosition() % 2 == 0)
{
if(fragmentCurrency.getSymbol().equals(feeCurrency))
{
fees = amount * fees / 100;
}
else
{
fees = purchasedPrice * fees / 100;
}
}
}
return fees;
}
private boolean isFieldCorrectlyFilled(TextInputEditText editText, boolean displayError)
{
String purchasedPriceText = editText.getText().toString();
@ -210,7 +297,7 @@ public class BuyFragment extends CustomRecordFragment {
private void updateAdapter()
{
List<String> symbolStrings = new ArrayList<>();
symbolStrings = new ArrayList<>();
symbolStrings.addAll(PlaceholderManager.getFeeOptionsForSymbol(fragmentPair.getFrom(), getSecureContext()));
symbolStrings.addAll(PlaceholderManager.getFeeOptionsForSymbol(fragmentPair.getTo(), getSecureContext()));

View File

@ -55,7 +55,7 @@ public class TransactionListAdapter extends ArrayAdapter<Transaction> {
TextView dateTxtView = convertView.findViewById(R.id.purchaseDate);
amountTxtView.setText(String.valueOf(transaction.getAmount()));
valueTxtView.setText(numberConformer(transaction.getPurchasedPrice() * transaction.getAmount()));
valueTxtView.setText(numberConformer(transaction.getPurchasePrice() * transaction.getAmount()));
dateTxtView.setText(getDateFromTimestamp(transaction.getTimestamp()));
LinearLayout deleteLayout = convertView.findViewById(R.id.deleteTransactionLayout);

View File

@ -10,18 +10,29 @@ public class Transaction {
private String symbol;
private double amount;
private long timestamp;
private double purchasedPrice;
private double purchasePrice;
private double fees;
private boolean isMined;
private String note;
private String symPair;
private String feeCurrency;
private String source;
private String destination;
private String type;
public Transaction(int transactionId, String symbol, double amount, long timestamp, double purchasedPrice, double fees)
public Transaction(int transactionId, String symbol, String symPair, double amount, long timestamp, double purchasedPrice, double fees, String note, String feeCurrency, String source, String destination, String type)
{
this.transactionId = transactionId;
this.symbol = symbol;
this.symPair = symPair;
this.amount = amount;
this.timestamp = timestamp;
this.purchasedPrice = purchasedPrice;
this.purchasePrice = purchasedPrice;
this.fees = fees;
this.note = note;
this.feeCurrency = feeCurrency;
this.source = source;
this.destination = destination;
this.type = type;
}
public int getTransactionId() {
@ -53,14 +64,14 @@ public class Transaction {
this.amount = amount;
}
public void setPurchasedPrice(double purchasedPrice)
public void setPurchasePrice(double purchasedPrice)
{
this.purchasedPrice = purchasedPrice;
this.purchasePrice = purchasedPrice;
}
public double getPurchasedPrice()
public double getPurchasePrice()
{
return purchasedPrice;
return purchasePrice;
}
public double getFees() {
@ -70,4 +81,56 @@ public class Transaction {
public void setFees(double fees) {
this.fees = fees;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getSymPair() {
return symPair;
}
public void setSymPair(String symPair) {
this.symPair = symPair;
}
public String getFeeCurrency() {
return feeCurrency;
}
public void setFeeCurrency(String feeCurrency) {
this.feeCurrency = feeCurrency;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

View File

@ -27,7 +27,7 @@ import java.util.List;
public class DatabaseManager extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 14;
private static final int DATABASE_VERSION = 15;
private static final String DATABASE_NAME = "mdn.db";
@ -46,6 +46,7 @@ public class DatabaseManager extends SQLiteOpenHelper{
private static final String KEY_TRANSACTION_FEES = "fees";
private static final String KEY_TRANSACTION_FEE_CURRENCY = "feeCurrency";
private static final String KEY_TRANSACTION_NOTES = "notes";
private static final String KEY_TRANSACTION_TYPE = "transactionType";
private static final String KEY_EXCHANGE_ID = "idExchange";
private static final String KEY_EXCHANGE_NAME = "name";
@ -82,7 +83,8 @@ public class DatabaseManager extends SQLiteOpenHelper{
+ KEY_TRANSACTION_PAIR + " VARCHAR(4),"
+ KEY_TRANSACTION_FEE_CURRENCY + " VARCHAR(4),"
+ KEY_TRANSACTION_SOURCE + " TEXT,"
+ KEY_TRANSACTION_DESTINATION + " TEXT"
+ KEY_TRANSACTION_DESTINATION + " TEXT,"
+ KEY_TRANSACTION_TYPE + " VARCHAR(1)"
+ ");");
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_EXCHANGE_KEYS + "("
@ -146,7 +148,26 @@ public class DatabaseManager extends SQLiteOpenHelper{
return false;
}
public void addTransaction(String symbol, Double amount, Date date, Double purchasedPrice, Double fees, String note, String symbolFrom, String feeCurrency, String exchange)
public void updateTransactionWithId(int transactionId, double amount, Date date, double purchasedPrice, double fees, String note, String symbolFrom, String feeCurrency, String exchange, String type)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_TRANSACTION_AMOUNT, amount);
cv.put(KEY_TRANSACTION_DATE, date.getTime());
cv.put(KEY_TRANSACTION_PURCHASE_PRICE, purchasedPrice);
cv.put(KEY_TRANSACTION_FEES, fees);
cv.put(KEY_TRANSACTION_NOTES, note);
cv.put(KEY_TRANSACTION_PAIR, symbolFrom);
cv.put(KEY_TRANSACTION_FEE_CURRENCY, feeCurrency);
cv.put(KEY_TRANSACTION_SOURCE, exchange);
cv.put(KEY_TRANSACTION_TYPE, type);
db.update(TABLE_MANUAL_TRANSACTIONS, cv, KEY_TRANSACTION_ID + "=" + transactionId, null);
}
public void addTransaction(String symbol, Double amount, Date date, Double purchasePrice, double fees, String note, String symbolFrom, String feeCurrency, String exchange, String type)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
@ -154,12 +175,13 @@ public class DatabaseManager extends SQLiteOpenHelper{
values.put(KEY_TRANSACTION_SYMBOL, symbol);
values.put(KEY_TRANSACTION_AMOUNT, amount);
values.put(KEY_TRANSACTION_DATE, date.getTime());
values.put(KEY_TRANSACTION_PURCHASE_PRICE, purchasedPrice);
values.put(KEY_TRANSACTION_PURCHASE_PRICE, purchasePrice);
values.put(KEY_TRANSACTION_FEES, fees);
values.put(KEY_TRANSACTION_NOTES, note);
values.put(KEY_TRANSACTION_PAIR, symbolFrom);
values.put(KEY_TRANSACTION_FEE_CURRENCY, feeCurrency);
values.put(KEY_TRANSACTION_SOURCE, exchange);
values.put(KEY_TRANSACTION_TYPE, type);
db.insert(TABLE_MANUAL_TRANSACTIONS, null, values);
db.close();
@ -529,20 +551,6 @@ public class DatabaseManager extends SQLiteOpenHelper{
return currencyList;
}
public void updateTransactionWithId(int transactionId, double amount, Date time, double purchasedPrice, double fees)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_TRANSACTION_AMOUNT, amount);
cv.put(KEY_TRANSACTION_DATE, time.getTime());
cv.put(KEY_TRANSACTION_PURCHASE_PRICE, purchasedPrice);
cv.put(KEY_TRANSACTION_FEES, fees);
db.update(TABLE_MANUAL_TRANSACTIONS, cv, KEY_TRANSACTION_ID + "=" + transactionId, null);
}
public Transaction getCurrencyTransactionById(int id)
{
String searchQuerry = "SELECT * FROM " + TABLE_MANUAL_TRANSACTIONS + " WHERE " + KEY_TRANSACTION_ID + "='" + id + "'";
@ -555,10 +563,16 @@ public class DatabaseManager extends SQLiteOpenHelper{
{
transaction = new Transaction(resultatList.getInt(resultatList.getColumnIndex(KEY_TRANSACTION_ID))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_PAIR))
, resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT))
, resultatList.getLong(resultatList.getColumnIndex(KEY_TRANSACTION_DATE))
, resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_PURCHASE_PRICE))
, resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)));
, resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_NOTES))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_FEE_CURRENCY))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SOURCE))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_DESTINATION))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_TYPE)));
}
resultatList.close();
@ -580,10 +594,16 @@ public class DatabaseManager extends SQLiteOpenHelper{
{
transactionList.add(new Transaction(resultatList.getInt(resultatList.getColumnIndex(KEY_TRANSACTION_ID))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_PAIR))
, resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT))
, resultatList.getLong(resultatList.getColumnIndex(KEY_TRANSACTION_DATE))
, resultatList.getLong(resultatList.getColumnIndex(KEY_TRANSACTION_PURCHASE_PRICE))
, resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES))));
, resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_PURCHASE_PRICE))
, resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_NOTES))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_FEE_CURRENCY))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SOURCE))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_DESTINATION))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_TYPE))));
}
resultatList.close();