Skip to content

Commit 39a8cf0

Browse files
Added setDocumentSnapshot() for FirestorePagingOptions (#1869)
1 parent 4c75c23 commit 39a8cf0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
- Allow most auth flows on devices without Google Play services (#1858)
22
- Add ability to setDefaultEmail to the EmailBuilder (#1670) (contributed by @laurentiu-git)
3+
- Add ability to set data directly on FirestorePagingOptions (#1750) (contributed by @laurentiu-git)

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
import androidx.paging.PagedList;
1515
import androidx.recyclerview.widget.DiffUtil;
1616

17+
import static com.firebase.ui.common.Preconditions.assertNull;
18+
1719
/**
1820
* Options to configure an {@link FirestorePagingAdapter}.
1921
*
2022
* Use {@link Builder} to create a new instance.
2123
*/
2224
public final class FirestorePagingOptions<T> {
2325

26+
private static final String ERR_DATA_SET = "Data already set. " +
27+
"Call only one of setData() or setQuery()";
28+
2429
private final LiveData<PagedList<DocumentSnapshot>> mData;
2530
private final SnapshotParser<T> mParser;
2631
private final DiffUtil.ItemCallback<DocumentSnapshot> mDiffCallback;
@@ -66,6 +71,34 @@ public static final class Builder<T> {
6671
private LifecycleOwner mOwner;
6772
private DiffUtil.ItemCallback<DocumentSnapshot> mDiffCallback;
6873

74+
/**
75+
* Directly set data using and parse with a {@link ClassSnapshotParser} based on
76+
* the given class.
77+
* <p>
78+
* Do not call this method after calling {@code setQuery}.
79+
*/
80+
@NonNull
81+
public Builder<T> setData(@NonNull LiveData<PagedList<DocumentSnapshot>> data,
82+
@NonNull Class<T> modelClass) {
83+
84+
return setData(data, new ClassSnapshotParser<>(modelClass));
85+
}
86+
87+
/**
88+
* Directly set data and parse with a custom {@link SnapshotParser}.
89+
* <p>
90+
* Do not call this method after calling {@code setQuery}.
91+
*/
92+
@NonNull
93+
public Builder<T> setData(@NonNull LiveData<PagedList<DocumentSnapshot>> data,
94+
@NonNull SnapshotParser<T> parser) {
95+
assertNull(mData, ERR_DATA_SET);
96+
97+
mData = data;
98+
mParser = parser;
99+
return this;
100+
}
101+
69102
/**
70103
* Sets the query using {@link Source#DEFAULT} and a {@link ClassSnapshotParser} based
71104
* on the given Class.
@@ -121,6 +154,8 @@ public Builder<T> setQuery(@NonNull Query query,
121154
@NonNull Source source,
122155
@NonNull PagedList.Config config,
123156
@NonNull SnapshotParser<T> parser) {
157+
assertNull(mData, ERR_DATA_SET);
158+
124159
// Build paged list
125160
FirestoreDataSource.Factory factory = new FirestoreDataSource.Factory(query, source);
126161
mData = new LivePagedListBuilder<>(factory, config).build();
@@ -162,7 +197,8 @@ public Builder<T> setLifecycleOwner(@NonNull LifecycleOwner owner) {
162197
@NonNull
163198
public FirestorePagingOptions<T> build() {
164199
if (mData == null || mParser == null) {
165-
throw new IllegalStateException("Must call setQuery() before calling build().");
200+
throw new IllegalStateException("Must call setQuery() or setDocumentSnapshot()" +
201+
" before calling build().");
166202
}
167203

168204
if (mDiffCallback == null) {

0 commit comments

Comments
 (0)