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 151b9f9..46ab665 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java @@ -24,6 +24,18 @@ public interface TransactionDao { @Query("SELECT * FROM crypto_coin_transaction ORDER BY date DESC") LivePagedListProvider transactionsByDate(); + @Query("SELECT * FROM crypto_coin_transaction ORDER BY amount DESC") + LivePagedListProvider transactionsByAmount(); + + @Query("SELECT * FROM crypto_coin_transaction ORDER BY is_input DESC") + LivePagedListProvider transactionsByIsInput(); + + @Query("SELECT * FROM crypto_coin_transaction ORDER BY `from` DESC") + LivePagedListProvider transactionsByFrom(); + + @Query("SELECT * FROM crypto_coin_transaction ORDER BY `to` DESC") + LivePagedListProvider transactionsByTo(); + @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/ImportAccountOptionsFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/ImportAccountOptionsFragment.java index 5a8ac5b..f4ae3da 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/ImportAccountOptionsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/ImportAccountOptionsFragment.java @@ -22,6 +22,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.activities.IntroActivity; import cy.agorise.crystalwallet.requestmanagers.FileServiceRequestListener; import cy.agorise.crystalwallet.requestmanagers.FileServiceRequests; import cy.agorise.crystalwallet.requestmanagers.ImportBackupRequest; @@ -127,15 +128,24 @@ public class ImportAccountOptionsFragment extends DialogFragment { importBackupRequest.setListener(new FileServiceRequestListener() { @Override public void onCarryOut() { - if (importBackupRequest.getStatus() == ImportBackupRequest.StatusCode.SUCCEEDED){ - Toast toast = Toast.makeText( - getContext(), "Backup restored!", Toast.LENGTH_LONG); - toast.show(); - } else if (importBackupRequest.getStatus() == ImportBackupRequest.StatusCode.FAILED){ - Toast toast = Toast.makeText( - getContext(), "An error ocurred while restoring the backup!", Toast.LENGTH_LONG); - toast.show(); - } + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + if (importBackupRequest.getStatus() == ImportBackupRequest.StatusCode.SUCCEEDED) { + Toast toast = Toast.makeText( + getContext(), "Backup restored!", Toast.LENGTH_LONG); + toast.show(); + + Intent intent = new Intent(getContext(), IntroActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + startActivity(intent); + } else if (importBackupRequest.getStatus() == ImportBackupRequest.StatusCode.FAILED) { + Toast toast = Toast.makeText( + getContext(), "An error ocurred while restoring the backup!", Toast.LENGTH_LONG); + toast.show(); + } + } + }); } }); 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 fadbe25..be09626 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/TransactionsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/TransactionsFragment.java @@ -12,7 +12,10 @@ import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.Spinner; +import java.util.ArrayList; import java.util.List; import butterknife.BindView; @@ -21,16 +24,23 @@ import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; import cy.agorise.crystalwallet.views.TransactionListView; +import cy.agorise.crystalwallet.views.TransactionOrderSpinnerAdapter; public class TransactionsFragment extends Fragment { @BindView(R.id.vTransactionListView) TransactionListView transactionListView; + @BindView(R.id.spTransactionsOrder) + Spinner spTransactionsOrder; + RecyclerView balanceRecyclerView; FloatingActionButton fabSend; FloatingActionButton fabReceive; + TransactionListViewModel transactionListViewModel; + LiveData> transactionsLiveData; + public TransactionsFragment() { // Required empty public constructor } @@ -83,8 +93,23 @@ public class TransactionsFragment extends Fragment { } }); - TransactionListViewModel transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class); - LiveData> transactionsLiveData = transactionListViewModel.getTransactionList(); + transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class); + + + initTransactionsOrderSpinner(); + changeTransactionList(); + return view; + } + + public void changeTransactionList(){ + TransactionOrderSpinnerAdapter.TransactionOrderSpinnerItem orderSelected = + (TransactionOrderSpinnerAdapter.TransactionOrderSpinnerItem)(spTransactionsOrder.getSelectedItem()); + + if (transactionsLiveData != null){ + transactionsLiveData.removeObservers(this); + } + transactionListViewModel.initTransactionList(orderSelected.getField()); + transactionsLiveData = transactionListViewModel.getTransactionList(); final Fragment fragment = this; transactionsLiveData.observe(this, new Observer>() { @@ -93,7 +118,32 @@ public class TransactionsFragment extends Fragment { transactionListView.setData(cryptoCoinTransactions, fragment); } }); + } - return view; + public void initTransactionsOrderSpinner(){ + List spinnerValues = new ArrayList(); + spinnerValues.add(new TransactionOrderSpinnerAdapter.TransactionOrderSpinnerItem("date","Date",0,false)); + spinnerValues.add(new TransactionOrderSpinnerAdapter.TransactionOrderSpinnerItem("amount","Amount",0,false)); + spinnerValues.add(new TransactionOrderSpinnerAdapter.TransactionOrderSpinnerItem("is_input","In/Out",0,false)); + spinnerValues.add(new TransactionOrderSpinnerAdapter.TransactionOrderSpinnerItem("from","From",0,false)); + spinnerValues.add(new TransactionOrderSpinnerAdapter.TransactionOrderSpinnerItem("to","To",0,false)); + + TransactionOrderSpinnerAdapter transactionOrderSpinnerAdapter = + new TransactionOrderSpinnerAdapter( + getContext(), android.R.layout.simple_spinner_item,spinnerValues + ); + spTransactionsOrder.setAdapter(transactionOrderSpinnerAdapter); + + spTransactionsOrder.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapterView, View view, int i, long l) { + changeTransactionList(); + } + + @Override + public void onNothingSelected(AdapterView adapterView) { + + } + }); } } 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 67449a9..5a0455f 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java @@ -4,6 +4,7 @@ import android.app.Application; import android.arch.lifecycle.AndroidViewModel; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.ViewModel; +import android.arch.paging.LivePagedListProvider; import android.arch.paging.PagedList; import android.content.Context; @@ -25,13 +26,46 @@ public class TransactionListViewModel extends AndroidViewModel { public TransactionListViewModel(Application application) { super(application); this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); - transactionList = this.db.transactionDao().transactionsByDate().create(0, + /*transactionList = this.db.transactionDao().transactionsByDate().create(0, new PagedList.Config.Builder() .setEnablePlaceholders(true) .setPageSize(10) .setPrefetchDistance(10) .build() - ); + );*/ + } + + public void initTransactionList(String orderField){ + LivePagedListProvider livePagedListProvider = null; + + switch (orderField){ + case "date": + livePagedListProvider = this.db.transactionDao().transactionsByDate(); + break; + case "amount": + livePagedListProvider = this.db.transactionDao().transactionsByAmount(); + break; + case "is_input": + livePagedListProvider = this.db.transactionDao().transactionsByIsInput(); + break; + case "from": + livePagedListProvider = this.db.transactionDao().transactionsByFrom(); + break; + case "to": + livePagedListProvider = this.db.transactionDao().transactionsByTo(); + break; + } + if (livePagedListProvider != null) { + this.transactionList = livePagedListProvider.create(0, + new PagedList.Config.Builder() + .setEnablePlaceholders(true) + .setPageSize(10) + .setPrefetchDistance(10) + .build() + ); + } else { + this.transactionList = null; + } } public LiveData> getTransactionList(){ diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionOrderSpinnerAdapter.java b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionOrderSpinnerAdapter.java new file mode 100644 index 0000000..aba92ae --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionOrderSpinnerAdapter.java @@ -0,0 +1,86 @@ +package cy.agorise.crystalwallet.views; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import java.util.List; + +import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.models.CryptoCurrency; + +/** + * Created by Henry Varona on 09/04/2018. + * + */ + +public class TransactionOrderSpinnerAdapter extends ArrayAdapter { + public static class TransactionOrderSpinnerItem { + private String field; + private String label; + private boolean ascending; + private int order; + + public TransactionOrderSpinnerItem(String field, String label, int order, boolean ascending){ + this.field = field; + this.label = label; + this.ascending = ascending; + this.order = order; + } + + public void setAscending(boolean newValue){ + this.ascending = newValue; + } + + public void setOrder(int newValue){ + this.order = newValue; + } + + public String getField(){ + return this.field; + } + + public String getLabel(){ + return this.label; + } + + public int getOrder(){ + return this.order; + } + + public boolean getAscending(){ + return this.ascending; + } + } + + + private List data; + + public TransactionOrderSpinnerAdapter(Context context, int resource, List objects) { + super(context, resource, objects); + this.data = objects; + } + + @Override + public View getDropDownView(int position, View convertView, ViewGroup parent) { + return getView(position, convertView, parent); + } + + /* + * Creates the view for every element of the spinner + */ + @Override + public View getView(int position, View convertView, ViewGroup parent) { + LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View v = inflater.inflate(R.layout.transactions_order_adapter_item, parent, false); + TextView tvTransactionOrderLabel = v.findViewById(R.id.tvTransactionOrderLabel); + + TransactionOrderSpinnerItem transactionOrderSpinnerItem = getItem(position); + tvTransactionOrderLabel.setText(transactionOrderSpinnerItem.getLabel()); + + return v; + } +} diff --git a/app/src/main/res/layout/transaction_list.xml b/app/src/main/res/layout/transaction_list.xml index 27063ea..68c494e 100644 --- a/app/src/main/res/layout/transaction_list.xml +++ b/app/src/main/res/layout/transaction_list.xml @@ -5,9 +5,38 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> - + android:layout_height="wrap_content"> + + + +