Skip to content

Commit 154ecde

Browse files
committed
Move diff callback to options
Change-Id: I0d0d763c7e03c7d37390a2a61bd34195e12f2ca6
1 parent cef8528 commit 154ecde

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

firestore/src/main/java/com/firebase/ui/firestore/paging/FirestorePagingAdapter.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import android.arch.paging.PagedListAdapter;
99
import android.support.annotation.NonNull;
1010
import android.support.annotation.Nullable;
11-
import android.support.v7.util.DiffUtil;
1211
import android.support.v7.widget.RecyclerView;
1312

1413
import com.firebase.ui.firestore.SnapshotParser;
@@ -50,30 +49,8 @@ public void onChanged(@Nullable PagedList<DocumentSnapshot> snapshots) {
5049
}
5150
};
5251

53-
public static class DiffCallback<T> extends DiffUtil.ItemCallback<DocumentSnapshot> {
54-
55-
private final SnapshotParser<T> mParser;
56-
57-
public DiffCallback(SnapshotParser<T> parser) {
58-
mParser = parser;
59-
}
60-
61-
@Override
62-
public boolean areItemsTheSame(DocumentSnapshot oldItem, DocumentSnapshot newItem) {
63-
return oldItem.getId().equals(newItem.getId());
64-
}
65-
66-
@Override
67-
public boolean areContentsTheSame(DocumentSnapshot oldItem, DocumentSnapshot newItem) {
68-
T oldModel = mParser.parseSnapshot(oldItem);
69-
T newModel = mParser.parseSnapshot(newItem);
70-
71-
return oldModel.equals(newModel);
72-
}
73-
}
74-
7552
public FirestorePagingAdapter(@NonNull FirestorePagingOptions<T> options) {
76-
super(new DiffCallback<>(options.getParser()));
53+
super(options.getDiffCallback());
7754

7855
mParser = options.getParser();
7956
mData = options.getData();

firestore/src/main/java/com/firebase/ui/firestore/paging/FirestorePagingOptions.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import android.arch.paging.PagedList;
66
import android.support.annotation.NonNull;
77
import android.support.annotation.Nullable;
8+
import android.support.v7.util.DiffUtil;
89

910
import com.firebase.ui.firestore.ClassSnapshotParser;
1011
import com.firebase.ui.firestore.SnapshotParser;
12+
import com.google.firebase.firestore.DocumentSnapshot;
1113
import com.google.firebase.firestore.Query;
1214

1315
/**
@@ -17,13 +19,16 @@ public class FirestorePagingOptions<T> {
1719

1820
private final PagingData mData;
1921
private final SnapshotParser<T> mParser;
22+
private final DiffUtil.ItemCallback<DocumentSnapshot> mDiffCallback;
2023
private final LifecycleOwner mOwner;
2124

2225
private FirestorePagingOptions(@NonNull PagingData data,
2326
@NonNull SnapshotParser<T> parser,
27+
@NonNull DiffUtil.ItemCallback<DocumentSnapshot> diffCallback,
2428
@Nullable LifecycleOwner owner) {
2529
mData = data;
2630
mParser = parser;
31+
mDiffCallback = diffCallback;
2732
mOwner = owner;
2833
}
2934

@@ -37,6 +42,11 @@ public SnapshotParser<T> getParser() {
3742
return mParser;
3843
}
3944

45+
@NonNull
46+
public DiffUtil.ItemCallback<DocumentSnapshot> getDiffCallback() {
47+
return mDiffCallback;
48+
}
49+
4050
@Nullable
4151
public LifecycleOwner getOwner() {
4252
return mOwner;
@@ -47,6 +57,7 @@ public static class Builder<T> {
4757
private PagingData mData;
4858
private SnapshotParser<T> mParser;
4959
private LifecycleOwner mOwner;
60+
private DiffUtil.ItemCallback<DocumentSnapshot> mDiffCallback;
5061

5162
@NonNull
5263
public Builder<T> setQuery(@NonNull Query query,
@@ -73,9 +84,23 @@ public Builder<T> setLifecycleOwner(@NonNull LifecycleOwner owner) {
7384
return this;
7485
}
7586

87+
@NonNull
88+
public Builder<T> setDiffCallback(@NonNull DiffUtil.ItemCallback<DocumentSnapshot> diffCallback) {
89+
mDiffCallback = diffCallback;
90+
return this;
91+
}
92+
7693
@NonNull
7794
public FirestorePagingOptions<T> build() {
78-
return new FirestorePagingOptions<>(mData, mParser, mOwner);
95+
if (mData == null || mParser == null) {
96+
throw new IllegalStateException("Must call setQuery() before calling build().");
97+
}
98+
99+
if (mDiffCallback == null) {
100+
mDiffCallback = new SnapshotDiffCallback<T>(mParser);
101+
}
102+
103+
return new FirestorePagingOptions<>(mData, mParser, mDiffCallback, mOwner);
79104
}
80105

81106
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.firebase.ui.firestore.paging;
2+
3+
import android.support.annotation.RestrictTo;
4+
import android.support.v7.util.DiffUtil;
5+
6+
import com.firebase.ui.firestore.SnapshotParser;
7+
import com.google.firebase.firestore.DocumentSnapshot;
8+
9+
/**
10+
* Default diff callback implementation for Firestore snapshots.
11+
*/
12+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
13+
public class SnapshotDiffCallback<T> extends DiffUtil.ItemCallback<DocumentSnapshot> {
14+
15+
private final SnapshotParser<T> mParser;
16+
17+
public SnapshotDiffCallback(SnapshotParser<T> parser) {
18+
mParser = parser;
19+
}
20+
21+
@Override
22+
public boolean areItemsTheSame(DocumentSnapshot oldItem, DocumentSnapshot newItem) {
23+
return oldItem.getId().equals(newItem.getId());
24+
}
25+
26+
@Override
27+
public boolean areContentsTheSame(DocumentSnapshot oldItem, DocumentSnapshot newItem) {
28+
T oldModel = mParser.parseSnapshot(oldItem);
29+
T newModel = mParser.parseSnapshot(newItem);
30+
31+
return oldModel.equals(newModel);
32+
}
33+
}

0 commit comments

Comments
 (0)