Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Adds Support For Alternative RowId Column
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbenprimke committed Nov 6, 2015
1 parent 6a00f7b commit 49a7a34
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public QuoteRecyclerViewAdapter(
RealmResults<QuoteModel> realmResults,
boolean automaticUpdate,
boolean animateIdType) {
super(context, realmResults, automaticUpdate, animateIdType);
super(context, realmResults, automaticUpdate, animateIdType, "quote");
}

public class ViewHolder extends RealmViewHolder {
Expand Down Expand Up @@ -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())));
Expand Down Expand Up @@ -217,6 +226,25 @@ protected Void doInBackground(Void... params) {
remoteItem.execute();
}

private void asyncUpdateQuote(final long id) {
AsyncTask<Void, Void, Void> remoteItem = new AsyncTask<Void, Void, Void>() {
@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<Void, Void, Void> remoteItem = new AsyncTask<Void, Void, Void>() {
@Override
Expand Down
69 changes: 55 additions & 14 deletions library/src/main/java/io/realm/RealmBasedRecyclerViewAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,6 +94,22 @@ public RowWrapper(boolean isRealm, int realmIndex, int sectionHeaderIndex, Strin
private boolean addSectionHeaders;
private String headerColumnName;

public RealmBasedRecyclerViewAdapter(
Context context,
RealmResults<T> realmResults,
boolean automaticUpdate,
boolean animateResults,
String animateColumnName) {
this(
context,
realmResults,
automaticUpdate,
animateResults,
false,
null,
animateColumnName);
}

public RealmBasedRecyclerViewAdapter(
Context context,
RealmResults<T> realmResults,
Expand All @@ -102,12 +119,30 @@ public RealmBasedRecyclerViewAdapter(
}

public RealmBasedRecyclerViewAdapter(
Context context,
RealmResults<T> realmResults,
boolean automaticUpdate,
boolean animateResults,
boolean addSectionHeaders,
String headerColumnName) {
Context context,
RealmResults<T> realmResults,
boolean automaticUpdate,
boolean animateResults,
boolean addSectionHeaders,
String headerColumnName) {
this(
context,
realmResults,
automaticUpdate,
animateResults,
addSectionHeaders,
headerColumnName,
null);
}

public RealmBasedRecyclerViewAdapter(
Context context,
RealmResults<T> realmResults,
boolean automaticUpdate,
boolean animateResults,
boolean addSectionHeaders,
String headerColumnName,
String animateColumnName) {
if (context == null) {
throw new IllegalArgumentException("Context cannot be null");
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 49a7a34

Please sign in to comment.