From c64e437ccff15e9dffaf7edbb76c7ad5d260733f Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Sun, 17 Sep 2017 22:38:05 -0400 Subject: [PATCH] - Changing Transaction List to use PagedList --- app/build.gradle | 11 ++--- .../agorise/crystalwallet/IntroActivity.java | 7 +-- .../crystalwallet/dao/TransactionDao.java | 4 ++ .../models/CryptoCoinTransaction.java | 35 +++++++++++++++ .../viewmodels/TransactionListViewModel.java | 13 ++++-- .../views/TransactionListAdapter.java | 40 +++++++---------- .../views/TransactionListView.java | 11 +++-- .../views/TransactionViewHolder.java | 44 +++++++++++++++++++ app/src/main/res/layout/transaction_list.xml | 2 +- 9 files changed, 125 insertions(+), 42 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java diff --git a/app/build.gradle b/app/build.gradle index 8f65315..b6fb2d8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,11 +34,12 @@ dependencies { compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' - compile "android.arch.lifecycle:runtime:1.0.0-alpha9" - compile "android.arch.lifecycle:extensions:1.0.0-alpha9" - annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha9" - compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'; - apt "android.arch.persistence.room:compiler:1.0.0-alpha9"; + compile "android.arch.lifecycle:runtime:1.0.0-alpha9-1" + compile "android.arch.lifecycle:extensions:1.0.0-alpha9-1" + annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha9-1" + compile 'android.arch.persistence.room:runtime:1.0.0-alpha9-1'; + apt "android.arch.persistence.room:compiler:1.0.0-alpha9-1"; + compile "android.arch.paging:runtime:1.0.0-alpha1"; compile 'com.idescout.sql:sqlscout-server:2.0'; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java b/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java index 191ead1..1430f91 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java @@ -5,6 +5,7 @@ import android.arch.lifecycle.LiveData; import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProvider; import android.arch.lifecycle.ViewModelProviders; +import android.arch.paging.PagedList; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; @@ -31,11 +32,11 @@ public class IntroActivity extends LifecycleActivity { transactionListView = this.findViewById(R.id.transaction_list); transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class); - LiveData> transactionData = transactionListViewModel.getTransactionList(); + LiveData> transactionData = transactionListViewModel.getTransactionList(); - transactionData.observe(this, new Observer>() { + transactionData.observe(this, new Observer>() { @Override - public void onChanged(List cryptoCoinTransactions) { + public void onChanged(PagedList cryptoCoinTransactions) { transactionListView.setData(cryptoCoinTransactions); } }); 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 62f4601..c29c0eb 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java @@ -1,6 +1,7 @@ package cy.agorise.crystalwallet.dao; import android.arch.lifecycle.LiveData; +import android.arch.paging.LivePagedListProvider; import android.arch.persistence.room.Dao; import android.arch.persistence.room.Insert; import android.arch.persistence.room.OnConflictStrategy; @@ -19,6 +20,9 @@ public interface TransactionDao { @Query("SELECT * FROM crypto_coin_transaction") LiveData> getAll(); + @Query("SELECT * FROM crypto_coin_transaction ORDER BY date ASC") + LivePagedListProvider transactionsByDate(); + @Insert(onConflict = OnConflictStrategy.REPLACE) public void insertTransaction(CryptoCoinTransaction... transactions); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java index d029710..07c9117 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java @@ -3,6 +3,8 @@ package cy.agorise.crystalwallet.models; import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; +import android.support.annotation.NonNull; +import android.support.v7.recyclerview.extensions.DiffCallback; import java.util.Date; @@ -130,4 +132,37 @@ public class CryptoCoinTransaction { public int getIdAsset() { return idAsset; } public void setIdAsset(int idAsset) { this.idAsset = idAsset; } + + + public static final DiffCallback DIFF_CALLBACK = new DiffCallback() { + @Override + public boolean areItemsTheSame( + @NonNull CryptoCoinTransaction oldTransaction, @NonNull CryptoCoinTransaction newTransaction) { + return oldTransaction.getId() == newTransaction.getId(); + } + @Override + public boolean areContentsTheSame( + @NonNull CryptoCoinTransaction oldTransaction, @NonNull CryptoCoinTransaction newTransaction) { + return oldTransaction.equals(newTransaction); + } + }; + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + CryptoCoinTransaction that = (CryptoCoinTransaction) o; + + if (isInput != that.isInput) return false; + if (accountId != that.accountId) return false; + if (amount != that.amount) return false; + if (idAsset != that.idAsset) return false; + if (isConfirmed != that.isConfirmed) return false; + if (date != null ? !date.equals(that.date) : that.date != null) return false; + if (from != null ? !from.equals(that.from) : that.from != null) return false; + return to != null ? to.equals(that.to) : that.to == null; + + } } 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 463f342..160dd32 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.PagedList; import android.content.Context; import java.util.List; @@ -18,17 +19,21 @@ import cy.agorise.crystalwallet.views.TransactionListView; public class TransactionListViewModel extends AndroidViewModel { - private LiveData> transactionList; + private LiveData> transactionList; private CrystalDatabase db; public TransactionListViewModel(Application application) { super(application); this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); - transactionList = this.db.transactionDao().getAll(); + transactionList = this.db.transactionDao().transactionsByDate().create(0, + new PagedList.Config.Builder() + .setPageSize(10) + .setPrefetchDistance(10) + .build() + ); } - 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 ca57c53..9301223 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java @@ -2,9 +2,11 @@ package cy.agorise.crystalwallet.views; import android.arch.lifecycle.LiveData; +import android.arch.paging.PagedListAdapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,36 +23,24 @@ import cy.agorise.crystalwallet.models.CryptoCoinTransaction; * Created by Henry Varona on 11/9/2017. */ -public class TransactionListAdapter extends ArrayAdapter { +public class TransactionListAdapter extends PagedListAdapter { - //List items; - - public TransactionListAdapter(Context context, List items) { - super(context, 0, items); - } - - public void updateData(List items){ - this.clear(); - this.addAll(items); - this.notifyDataSetChanged(); + public TransactionListAdapter() { + super(CryptoCoinTransaction.DIFF_CALLBACK); } @Override - public View getView(int position, View convertView, ViewGroup parent) { + public TransactionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + return null; + } + + @Override + public void onBindViewHolder(TransactionViewHolder holder, int position) { CryptoCoinTransaction transaction = getItem(position); - - if (convertView == null) { - convertView = LayoutInflater.from(getContext()).inflate(R.layout.transaction_list_item, parent, false); + if (transaction != null) { + holder.bindTo(transaction); + } else { + holder.clear(); } - - TextView tvFrom = (TextView) convertView.findViewById(R.id.fromText); - TextView tvTo = (TextView) convertView.findViewById(R.id.toText); - TextView tvAmount = (TextView) convertView.findViewById(R.id.amountText); - - tvFrom.setText(transaction.getFrom()); - tvTo.setText(transaction.getTo()); - tvAmount.setText(""+transaction.getAmount()); - - return convertView; } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListView.java b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListView.java index c5b58c3..6a7f4a0 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListView.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListView.java @@ -1,6 +1,9 @@ package cy.agorise.crystalwallet.views; +import android.arch.paging.PagedList; +import android.arch.paging.PagedListAdapter; import android.content.Context; +import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.View; import android.widget.ListView; @@ -19,7 +22,7 @@ import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; public class TransactionListView extends RelativeLayout { View rootView; - ListView listView; + RecyclerView listView; TransactionListAdapter listAdapter; TransactionListViewModel transactionListViewModel; @@ -30,12 +33,12 @@ public class TransactionListView extends RelativeLayout { this.listView = rootView.findViewById(R.id.transactionListView); } - public void setData(List data){ + public void setData(PagedList data){ if (this.listAdapter == null) { - this.listAdapter = new TransactionListAdapter(this.getContext(), data); + this.listAdapter = new TransactionListAdapter(); this.listView.setAdapter(this.listAdapter); } else { - this.listAdapter.updateData(data); + this.listAdapter.setList(data); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java new file mode 100644 index 0000000..0ebc533 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java @@ -0,0 +1,44 @@ +package cy.agorise.crystalwallet.views; + +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; + +/** + * Created by Henry Varona on 17/9/2017. + */ + +public class TransactionViewHolder extends RecyclerView.ViewHolder { + private TextView transactionFrom; + private TextView transactionTo; + private TextView transactionAmount; + + + public TransactionViewHolder(View itemView) { + super(itemView); + transactionFrom = (TextView) itemView.findViewById(R.id.fromText); + transactionTo = (TextView) itemView.findViewById(R.id.toText); + transactionAmount = (TextView) itemView.findViewById(R.id.amountText); + } + + public void clear(){ + transactionFrom.setText("loading..."); + transactionTo.setText(""); + transactionAmount.setText(""); + } + + public void bindTo(final CryptoCoinTransaction transaction/*, final OnTransactionClickListener listener*/) { + transactionFrom.setText(transaction.getFrom()); + transactionTo.setText(transaction.getTo()); + transactionAmount.setText(""+transaction.getAmount()); + /*itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + listener.onUserClick(user); + } + });*/ + } +} diff --git a/app/src/main/res/layout/transaction_list.xml b/app/src/main/res/layout/transaction_list.xml index 34a79de..59e8e71 100644 --- a/app/src/main/res/layout/transaction_list.xml +++ b/app/src/main/res/layout/transaction_list.xml @@ -3,7 +3,7 @@ android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> -