|
14 | 14 | import androidx.paging.PagedList; |
15 | 15 | import androidx.recyclerview.widget.DiffUtil; |
16 | 16 |
|
| 17 | +import static com.firebase.ui.common.Preconditions.assertNull; |
| 18 | + |
17 | 19 | /** |
18 | 20 | * Options to configure an {@link FirestorePagingAdapter}. |
19 | 21 | * |
20 | 22 | * Use {@link Builder} to create a new instance. |
21 | 23 | */ |
22 | 24 | public final class FirestorePagingOptions<T> { |
23 | 25 |
|
| 26 | + private static final String ERR_DATA_SET = "Data already set. " + |
| 27 | + "Call only one of setData() or setQuery()"; |
| 28 | + |
24 | 29 | private final LiveData<PagedList<DocumentSnapshot>> mData; |
25 | 30 | private final SnapshotParser<T> mParser; |
26 | 31 | private final DiffUtil.ItemCallback<DocumentSnapshot> mDiffCallback; |
@@ -66,6 +71,34 @@ public static final class Builder<T> { |
66 | 71 | private LifecycleOwner mOwner; |
67 | 72 | private DiffUtil.ItemCallback<DocumentSnapshot> mDiffCallback; |
68 | 73 |
|
| 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 | + |
69 | 102 | /** |
70 | 103 | * Sets the query using {@link Source#DEFAULT} and a {@link ClassSnapshotParser} based |
71 | 104 | * on the given Class. |
@@ -121,6 +154,8 @@ public Builder<T> setQuery(@NonNull Query query, |
121 | 154 | @NonNull Source source, |
122 | 155 | @NonNull PagedList.Config config, |
123 | 156 | @NonNull SnapshotParser<T> parser) { |
| 157 | + assertNull(mData, ERR_DATA_SET); |
| 158 | + |
124 | 159 | // Build paged list |
125 | 160 | FirestoreDataSource.Factory factory = new FirestoreDataSource.Factory(query, source); |
126 | 161 | mData = new LivePagedListBuilder<>(factory, config).build(); |
@@ -162,7 +197,8 @@ public Builder<T> setLifecycleOwner(@NonNull LifecycleOwner owner) { |
162 | 197 | @NonNull |
163 | 198 | public FirestorePagingOptions<T> build() { |
164 | 199 | 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()."); |
166 | 202 | } |
167 | 203 |
|
168 | 204 | if (mDiffCallback == null) { |
|
0 commit comments