Skip to content

Commit d7aec38

Browse files
PatilShreyassamtstern
authored andcommitted
Method to update Query in Firebase and Firestore Adapters. (#1660)
1 parent aeb1e5c commit d7aec38

File tree

7 files changed

+73
-8
lines changed

7 files changed

+73
-8
lines changed

buildSrc/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ repositories {
44

55
plugins {
66
`kotlin-dsl`
7-
}
7+
}

common/src/main/java/com/firebase/ui/common/BaseObservableSnapshotArray.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ public boolean isListening(@NonNull L listener) {
160160
return mListeners.contains(listener);
161161
}
162162

163+
/**
164+
* Clear data and notify all listeners.
165+
*/
166+
public void clear() {
167+
getSnapshots().clear();
168+
notifyOnDataChanged();
169+
}
170+
163171
protected final void notifyOnChildChanged(@NonNull ChangeEventType type,
164172
@NonNull S snapshot,
165173
int newIndex,

database/src/main/java/com/firebase/ui/database/FirebaseArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public class FirebaseArray<T> extends ObservableSnapshotArray<T>
3434
implements ChildEventListener, ValueEventListener {
35-
private final Query mQuery;
35+
private Query mQuery;
3636
private final List<DataSnapshot> mSnapshots = new ArrayList<>();
3737

3838
/**

database/src/main/java/com/firebase/ui/database/FirebaseIndexArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class FirebaseIndexArray<T> extends ObservableSnapshotArray<T>
3434
implements ChangeEventListener {
3535
private static final String TAG = "FirebaseIndexArray";
3636

37-
private final DatabaseReference mDataRef;
37+
private DatabaseReference mDataRef;
3838
private final Map<DatabaseReference, ValueEventListener> mRefs = new HashMap<>();
3939

4040
private final FirebaseArray<String> mKeySnapshots;

database/src/main/java/com/firebase/ui/database/FirebaseRecyclerAdapter.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ public abstract class FirebaseRecyclerAdapter<T, VH extends RecyclerView.ViewHol
2828
extends RecyclerView.Adapter<VH> implements FirebaseAdapter<T> {
2929
private static final String TAG = "FirebaseRecyclerAdapter";
3030

31-
private final ObservableSnapshotArray<T> mSnapshots;
31+
private FirebaseRecyclerOptions<T> mOptions;
32+
private ObservableSnapshotArray<T> mSnapshots;
3233

3334
/**
3435
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
3536
* {@link FirebaseRecyclerOptions} for configuration options.
3637
*/
3738
public FirebaseRecyclerAdapter(@NonNull FirebaseRecyclerOptions<T> options) {
39+
mOptions = options;
3840
mSnapshots = options.getSnapshots();
3941

40-
if (options.getOwner() != null) {
41-
options.getOwner().getLifecycle().addObserver(this);
42+
if (mOptions.getOwner() != null) {
43+
mOptions.getOwner().getLifecycle().addObserver(this);
4244
}
4345
}
4446

@@ -117,6 +119,30 @@ public int getItemCount() {
117119
return mSnapshots.isListening(this) ? mSnapshots.size() : 0;
118120
}
119121

122+
/**
123+
* Re-initialize the Adapter with a new set of options. Can be used to change the query
124+
* without re-constructing the entire adapter.
125+
*/
126+
public void updateOptions(@NonNull FirebaseRecyclerOptions<T> options) {
127+
// Tear down old options
128+
boolean wasListening = mSnapshots.isListening(this);
129+
if (mOptions.getOwner() != null) {
130+
mOptions.getOwner().getLifecycle().removeObserver(this);
131+
}
132+
mSnapshots.clear();
133+
stopListening();
134+
135+
// Set up new options
136+
mOptions = options;
137+
mSnapshots = options.getSnapshots();
138+
if (options.getOwner() != null) {
139+
options.getOwner().getLifecycle().addObserver(this);
140+
}
141+
if (wasListening) {
142+
startListening();
143+
}
144+
}
145+
120146
@Override
121147
public void onBindViewHolder(@NonNull VH holder, int position) {
122148
onBindViewHolder(holder, position, getItem(position));

firestore/src/main/java/com/firebase/ui/firestore/FirestoreArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
public class FirestoreArray<T> extends ObservableSnapshotArray<T>
2424
implements EventListener<QuerySnapshot> {
25-
private final Query mQuery;
25+
private Query mQuery;
2626
private final MetadataChanges mMetadataChanges;
2727
private ListenerRegistration mRegistration;
2828

firestore/src/main/java/com/firebase/ui/firestore/FirestoreRecyclerAdapter.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public abstract class FirestoreRecyclerAdapter<T, VH extends RecyclerView.ViewHo
2525

2626
private static final String TAG = "FirestoreRecycler";
2727

28-
private final ObservableSnapshotArray<T> mSnapshots;
28+
private FirestoreRecyclerOptions<T> mOptions;
29+
private ObservableSnapshotArray<T> mSnapshots;
2930

3031
/**
3132
* Create a new RecyclerView adapter that listens to a Firestore Query. See {@link
@@ -83,11 +84,41 @@ public T getItem(int position) {
8384
return mSnapshots.get(position);
8485
}
8586

87+
/**
88+
* Gets the size of snapshots in adapter.
89+
*
90+
* @return the total count of snapshots in adapter.
91+
* @see ObservableSnapshotArray#size()
92+
*/
8693
@Override
8794
public int getItemCount() {
8895
return mSnapshots.isListening(this) ? mSnapshots.size() : 0;
8996
}
9097

98+
/**
99+
* Re-initialize the Adapter with a new set of options. Can be used to change the query without
100+
* re-constructing the entire adapter.
101+
*/
102+
public void updateOptions(@NonNull FirestoreRecyclerOptions<T> options) {
103+
// Tear down old options
104+
boolean wasListening = mSnapshots.isListening(this);
105+
if (mOptions.getOwner() != null) {
106+
mOptions.getOwner().getLifecycle().removeObserver(this);
107+
}
108+
mSnapshots.clear();
109+
stopListening();
110+
111+
// Set up new options
112+
mOptions = options;
113+
mSnapshots = options.getSnapshots();
114+
if (options.getOwner() != null) {
115+
options.getOwner().getLifecycle().addObserver(this);
116+
}
117+
if (wasListening) {
118+
startListening();
119+
}
120+
}
121+
91122
@Override
92123
public void onChildChanged(@NonNull ChangeEventType type,
93124
@NonNull DocumentSnapshot snapshot,

0 commit comments

Comments
 (0)