Fix fee format when editing a transaction

This commit is contained in:
Tanguy Herbron 2018-08-15 22:39:12 +02:00
parent 4bdffe22c4
commit e308e962d7
5 changed files with 81 additions and 16 deletions

View File

@ -32,6 +32,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import com.herbron.moodl.Activities.RecordTransactionFragments.BuyFragment;
import com.herbron.moodl.CurrencyInfoUpdateNotifierInterface;
import com.herbron.moodl.CustomAdapters.PairRecordListAdapter;
import com.herbron.moodl.CustomLayouts.CustomRecordFragment;
@ -227,6 +228,8 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
pair_autoCompleteTextView.setText(PlaceholderManager.getPairString(pair.getFrom(), pair.getTo(), getBaseContext()));
pair_autoCompleteTextView.setEnabled(true);
((BuyFragment) pageAdapter.getItem(0)).updatePair(pair);
updatePairData();
found = true;

View File

@ -63,6 +63,7 @@ public class BuyFragment extends CustomRecordFragment {
private List<String> symbolStrings;
private int transactionId;
private Transaction transaction;
@Nullable
@Override
@ -78,8 +79,6 @@ public class BuyFragment extends CustomRecordFragment {
initializeViewElements();
checkCallingIntent();
return view;
}
@ -91,16 +90,16 @@ public class BuyFragment extends CustomRecordFragment {
if(transactionId != -1)
{
DatabaseManager databaseManager = new DatabaseManager(context);
Transaction transaction = databaseManager.getCurrencyTransactionById(transactionId);
transaction = databaseManager.getCurrencyTransactionById(transactionId);
if(transaction.getType().equals("b"))
{
fillFields(transaction);
fillFields();
}
}
}
private void fillFields(Transaction transaction)
private void fillFields()
{
amoutEditText.setText(String.valueOf(transaction.getAmount()));
buyPriceEditText.setText(String.valueOf(transaction.getPurchasePrice()));
@ -109,6 +108,31 @@ public class BuyFragment extends CustomRecordFragment {
totalValueEditText.setText(String.valueOf(transaction.getAmount() * transaction.getPurchasePrice()));
fees_editText.setText(String.valueOf(transaction.getFees()));
note_editText.setText(transaction.getNote());
Log.d("moodl", "> " + fragmentPair);
/*if(transaction.getSymbol().equals(fragmentPair.getFrom()))
{
if(transaction.getFeeFormat().equals("p"))
{
feesCurrencySpinner.setSelection(0);
}
else
{
feesCurrencySpinner.setSelection(1);
}
}
else
{
if(transaction.getFeeFormat().equals("p"))
{
feesCurrencySpinner.setSelection(2);
}
else
{
feesCurrencySpinner.setSelection(3);
}
}*/
}
private void initializeViewElements()
@ -208,7 +232,8 @@ public class BuyFragment extends CustomRecordFragment {
, fragmentPair.getFrom().equals(fragmentCurrency.getSymbol()) ? fragmentPair.getTo() : fragmentPair.getFrom()
, feeCurrency
, fragmentExchange.getName()
, "b");
, "b"
, feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f");
Intent intent = new Intent(getActivity(), HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
@ -225,7 +250,8 @@ public class BuyFragment extends CustomRecordFragment {
, fragmentPair.getFrom().equals(fragmentCurrency.getSymbol()) ? fragmentPair.getTo() : fragmentPair.getFrom()
, feeCurrency
, fragmentExchange.getName()
, "b");
, "b"
, feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f");
}
getActivity().finish();
@ -235,6 +261,8 @@ public class BuyFragment extends CustomRecordFragment {
fees_editText = view.findViewById(R.id.fees_editText);
note_editText = view.findViewById(R.id.note_editText);
checkCallingIntent();
}
private double getFees(String feeCurrency, double amount, double purchasedPrice)
@ -367,6 +395,19 @@ public class BuyFragment extends CustomRecordFragment {
fragmentExchange = exchange;
}
public void updatePair(Pair pair)
{
currencyAdapter = new ArrayAdapter<String>(getSecureContext(), android.R.layout.simple_spinner_item, new ArrayList<>());
currencyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
feesCurrencySpinner.setAdapter(currencyAdapter);
symbolStrings = new ArrayList<>();
symbolStrings.addAll(PlaceholderManager.getFeeOptionsForSymbol(pair.getFrom(), getSecureContext()));
symbolStrings.addAll(PlaceholderManager.getFeeOptionsForSymbol(pair.getTo(), getSecureContext()));
currencyAdapter.addAll(symbolStrings);
currencyAdapter.notifyDataSetChanged();
}
@Override
public void onPairUpdated() {
fragmentPair = pair;
@ -393,7 +434,7 @@ public class BuyFragment extends CustomRecordFragment {
calendar = Calendar.getInstance();
}
fragmentCurrency.getTimestampPrice(getSecureContext(), preferenceManager.getDefaultCurrency(), calendar.getTimeInMillis() / 1000);
fragmentCurrency.getTimestampPrice(getSecureContext(), fragmentCurrency.getSymbol().equals(fragmentPair.getFrom()) ? fragmentPair.getTo() : fragmentPair.getFrom(), calendar.getTimeInMillis() / 1000);
}
}

View File

@ -12,14 +12,15 @@ public class Transaction {
private long timestamp;
private double purchasePrice;
private double fees;
private String feeCurrency;
private String feeFormat;
private String note;
private String symPair;
private String feeCurrency;
private String source;
private String destination;
private String type;
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)
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, String feeFormat)
{
this.transactionId = transactionId;
this.symbol = symbol;
@ -33,6 +34,7 @@ public class Transaction {
this.source = source;
this.destination = destination;
this.type = type;
this.feeFormat = feeFormat;
}
public int getTransactionId() {
@ -133,4 +135,12 @@ public class Transaction {
public void setType(String type) {
this.type = type;
}
public String getFeeFormat() {
return feeFormat;
}
public void setFeeFormat(String feeFormat) {
this.feeFormat = feeFormat;
}
}

View File

@ -27,7 +27,7 @@ import java.util.List;
public class DatabaseManager extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 15;
private static final int DATABASE_VERSION = 16;
private static final String DATABASE_NAME = "mdn.db";
@ -45,6 +45,7 @@ public class DatabaseManager extends SQLiteOpenHelper{
private static final String KEY_TRANSACTION_DESTINATION = "destination";
private static final String KEY_TRANSACTION_FEES = "fees";
private static final String KEY_TRANSACTION_FEE_CURRENCY = "feeCurrency";
private static final String KEY_TRANSACTION_FEE_FORMAT = "feeFormat";
private static final String KEY_TRANSACTION_NOTES = "notes";
private static final String KEY_TRANSACTION_TYPE = "transactionType";
@ -78,10 +79,11 @@ public class DatabaseManager extends SQLiteOpenHelper{
+ KEY_TRANSACTION_AMOUNT + " TEXT,"
+ KEY_TRANSACTION_DATE + " TEXT,"
+ KEY_TRANSACTION_PURCHASE_PRICE + " REAL,"
+ KEY_TRANSACTION_FEES + " REAL,"
+ KEY_TRANSACTION_NOTES + " TEXT,"
+ KEY_TRANSACTION_PAIR + " VARCHAR(4),"
+ KEY_TRANSACTION_FEE_CURRENCY + " VARCHAR(4),"
+ KEY_TRANSACTION_FEES + " REAL,"
+ KEY_TRANSACTION_FEE_FORMAT + " VARCHAT(1),"
+ KEY_TRANSACTION_SOURCE + " TEXT,"
+ KEY_TRANSACTION_DESTINATION + " TEXT,"
+ KEY_TRANSACTION_TYPE + " VARCHAR(1)"
@ -148,7 +150,7 @@ public class DatabaseManager extends SQLiteOpenHelper{
return false;
}
public void updateTransactionWithId(int transactionId, double amount, Date date, double purchasedPrice, double fees, String note, String symbolFrom, String feeCurrency, String exchange, String type)
public void updateTransactionWithId(int transactionId, double amount, Date date, double purchasedPrice, double fees, String note, String symbolFrom, String feeCurrency, String exchange, String type, String feeFormat)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
@ -162,12 +164,13 @@ public class DatabaseManager extends SQLiteOpenHelper{
cv.put(KEY_TRANSACTION_FEE_CURRENCY, feeCurrency);
cv.put(KEY_TRANSACTION_SOURCE, exchange);
cv.put(KEY_TRANSACTION_TYPE, type);
cv.put(KEY_TRANSACTION_FEE_FORMAT, feeFormat);
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)
public void addTransaction(String symbol, Double amount, Date date, Double purchasePrice, double fees, String note, String symbolFrom, String feeCurrency, String exchange, String type, String feeFormat)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
@ -182,6 +185,7 @@ public class DatabaseManager extends SQLiteOpenHelper{
values.put(KEY_TRANSACTION_FEE_CURRENCY, feeCurrency);
values.put(KEY_TRANSACTION_SOURCE, exchange);
values.put(KEY_TRANSACTION_TYPE, type);
values.put(KEY_TRANSACTION_FEE_FORMAT, feeFormat);
db.insert(TABLE_MANUAL_TRANSACTIONS, null, values);
db.close();
@ -572,7 +576,8 @@ public class DatabaseManager extends SQLiteOpenHelper{
, 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.getString(resultatList.getColumnIndex(KEY_TRANSACTION_TYPE))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_FEE_FORMAT)));
}
resultatList.close();
@ -603,7 +608,8 @@ public class DatabaseManager extends SQLiteOpenHelper{
, 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.getString(resultatList.getColumnIndex(KEY_TRANSACTION_TYPE))
, resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_FEE_FORMAT))));
}
resultatList.close();

View File

@ -33,4 +33,9 @@ public class Pair {
{
return to;
}
@Override
public String toString() {
return from + "/" + to;
}
}