- Changing Transaction List to use PagedList

This commit is contained in:
Javier Varona 2017-09-17 22:38:05 -04:00
parent f388f77b49
commit c64e437ccf
9 changed files with 125 additions and 42 deletions

View file

@ -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';
}

View file

@ -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<List<CryptoCoinTransaction>> transactionData = transactionListViewModel.getTransactionList();
LiveData<PagedList<CryptoCoinTransaction>> transactionData = transactionListViewModel.getTransactionList();
transactionData.observe(this, new Observer<List<CryptoCoinTransaction>>() {
transactionData.observe(this, new Observer<PagedList<CryptoCoinTransaction>>() {
@Override
public void onChanged(List<CryptoCoinTransaction> cryptoCoinTransactions) {
public void onChanged(PagedList<CryptoCoinTransaction> cryptoCoinTransactions) {
transactionListView.setData(cryptoCoinTransactions);
}
});

View file

@ -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<List<CryptoCoinTransaction>> getAll();
@Query("SELECT * FROM crypto_coin_transaction ORDER BY date ASC")
LivePagedListProvider<Integer, CryptoCoinTransaction> transactionsByDate();
@Insert(onConflict = OnConflictStrategy.REPLACE)
public void insertTransaction(CryptoCoinTransaction... transactions);
}

View file

@ -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<CryptoCoinTransaction> DIFF_CALLBACK = new DiffCallback<CryptoCoinTransaction>() {
@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;
}
}

View file

@ -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<List<CryptoCoinTransaction>> transactionList;
private LiveData<PagedList<CryptoCoinTransaction>> 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<List<CryptoCoinTransaction>> getTransactionList(){
public LiveData<PagedList<CryptoCoinTransaction>> getTransactionList(){
return this.transactionList;
}
}

View file

@ -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<CryptoCoinTransaction> {
public class TransactionListAdapter extends PagedListAdapter<CryptoCoinTransaction, TransactionViewHolder> {
//List<CryptoCoinTransaction> items;
public TransactionListAdapter(Context context, List<CryptoCoinTransaction> items) {
super(context, 0, items);
}
public void updateData(List<CryptoCoinTransaction> 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;
}
}

View file

@ -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<CryptoCoinTransaction> data){
public void setData(PagedList<CryptoCoinTransaction> 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);
}
}

View file

@ -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);
}
});*/
}
}

View file

@ -3,7 +3,7 @@
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
<RecyclerView
android:id="@+id/transactionListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />