From e9e495b366b4622767d2d91f4e792a6418f3f732 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Mon, 21 May 2018 21:38:05 -0400 Subject: [PATCH] - Transaction search implemented - Now the transaction list print contact name when the address match with that contact --- .../crystalwallet/dao/TransactionDao.java | 26 +++-- .../fragments/NoneSecurityFragment.java | 32 +------ .../fragments/TransactionsFragment.java | 21 +++- .../models/CryptoCoinTransactionExtended.java | 96 +++++++++++++++++++ .../viewmodels/TransactionListViewModel.java | 21 ++-- .../views/TransactionListAdapter.java | 7 +- .../views/TransactionListView.java | 3 +- .../views/TransactionViewHolder.java | 25 +++-- app/src/main/res/layout/transaction_list.xml | 3 +- 9 files changed, 167 insertions(+), 67 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransactionExtended.java diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java index 46ab665..ce435fc 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java @@ -11,6 +11,7 @@ import java.util.Date; import java.util.List; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; +import cy.agorise.crystalwallet.models.CryptoCoinTransactionExtended; /** * Created by Henry Varona on 12/9/2017. @@ -18,23 +19,28 @@ import cy.agorise.crystalwallet.models.CryptoCoinTransaction; @Dao public interface TransactionDao { + static final String transactionsQuery = "SELECT cct.*, cna.name AS user_account_name, c.name AS contact_name FROM crypto_coin_transaction cct " + + "LEFT JOIN crypto_net_account cna ON cct.account_id = cna.id " + + "LEFT JOIN contact c ON c.id = (SELECT ca.contact_id FROM contact_address ca WHERE ca.address LIKE (CASE is_input WHEN 1 THEN cct.\"from\" ELSE cct.\"to\" END) LIMIT 1) " + + "WHERE user_account_name LIKE '%'||:search||'%' OR contact_name LIKE '%'||:search||'%' OR cct.\"from\" LIKE '%'||:search||'%' OR cct.\"to\" LIKE '%'||:search||'%'"; + @Query("SELECT * FROM crypto_coin_transaction") LiveData> getAll(); - @Query("SELECT * FROM crypto_coin_transaction ORDER BY date DESC") - LivePagedListProvider transactionsByDate(); + @Query(transactionsQuery + " ORDER BY date DESC") + LivePagedListProvider transactionsByDate(String search); - @Query("SELECT * FROM crypto_coin_transaction ORDER BY amount DESC") - LivePagedListProvider transactionsByAmount(); + @Query(transactionsQuery + " ORDER BY amount DESC") + LivePagedListProvider transactionsByAmount(String search); - @Query("SELECT * FROM crypto_coin_transaction ORDER BY is_input DESC") - LivePagedListProvider transactionsByIsInput(); + @Query(transactionsQuery + " ORDER BY is_input DESC") + LivePagedListProvider transactionsByIsInput(String search); - @Query("SELECT * FROM crypto_coin_transaction ORDER BY `from` DESC") - LivePagedListProvider transactionsByFrom(); + @Query(transactionsQuery + " ORDER BY `from` DESC") + LivePagedListProvider transactionsByFrom(String search); - @Query("SELECT * FROM crypto_coin_transaction ORDER BY `to` DESC") - LivePagedListProvider transactionsByTo(); + @Query(transactionsQuery + " ORDER BY `to` DESC") + LivePagedListProvider transactionsByTo(String search); @Query("SELECT * FROM crypto_coin_transaction WHERE account_id = :idAccount ORDER BY date DESC") LiveData> getByIdAccountLiveData(long idAccount); diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/NoneSecurityFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/NoneSecurityFragment.java index fc34e1e..090cfe2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/NoneSecurityFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/NoneSecurityFragment.java @@ -25,11 +25,6 @@ import cy.agorise.crystalwallet.viewmodels.validators.PinSecurityValidator; public class NoneSecurityFragment extends Fragment { - GeneralSetting passwordGeneralSetting = new GeneralSetting(); - GeneralSetting patternGeneralSetting = new GeneralSetting(); - - GeneralSettingListViewModel generalSettingListViewModel; - public NoneSecurityFragment() { // Required empty public constructor } @@ -48,24 +43,6 @@ public class NoneSecurityFragment extends Fragment { View v = inflater.inflate(R.layout.fragment_none_security, container, false); ButterKnife.bind(this, v); - generalSettingListViewModel = ViewModelProviders.of(this).get(GeneralSettingListViewModel.class); - LiveData> generalSettingsLiveData = generalSettingListViewModel.getGeneralSettingList(); - - generalSettingsLiveData.observe(this, new Observer>() { - @Override - public void onChanged(@Nullable List generalSettings) { - if (generalSettings != null){ - for (GeneralSetting generalSetting:generalSettings) { - if (generalSetting.getName().equals(GeneralSetting.SETTING_PASSWORD)){ - passwordGeneralSetting = generalSetting; - } else if (generalSetting.getName().equals(GeneralSetting.SETTING_PATTERN)){ - patternGeneralSetting = generalSetting; - } - } - } - } - }); - return v; } @@ -73,14 +50,7 @@ public class NoneSecurityFragment extends Fragment { public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { - setSecurityOff(); + CrystalSecurityMonitor.getInstance(null).clearSecurity(); } } - - public void setSecurityOff(){ - generalSettingListViewModel.deleteGeneralSettings(passwordGeneralSetting,patternGeneralSetting); - - CrystalSecurityMonitor.getInstance(null).setPasswordSecurity(""); - CrystalSecurityMonitor.getInstance(null).setPatternEncrypted(""); - } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/TransactionsFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/TransactionsFragment.java index be09626..83ce128 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/TransactionsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/TransactionsFragment.java @@ -9,10 +9,12 @@ import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v7.widget.RecyclerView; +import android.text.Editable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; +import android.widget.EditText; import android.widget.Spinner; import java.util.ArrayList; @@ -20,8 +22,10 @@ import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; +import butterknife.OnTextChanged; import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; +import cy.agorise.crystalwallet.models.CryptoCoinTransactionExtended; import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; import cy.agorise.crystalwallet.views.TransactionListView; import cy.agorise.crystalwallet.views.TransactionOrderSpinnerAdapter; @@ -34,12 +38,15 @@ public class TransactionsFragment extends Fragment { @BindView(R.id.spTransactionsOrder) Spinner spTransactionsOrder; + @BindView(R.id.etTransactionSearch) + EditText etTransactionSearch; + RecyclerView balanceRecyclerView; FloatingActionButton fabSend; FloatingActionButton fabReceive; TransactionListViewModel transactionListViewModel; - LiveData> transactionsLiveData; + LiveData> transactionsLiveData; public TransactionsFragment() { // Required empty public constructor @@ -108,18 +115,24 @@ public class TransactionsFragment extends Fragment { if (transactionsLiveData != null){ transactionsLiveData.removeObservers(this); } - transactionListViewModel.initTransactionList(orderSelected.getField()); + transactionListViewModel.initTransactionList(orderSelected.getField(),etTransactionSearch.getText().toString()); transactionsLiveData = transactionListViewModel.getTransactionList(); final Fragment fragment = this; - transactionsLiveData.observe(this, new Observer>() { + transactionsLiveData.observe(this, new Observer>() { @Override - public void onChanged(@Nullable PagedList cryptoCoinTransactions) { + public void onChanged(@Nullable PagedList cryptoCoinTransactions) { transactionListView.setData(cryptoCoinTransactions, fragment); } }); } + @OnTextChanged(value = R.id.etTransactionSearch, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterTransactionSearchChange(Editable editable) { + changeTransactionList(); + } + public void initTransactionsOrderSpinner(){ List spinnerValues = new ArrayList(); spinnerValues.add(new TransactionOrderSpinnerAdapter.TransactionOrderSpinnerItem("date","Date",0,false)); diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransactionExtended.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransactionExtended.java new file mode 100644 index 0000000..d41a55e --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransactionExtended.java @@ -0,0 +1,96 @@ +package cy.agorise.crystalwallet.models; + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Embedded; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.ForeignKey; +import android.arch.persistence.room.Ignore; +import android.arch.persistence.room.Index; +import android.arch.persistence.room.PrimaryKey; +import android.support.annotation.NonNull; +import android.support.v7.recyclerview.extensions.DiffCallback; + +import java.util.Date; + +/** + * Represents a generic CryptoNet Transaction + * + * Created by Henry Varona on 15/5/2018. + */ +@Entity +public class CryptoCoinTransactionExtended { + + @Embedded + public CryptoCoinTransaction cryptoCoinTransaction; + + @ColumnInfo(name="user_account_name") + public String userAccountName; + + @ColumnInfo(name="contact_name") + public String contactName; + + public String getUserAccountName(){ + return this.userAccountName; + } + + public String getContactName(){ + return this.contactName; + } + + public String getFrom() { return this.cryptoCoinTransaction.getFrom(); } + + public String getTo() { return this.cryptoCoinTransaction.getTo(); } + + public long getAccountId() { + return this.cryptoCoinTransaction.getAccountId(); + } + + public CryptoNetAccount getAccount() { + return this.cryptoCoinTransaction.getAccount(); + } + + public long getId() { + return this.cryptoCoinTransaction.getId(); + } + + public Date getDate() { + return this.cryptoCoinTransaction.getDate(); + } + + public boolean getInput() { + return this.cryptoCoinTransaction.getInput(); + } + + public boolean isConfirmed() { return this.cryptoCoinTransaction.isConfirmed(); } + + public long getAmount() { return this.cryptoCoinTransaction.getAmount(); } + + public int getIdCurrency() { return this.cryptoCoinTransaction.getIdCurrency(); } + + public static final DiffCallback DIFF_CALLBACK = new DiffCallback() { + @Override + public boolean areItemsTheSame( + @NonNull CryptoCoinTransactionExtended oldTransaction, @NonNull CryptoCoinTransactionExtended newTransaction) { + return oldTransaction.getId() == newTransaction.getId(); + } + @Override + public boolean areContentsTheSame( + @NonNull CryptoCoinTransactionExtended oldTransaction, @NonNull CryptoCoinTransactionExtended newTransaction) { + return oldTransaction.equals(newTransaction); + } + }; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + CryptoCoinTransactionExtended that = (CryptoCoinTransactionExtended) o; + + if (this.cryptoCoinTransaction != null ? !this.cryptoCoinTransaction.equals(that.cryptoCoinTransaction) : that.cryptoCoinTransaction != null) return false; + if (this.userAccountName != null ? !this.userAccountName.equals(that.userAccountName) : that.userAccountName != null) return false; + if (this.contactName != null ? !this.contactName.equals(that.contactName) : that.contactName != null) return false; + + return true; + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java index 5a0455f..4ea9600 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java @@ -12,6 +12,7 @@ import java.util.List; import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; +import cy.agorise.crystalwallet.models.CryptoCoinTransactionExtended; import cy.agorise.crystalwallet.views.TransactionListView; /** @@ -20,7 +21,7 @@ import cy.agorise.crystalwallet.views.TransactionListView; public class TransactionListViewModel extends AndroidViewModel { - private LiveData> transactionList; + private LiveData> transactionList; private CrystalDatabase db; public TransactionListViewModel(Application application) { @@ -35,25 +36,27 @@ public class TransactionListViewModel extends AndroidViewModel { );*/ } - public void initTransactionList(String orderField){ - LivePagedListProvider livePagedListProvider = null; + public void initTransactionList(String orderField, String search){ + LivePagedListProvider livePagedListProvider = null; switch (orderField){ case "date": - livePagedListProvider = this.db.transactionDao().transactionsByDate(); + livePagedListProvider = this.db.transactionDao().transactionsByDate(search); break; case "amount": - livePagedListProvider = this.db.transactionDao().transactionsByAmount(); + livePagedListProvider = this.db.transactionDao().transactionsByAmount(search); break; case "is_input": - livePagedListProvider = this.db.transactionDao().transactionsByIsInput(); + livePagedListProvider = this.db.transactionDao().transactionsByIsInput(search); break; case "from": - livePagedListProvider = this.db.transactionDao().transactionsByFrom(); + livePagedListProvider = this.db.transactionDao().transactionsByFrom(search); break; case "to": - livePagedListProvider = this.db.transactionDao().transactionsByTo(); + livePagedListProvider = this.db.transactionDao().transactionsByTo(search); break; + default: + livePagedListProvider = this.db.transactionDao().transactionsByDate(search); } if (livePagedListProvider != null) { this.transactionList = livePagedListProvider.create(0, @@ -68,7 +71,7 @@ public class TransactionListViewModel extends AndroidViewModel { } } - public LiveData> getTransactionList(){ + public LiveData> getTransactionList(){ return this.transactionList; } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java index bc0abb3..56742f1 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java @@ -19,6 +19,7 @@ import java.util.List; import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; +import cy.agorise.crystalwallet.models.CryptoCoinTransactionExtended; /** * Created by Henry Varona on 11/9/2017. @@ -29,12 +30,12 @@ import cy.agorise.crystalwallet.models.CryptoCoinTransaction; * that will be extended with the scroll of the user */ -public class TransactionListAdapter extends PagedListAdapter { +public class TransactionListAdapter extends PagedListAdapter { Fragment fragment; public TransactionListAdapter(Fragment fragment) { - super(CryptoCoinTransaction.DIFF_CALLBACK); + super(CryptoCoinTransactionExtended.DIFF_CALLBACK); this.fragment = fragment; } @@ -48,7 +49,7 @@ public class TransactionListAdapter extends PagedListAdapter data, Fragment fragment){ + public void setData(PagedList data, Fragment fragment){ //Initializes the adapter of the transaction list if (this.listAdapter == null) { this.listAdapter = new TransactionListAdapter(fragment); diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java index ad10bc9..a8ca0d0 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java @@ -21,6 +21,7 @@ import java.util.TimeZone; import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.activities.CryptoCoinTransactionReceiptActivity; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; +import cy.agorise.crystalwallet.models.CryptoCoinTransactionExtended; import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.GeneralSetting; @@ -112,7 +113,7 @@ public class TransactionViewHolder extends RecyclerView.ViewHolder { /* * Binds a transaction object with this element view */ - public void bindTo(final CryptoCoinTransaction transaction/*, final OnTransactionClickListener listener*/) { + public void bindTo(final CryptoCoinTransactionExtended transaction/*, final OnTransactionClickListener listener*/) { if (transaction == null){ clear(); } else { @@ -147,16 +148,24 @@ public class TransactionViewHolder extends RecyclerView.ViewHolder { LiveData cryptoNetAccountLiveData = cryptoNetAccountViewModel.getCryptoNetAccount(); - cryptoNetAccountLiveData.observe(this.fragment, new Observer() { - @Override - public void onChanged(@Nullable CryptoNetAccount cryptoNetAccount) { + //cryptoNetAccountLiveData.observe(this.fragment, new Observer() { + // @Override + // public void onChanged(@Nullable CryptoNetAccount cryptoNetAccount) { if (transaction.getInput()){ - tvTo.setText(cryptoNetAccount.getName()); + tvTo.setText(transaction.getUserAccountName()); + + if ((transaction.getContactName() != null)&&(!transaction.equals(""))){ + tvFrom.setText(transaction.getContactName()); + } } else { - tvFrom.setText(cryptoNetAccount.getName()); + tvFrom.setText(transaction.getUserAccountName()); + + if ((transaction.getContactName() != null)&&(!transaction.equals(""))){ + tvTo.setText(transaction.getContactName()); + } } - } - }); + // } + //}); String finalAmountText = ""; if (transaction.getInput()) { diff --git a/app/src/main/res/layout/transaction_list.xml b/app/src/main/res/layout/transaction_list.xml index 1397df5..0bb0289 100644 --- a/app/src/main/res/layout/transaction_list.xml +++ b/app/src/main/res/layout/transaction_list.xml @@ -26,7 +26,8 @@ android:layout_width="40dip" android:layout_height="40dip" android:layout_alignParentTop="true" - android:layout_toStartOf="@+id/spTransactionsOrder" /> + android:layout_toStartOf="@+id/spTransactionsOrder" + android:visibility="gone" />