From 49a7a34dbcaf00bbc0258c173c6bc67398759804 Mon Sep 17 00:00:00 2001 From: Thorben Primke Date: Fri, 6 Nov 2015 12:41:48 -0800 Subject: [PATCH] Adds Support For Alternative RowId Column --- .../example/MainActivity.java | 30 +++++++- .../realm/RealmBasedRecyclerViewAdapter.java | 69 +++++++++++++++---- 2 files changed, 84 insertions(+), 15 deletions(-) diff --git a/example/app/src/main/java/co/moonmonkeylabs/realmrecyclerview/example/MainActivity.java b/example/app/src/main/java/co/moonmonkeylabs/realmrecyclerview/example/MainActivity.java index 905657f..4bc9ad7 100644 --- a/example/app/src/main/java/co/moonmonkeylabs/realmrecyclerview/example/MainActivity.java +++ b/example/app/src/main/java/co/moonmonkeylabs/realmrecyclerview/example/MainActivity.java @@ -142,7 +142,7 @@ public QuoteRecyclerViewAdapter( RealmResults realmResults, boolean automaticUpdate, boolean animateIdType) { - super(context, realmResults, automaticUpdate, animateIdType); + super(context, realmResults, automaticUpdate, animateIdType, "quote"); } public class ViewHolder extends RealmViewHolder { @@ -173,6 +173,15 @@ public void onClick(View v) { } } ); + viewHolder.quoteTextView.setOnLongClickListener( + new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + asyncUpdateQuote(quoteModel.getId()); + return true; + } + } + ); if (type != null && type.equals("Grid")) { viewHolder.container.setBackgroundColor( colors.get((int) (quoteModel.getId() % colors.size()))); @@ -217,6 +226,25 @@ protected Void doInBackground(Void... params) { remoteItem.execute(); } + private void asyncUpdateQuote(final long id) { + AsyncTask remoteItem = new AsyncTask() { + @Override + protected Void doInBackground(Void... params) { + Realm instance = Realm.getInstance(MainActivity.this); + QuoteModel quoteModel = + instance.where(QuoteModel.class).equalTo("id", id).findFirst(); + if (quoteModel != null) { + instance.beginTransaction(); + quoteModel.setQuote("Updated: " + quoteModel.getQuote()); + instance.commitTransaction(); + } + instance.close(); + return null; + } + }; + remoteItem.execute(); + } + private void asyncRefreshAllQuotes() { AsyncTask remoteItem = new AsyncTask() { @Override diff --git a/library/src/main/java/io/realm/RealmBasedRecyclerViewAdapter.java b/library/src/main/java/io/realm/RealmBasedRecyclerViewAdapter.java index 840d6db..5722c8d 100644 --- a/library/src/main/java/io/realm/RealmBasedRecyclerViewAdapter.java +++ b/library/src/main/java/io/realm/RealmBasedRecyclerViewAdapter.java @@ -36,6 +36,7 @@ import co.moonmonkeylabs.realmrecyclerview.LoadMoreListItemView; import co.moonmonkeylabs.realmrecyclerview.R; import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView; +import difflib.Chunk; import difflib.Delta; import difflib.DiffUtils; import difflib.Patch; @@ -93,6 +94,22 @@ public RowWrapper(boolean isRealm, int realmIndex, int sectionHeaderIndex, Strin private boolean addSectionHeaders; private String headerColumnName; + public RealmBasedRecyclerViewAdapter( + Context context, + RealmResults realmResults, + boolean automaticUpdate, + boolean animateResults, + String animateColumnName) { + this( + context, + realmResults, + automaticUpdate, + animateResults, + false, + null, + animateColumnName); + } + public RealmBasedRecyclerViewAdapter( Context context, RealmResults realmResults, @@ -102,12 +119,30 @@ public RealmBasedRecyclerViewAdapter( } public RealmBasedRecyclerViewAdapter( - Context context, - RealmResults realmResults, - boolean automaticUpdate, - boolean animateResults, - boolean addSectionHeaders, - String headerColumnName) { + Context context, + RealmResults realmResults, + boolean automaticUpdate, + boolean animateResults, + boolean addSectionHeaders, + String headerColumnName) { + this( + context, + realmResults, + automaticUpdate, + animateResults, + addSectionHeaders, + headerColumnName, + null); + } + + public RealmBasedRecyclerViewAdapter( + Context context, + RealmResults realmResults, + boolean automaticUpdate, + boolean animateResults, + boolean addSectionHeaders, + String headerColumnName, + String animateColumnName) { if (context == null) { throw new IllegalArgumentException("Context cannot be null"); } @@ -123,17 +158,22 @@ public RealmBasedRecyclerViewAdapter( // If automatic updates aren't enabled, then animateResults should be false as well. this.animateResults = (automaticUpdate && animateResults); if (animateResults) { - final long primaryKey = realmResults.getTable().getTable().getPrimaryKey(); - if (primaryKey == TableOrView.NO_MATCH) { - throw new IllegalStateException("Animating the results requires a primaryKey."); + if (animateColumnName == null) { + animateColumnIndex = realmResults.getTable().getTable().getPrimaryKey(); + } else { + animateColumnIndex = realmResults.getTable().getTable() + .getColumnIndex(animateColumnName); + } + if (animateColumnIndex == TableOrView.NO_MATCH) { + throw new IllegalStateException( + "Animating the results requires a primaryKey/valid animateColumnName."); } - ColumnType columnType = realmResults.getTable().getColumnType(primaryKey); - if (columnType != ColumnType.INTEGER && columnType != ColumnType.STRING) { + + animateIdType = realmResults.getTable().getColumnType(animateColumnIndex); + if (animateIdType != ColumnType.INTEGER && animateIdType != ColumnType.STRING) { throw new IllegalStateException( "Animating requires a primary key of type Integer/Long or String"); } - animateColumnIndex = primaryKey; - animateIdType = columnType; } if (addSectionHeaders && headerColumnName == null) { @@ -353,7 +393,8 @@ public void onChange() { if (delta.getRevised().size() == 1) { notifyItemInserted(delta.getRevised().getPosition()); } else { - notifyDataSetChanged(); + final Chunk revised = delta.getRevised(); + notifyItemRangeInserted(revised.getPosition(), revised.size()); } } else if (delta.getType() == Delta.TYPE.DELETE) { if (delta.getOriginal().size() == 1) {