From 2b638acd8913c1b35b1c232e6762f0a38beb1e69 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Mon, 20 Nov 2017 22:24:48 -0400 Subject: [PATCH] - Fixing Transaction list to work properly - Improving UI transaction list info --- .../crystalwallet/dao/TransactionDao.java | 6 ++-- .../fragments/TransactionsFragment.java | 32 ++++++++++++++++++- .../viewmodels/CryptoCurrencyViewModel.java | 27 ++++++++++++++++ .../views/TransactionListAdapter.java | 8 +++-- .../views/TransactionListView.java | 5 +-- .../views/TransactionViewHolder.java | 29 +++++++++++++++-- .../main/res/layout/transaction_list_item.xml | 11 +++++++ 7 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoCurrencyViewModel.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 9bd79b2..d84a61b 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java @@ -20,13 +20,13 @@ public interface TransactionDao { @Query("SELECT * FROM crypto_coin_transaction") LiveData> getAll(); - @Query("SELECT * FROM crypto_coin_transaction ORDER BY date ASC") + @Query("SELECT * FROM crypto_coin_transaction ORDER BY date DESC") LivePagedListProvider transactionsByDate(); - @Query("SELECT * FROM crypto_coin_transaction WHERE account_id = :idAccount ORDER BY date ASC") + @Query("SELECT * FROM crypto_coin_transaction WHERE account_id = :idAccount ORDER BY date DESC") LiveData> getByIdAccountLiveData(long idAccount); - @Query("SELECT * FROM crypto_coin_transaction WHERE account_id = :idAccount ORDER BY date ASC") + @Query("SELECT * FROM crypto_coin_transaction WHERE account_id = :idAccount ORDER BY date DESC") List getByIdAccount(long idAccount); @Insert(onConflict = OnConflictStrategy.REPLACE) 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 d98763f..7bd7edb 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/TransactionsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/TransactionsFragment.java @@ -1,14 +1,30 @@ package cy.agorise.crystalwallet.fragments; +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.Observer; +import android.arch.lifecycle.ViewModelProviders; +import android.arch.paging.PagedList; import android.os.Bundle; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.models.CryptoCoinTransaction; +import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; +import cy.agorise.crystalwallet.views.TransactionListView; public class TransactionsFragment extends Fragment { + + @BindView(R.id.vTransactionListView) + TransactionListView transactionListView; + public TransactionsFragment() { // Required empty public constructor } @@ -29,6 +45,20 @@ public class TransactionsFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_transactions, container, false); + View v = inflater.inflate(R.layout.fragment_transactions, container, false); + ButterKnife.bind(this, v); + + TransactionListViewModel transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class); + LiveData> transactionsLiveData = transactionListViewModel.getTransactionList(); + + final Fragment fragment = this; + transactionsLiveData.observe(this, new Observer>() { + @Override + public void onChanged(@Nullable PagedList cryptoCoinTransactions) { + transactionListView.setData(cryptoCoinTransactions, fragment); + } + }); + + return v; } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoCurrencyViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoCurrencyViewModel.java new file mode 100644 index 0000000..fa6cc20 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoCurrencyViewModel.java @@ -0,0 +1,27 @@ +package cy.agorise.crystalwallet.viewmodels; + +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; + +import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.models.CryptoCurrency; +import cy.agorise.crystalwallet.models.CryptoCurrencyEquivalence; + +/** + * Created by Henry Varona on 4/11/2017. + */ + +public class CryptoCurrencyViewModel extends AndroidViewModel { + + private CrystalDatabase db; + + public CryptoCurrencyViewModel(Application application) { + super(application); + this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); + } + + public CryptoCurrency getCryptoCurrencyById(long id){ + return this.db.cryptoCurrencyDao().getById(id); + } +} 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 6eee357..bc0abb3 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java @@ -6,6 +6,7 @@ import android.arch.paging.PagedListAdapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -30,8 +31,11 @@ import cy.agorise.crystalwallet.models.CryptoCoinTransaction; public class TransactionListAdapter extends PagedListAdapter { - public TransactionListAdapter() { + Fragment fragment; + + public TransactionListAdapter(Fragment fragment) { super(CryptoCoinTransaction.DIFF_CALLBACK); + this.fragment = fragment; } @Override @@ -39,7 +43,7 @@ public class TransactionListAdapter extends PagedListAdapter data){ + public void setData(PagedList data, Fragment fragment){ //Initializes the adapter of the transaction list if (this.listAdapter == null) { - this.listAdapter = new TransactionListAdapter(); + this.listAdapter = new TransactionListAdapter(fragment); this.listView.setAdapter(this.listAdapter); } 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 dfccc6c..3aa6ecd 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java @@ -1,11 +1,16 @@ package cy.agorise.crystalwallet.views; +import android.arch.lifecycle.LifecycleOwner; +import android.arch.lifecycle.ViewModelProviders; +import android.support.v4.app.Fragment; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; +import cy.agorise.crystalwallet.models.CryptoCurrency; +import cy.agorise.crystalwallet.viewmodels.CryptoCurrencyViewModel; /** * Created by Henry Varona on 17/9/2017. @@ -27,14 +32,18 @@ public class TransactionViewHolder extends RecyclerView.ViewHolder { */ private TextView transactionAmount; + private TextView tvTransactionDate; - public TransactionViewHolder(View itemView) { + private Fragment fragment; + + public TransactionViewHolder(View itemView, Fragment fragment) { super(itemView); //TODO: use ButterKnife to load this transactionFrom = (TextView) itemView.findViewById(R.id.fromText); transactionTo = (TextView) itemView.findViewById(R.id.toText); transactionAmount = (TextView) itemView.findViewById(R.id.amountText); - + tvTransactionDate = (TextView) itemView.findViewById(R.id.tvTransactionDate); + this.fragment = fragment; } /* @@ -55,9 +64,23 @@ public class TransactionViewHolder extends RecyclerView.ViewHolder { transactionTo.setText(""); transactionAmount.setText(""); } else { + CryptoCurrencyViewModel cryptoCurrencyViewModel = ViewModelProviders.of(this.fragment).get(CryptoCurrencyViewModel.class); + CryptoCurrency cryptoCurrency = cryptoCurrencyViewModel.getCryptoCurrencyById(transaction.getIdCurrency()); + String amountString = String.format("%.2f",transaction.getAmount()/Math.pow(10,cryptoCurrency.getPrecision())); + + tvTransactionDate.setText(transaction.getDate().toString()); transactionFrom.setText(transaction.getFrom()); transactionTo.setText(transaction.getTo()); - transactionAmount.setText("" + transaction.getAmount()); + + if (transaction.getInput()) { + transactionAmount.setTextColor(itemView.getContext().getResources().getColor(R.color.green)); + } else { + transactionAmount.setTextColor(itemView.getContext().getResources().getColor(R.color.red)); + } + transactionAmount.setText( + amountString + + " " + + cryptoCurrency.getName()); //This will load the transaction receipt when the user clicks this view /*itemView.setOnClickListener(new View.OnClickListener() { @Override diff --git a/app/src/main/res/layout/transaction_list_item.xml b/app/src/main/res/layout/transaction_list_item.xml index 68a4cde..27a8aa9 100644 --- a/app/src/main/res/layout/transaction_list_item.xml +++ b/app/src/main/res/layout/transaction_list_item.xml @@ -13,10 +13,20 @@ android:layout_alignParentStart="true" android:layout_alignParentTop="true"> + + @@ -25,6 +35,7 @@ android:id="@+id/toText" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_below="@+id/tvTransactionDate" android:layout_alignParentBottom="false" android:layout_alignParentRight="true" android:ems="10"