From f907a4bdb17ce33bc6ba4ba4097eea4b484ca270 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sun, 19 Aug 2018 02:48:52 +0200 Subject: [PATCH] Update transfer tab in RecordTransactionActivity - Add backend code to the Transfer tab TODO : - Support for %/fixed fees for transfers - Edition support for transfers --- .idea/assetWizardSettings.xml | 50 +++- .../TransferFragment.java | 280 +++++++++++++++++- .../moodl/DataManagers/DatabaseManager.java | 49 ++- .../record_transaction_fragment_transfer.xml | 86 ++++-- 4 files changed, 427 insertions(+), 38 deletions(-) diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml index 943e267..ddc44c0 100644 --- a/.idea/assetWizardSettings.xml +++ b/.idea/assetWizardSettings.xml @@ -5,7 +5,55 @@ - + + + diff --git a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/TransferFragment.java b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/TransferFragment.java index 62ee88c..73dc8b0 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/TransferFragment.java +++ b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/TransferFragment.java @@ -1,58 +1,138 @@ package com.herbron.moodl.Activities.RecordTransactionFragments; import android.app.DatePickerDialog; +import android.content.Intent; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.TextInputEditText; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.ArrayAdapter; +import android.widget.Button; import android.widget.DatePicker; import android.widget.Spinner; import android.widget.TimePicker; 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.R; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Calendar; +import java.util.HashSet; import java.util.Locale; +import java.util.Set; public class TransferFragment extends CustomRecordFragment { private static Currency fragmentCurrency; + private static Exchange fragmentExchange; private Spinner fromSpinner; private Spinner toSpinner; + private Spinner feeSpinner; + + private Button saveButton; + private int transactionId; + private Transaction transaction; private TextInputEditText transferDateEditText; + private TextInputEditText amountEditText; + private TextInputEditText feesEditText; + private TextInputEditText noteEditText; + private SimpleDateFormat sdf; private Calendar calendar; private View view; + public static final String EXCHANGE_CODE = "stra:e"; + public static final String WALLET_CODE = "stra:mw"; + public static final String MINING_CODE = "stra:m"; + public static final String ELSE_WALLET_CODE = "stra:smew"; + public static final String AIRDROP_CODE = "stra:a"; + public static final String UNKNOWN_CODE = "stra:unk"; + public static final String FORK_CODE = "stra:fo"; + + private View.OnClickListener saveButtonClickListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + if(isFieldCorrectlyFilled(amountEditText, true)) + { + if(isTransactionPossible()) + { + DatabaseManager databaseManager = new DatabaseManager(getContext()); + double amount = Double.valueOf(amountEditText.getText().toString()); + double fees = getFees(); + + databaseManager.addTransaction(fragmentCurrency.getSymbol() + , amount + , calendar.getTime() + , 0 + , fees + , noteEditText.getText().toString() + , "" + , fragmentCurrency.getSymbol() + , getDestination() + , getSource() + , "t" + , feeSpinner.getSelectedItemPosition() == 0 ? "p" : "f"); + + getActivity().finish(); + } + else + { + Drawable backgroundDrawableTo = toSpinner.getBackground(); + backgroundDrawableTo.mutate(); + backgroundDrawableTo.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.error), PorterDuff.Mode.SRC_ATOP)); + backgroundDrawableTo.invalidateSelf(); + + Drawable backgroundDrawableFrom = fromSpinner.getBackground(); + backgroundDrawableFrom.mutate(); + backgroundDrawableFrom.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.error), PorterDuff.Mode.SRC_ATOP)); + backgroundDrawableFrom.invalidateSelf(); + + view.findViewById(R.id.errorLayouts).setVisibility(View.VISIBLE); + } + } + } + }; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.record_transaction_fragment_transfer, container, false); calendar = Calendar.getInstance(); - sdf = new SimpleDateFormat(" HH:mm dd/MM/yyyy", Locale.UK); + sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy", Locale.UK); + initializeViewElements(); + + return view; + } + + private void initializeViewElements() + { fromSpinner = view.findViewById(R.id.from_transfer_spinner); toSpinner = view.findViewById(R.id.to_transfer_spinner); + feeSpinner = view.findViewById(R.id.feesFormat_editText_transfer); - ArrayAdapter fromAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.from_transfer_options_string_array)); - fromAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - ArrayAdapter toAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.to_transfer_options_string_array)); - toAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - - fromSpinner.setAdapter(fromAdapter); - toSpinner.setAdapter(toAdapter); + setupSpinnesr(); + feesEditText = view.findViewById(R.id.fees_editText_transfer); + noteEditText = view.findViewById(R.id.note_editText_transfer); + amountEditText = view.findViewById(R.id.amount_editText_transfer); transferDateEditText = view.findViewById(R.id.transfertDate_editText); transferDateEditText.setText(sdf.format(calendar.getTime())); @@ -63,7 +143,187 @@ public class TransferFragment extends CustomRecordFragment { } }); - return view; + saveButton = view.findViewById(R.id.saveTransferButton); + saveButton.setOnClickListener(saveButtonClickListener); + + checkCallingIntent(); + } + + private void checkCallingIntent() + { + Intent intent = getActivity().getIntent(); + transactionId = intent.getIntExtra("transactionId", -1); + + if(transactionId != -1) + { + DatabaseManager databaseManager = new DatabaseManager(getContext()); + transaction = databaseManager.getCurrencyTransactionById(transactionId); + + if(transaction.getType().equals("t")) + { + fillFields(); + } + } + } + + private void fillFields() + { + amountEditText.setText(String.valueOf(transaction.getAmount())); + //Fill other fields + } + + private void setupSpinnesr() + { + ArrayAdapter fromAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.from_transfer_options_string_array)); + fromAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + fromSpinner.setAdapter(fromAdapter); + + ArrayAdapter toAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.to_transfer_options_string_array)); + toAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + toSpinner.setAdapter(toAdapter); + + ArrayAdapter feeAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.fees_options)); + feeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + feeSpinner.setAdapter(feeAdapter); + + fromSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View v, int position, long id) { + Drawable backgroundDrawableFrom = fromSpinner.getBackground(); + backgroundDrawableFrom.mutate(); + backgroundDrawableFrom.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.separationColor), PorterDuff.Mode.SRC_ATOP)); + backgroundDrawableFrom.invalidateSelf(); + + view.findViewById(R.id.errorLayouts).setVisibility(View.INVISIBLE); + } + + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + + toSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View v, int position, long id) { + Drawable backgroundDrawableTo = toSpinner.getBackground(); + backgroundDrawableTo.mutate(); + backgroundDrawableTo.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.separationColor), PorterDuff.Mode.SRC_ATOP)); + backgroundDrawableTo.invalidateSelf(); + + view.findViewById(R.id.errorLayouts).setVisibility(View.INVISIBLE); + } + + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + } + + private double getFees() + { + double fees = 0; + + if(!feesEditText.getText().toString().equals("")) + { + fees = Double.parseDouble(feesEditText.getText().toString()); + } + + return fees; + } + + private boolean isTransactionPossible() + { + Set conflictFrom = new HashSet<>(Arrays.asList(2, 3, 4, 5, 6)); + Set conflictTo = new HashSet<>(Arrays.asList(2, 3)); + + return !(conflictFrom.contains(fromSpinner.getSelectedItemPosition()) && conflictTo.contains(toSpinner.getSelectedItemPosition())); + } + + private String getDestination() + { + String destination = ""; + + switch (toSpinner.getSelectedItemPosition()) + { + case 0: + destination = EXCHANGE_CODE; + break; + case 1: + destination = WALLET_CODE; + break; + case 2: + destination = ELSE_WALLET_CODE; + break; + case 3: + destination = UNKNOWN_CODE; + break; + } + + return destination; + } + + private String getSource() + { + String source = ""; + + switch (fromSpinner.getSelectedItemPosition()) + { + case 0: + source = EXCHANGE_CODE; + break; + case 1: + source = WALLET_CODE; + break; + case 2: + source = MINING_CODE; + break; + case 3: + source = ELSE_WALLET_CODE; + break; + case 4: + source = AIRDROP_CODE; + break; + case 5: + source = UNKNOWN_CODE; + break; + case 6: + source = FORK_CODE; + break; + } + + return source; + } + + private boolean isFieldCorrectlyFilled(TextInputEditText editText, boolean displayError) + { + String purchasedPriceText = editText.getText().toString(); + double purchasedPrice; + + try { + purchasedPrice = Double.parseDouble(purchasedPriceText); + + if(purchasedPrice < 0) + { + if(displayError) editText.setError(getResources().getString(R.string.field_negative)); + + return false; + } + } catch (NumberFormatException e) { + if(displayError) editText.setError(getResources().getString(R.string.field_nan)); + + return false; + } + + if(purchasedPriceText.equals("")) + { + if(displayError) editText.setError(getResources().getString(R.string.field_empty)); + + return false; + } + + return true; } private void createDatePicker() @@ -114,7 +374,7 @@ public class TransferFragment extends CustomRecordFragment { @Override public void onExchangeUpdated() { - + fragmentExchange = exchange; } @Override diff --git a/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java b/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java index c1574e3..f09b966 100644 --- a/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java +++ b/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java @@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; +import com.herbron.moodl.Activities.RecordTransactionFragments.TransferFragment; import com.herbron.moodl.DataManagers.CurrencyData.Currency; import com.herbron.moodl.DataManagers.CurrencyData.Transaction; import com.herbron.moodl.DataManagers.ExchangeManager.BinanceManager; @@ -18,8 +19,11 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * Created by Guitoune on 14/01/2018. @@ -150,7 +154,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, String feeFormat) + public void updateTransactionWithId(int transactionId, double amount, Date date, double purchasedPrice, double fees, String note, String symbolFrom, String feeCurrency, String destination, String source, String type, String feeFormat) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues cv = new ContentValues(); @@ -162,7 +166,8 @@ public class DatabaseManager extends SQLiteOpenHelper{ 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_SOURCE, source); + cv.put(KEY_TRANSACTION_DESTINATION, destination); cv.put(KEY_TRANSACTION_TYPE, type); cv.put(KEY_TRANSACTION_FEE_FORMAT, feeFormat); @@ -170,7 +175,7 @@ public class DatabaseManager extends SQLiteOpenHelper{ } - 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) + public void addTransaction(String symbol, Double amount, Date date, double purchasePrice, double fees, String note, String symbolFrom, String feeCurrency, String destination, String source, String type, String feeFormat) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); @@ -183,11 +188,12 @@ public class DatabaseManager extends SQLiteOpenHelper{ 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_SOURCE, source); + values.put(KEY_TRANSACTION_DESTINATION, destination); values.put(KEY_TRANSACTION_TYPE, type); values.put(KEY_TRANSACTION_FEE_FORMAT, feeFormat); - db.insert(TABLE_MANUAL_TRANSACTIONS, null, values); + Log.d("moodl", "Insert result " + db.insert(TABLE_MANUAL_TRANSACTIONS, null, values)); db.close(); } @@ -545,7 +551,7 @@ public class DatabaseManager extends SQLiteOpenHelper{ return accountList; } - public List getAllCurrenciesFromManualCurrency() + public List getAllCurrenciesFromTransactions() { String searchQuerry = "SELECT * FROM " + TABLE_MANUAL_TRANSACTIONS; SQLiteDatabase db = this.getWritableDatabase(); @@ -559,7 +565,7 @@ public class DatabaseManager extends SQLiteOpenHelper{ String feeSym = resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_FEE_CURRENCY)); String type = resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_TYPE)); - if(type != null) + if(type != null && !type.equals("")) { switch (type) { @@ -579,7 +585,7 @@ public class DatabaseManager extends SQLiteOpenHelper{ if(symbol.equals(feeSym)) { currencyList.add(new Currency(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) - , - resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT)) + resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); + , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT)) + resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); } else { @@ -588,6 +594,27 @@ public class DatabaseManager extends SQLiteOpenHelper{ } break; case "t": + if(isBalanceRelated(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SOURCE))) && isBalanceRelated(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_DESTINATION)))) + { + currencyList.add(new Currency(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) + , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); + } + else + { + if(isBalanceRelated(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SOURCE)))) + { + currencyList.add(new Currency(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) + , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT)) + resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); + } + else + { + if(isBalanceRelated(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_DESTINATION)))) + { + currencyList.add(new Currency(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) + , resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT)) - resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); + } + } + } break; } } @@ -613,6 +640,12 @@ public class DatabaseManager extends SQLiteOpenHelper{ return currencyList; } + private boolean isBalanceRelated(String str) + { + Set set = new HashSet<>(Arrays.asList(TransferFragment.EXCHANGE_CODE, TransferFragment.WALLET_CODE)); + return set.contains(str); + } + public Transaction getCurrencyTransactionById(int id) { String searchQuerry = "SELECT * FROM " + TABLE_MANUAL_TRANSACTIONS + " WHERE " + KEY_TRANSACTION_ID + "='" + id + "'"; diff --git a/app/src/main/res/layout/record_transaction_fragment_transfer.xml b/app/src/main/res/layout/record_transaction_fragment_transfer.xml index 45721c2..0a6164b 100644 --- a/app/src/main/res/layout/record_transaction_fragment_transfer.xml +++ b/app/src/main/res/layout/record_transaction_fragment_transfer.xml @@ -2,6 +2,7 @@ + android:layout_marginBottom="8dp" + android:background="@drawable/spinner_background" + android:spinnerMode="dropdown"/> + android:layout_marginBottom="8dp" + android:background="@drawable/spinner_background"/> - + android:layout_height="wrap_content" + android:orientation="horizontal"> - + android:layout_weight="0.7"> - + + + + + + + - + android:layout_gravity="end"> + + + + + + + + + + + +