Skip to content

Commit 70184f2

Browse files
SUPERCILEXsamtstern
authored andcommitted
Bring improvements from Firestore adapter to Firebase adapters (#15)
1 parent db46162 commit 70184f2

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ interface FirebaseAdapter<T> extends ChangeEventListener, LifecycleObserver {
1616
/**
1717
* Removes listeners and clears all items in the backing {@link FirebaseArray}.
1818
*/
19-
void cleanup();
19+
void stopListening();
20+
21+
ObservableSnapshotArray<T> getSnapshots();
2022

2123
T getItem(int position);
2224

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public abstract class FirebaseListAdapter<T> extends BaseAdapter implements FirebaseAdapter<T> {
2929
private static final String TAG = "FirebaseListAdapter";
3030

31-
protected final ObservableSnapshotArray<T> mSnapshots;
31+
private final ObservableSnapshotArray<T> mSnapshots;
3232
protected final int mLayout;
3333

3434
public FirebaseListAdapter(FirebaseListOptions<T> options) {
@@ -49,18 +49,15 @@ public void startListening() {
4949
}
5050

5151
@Override
52-
public void cleanup() {
52+
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
53+
public void stopListening() {
5354
mSnapshots.removeChangeEventListener(this);
55+
notifyDataSetChanged();
5456
}
5557

56-
@SuppressWarnings("unused")
57-
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
58-
void cleanup(LifecycleOwner source, Lifecycle.Event event) {
59-
if (event == Lifecycle.Event.ON_STOP) {
60-
cleanup();
61-
} else if (event == Lifecycle.Event.ON_DESTROY) {
62-
source.getLifecycle().removeObserver(this);
63-
}
58+
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
59+
void cleanup(LifecycleOwner source) {
60+
source.getLifecycle().removeObserver(this);
6461
}
6562

6663
@Override
@@ -80,6 +77,11 @@ public void onError(DatabaseError error) {
8077
Log.w(TAG, error.toException());
8178
}
8279

80+
@Override
81+
public ObservableSnapshotArray<T> getSnapshots() {
82+
return mSnapshots;
83+
}
84+
8385
@Override
8486
public T getItem(int position) {
8587
return mSnapshots.getObject(position);

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public abstract class FirebaseRecyclerAdapter<T, VH extends RecyclerView.ViewHol
2727
extends RecyclerView.Adapter<VH> implements FirebaseAdapter<T> {
2828
private static final String TAG = "FirebaseRecyclerAdapter";
2929

30-
protected final ObservableSnapshotArray<T> mSnapshots;
30+
private final ObservableSnapshotArray<T> mSnapshots;
3131

3232
/**
3333
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
@@ -50,19 +50,15 @@ public void startListening() {
5050
}
5151

5252
@Override
53-
public void cleanup() {
53+
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
54+
public void stopListening() {
5455
mSnapshots.removeChangeEventListener(this);
5556
notifyDataSetChanged();
5657
}
5758

58-
@SuppressWarnings("unused")
59-
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
60-
void cleanup(LifecycleOwner source, Lifecycle.Event event) {
61-
if (event == Lifecycle.Event.ON_STOP) {
62-
cleanup();
63-
} else if (event == Lifecycle.Event.ON_DESTROY) {
64-
source.getLifecycle().removeObserver(this);
65-
}
59+
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
60+
void cleanup(LifecycleOwner source) {
61+
source.getLifecycle().removeObserver(this);
6662
}
6763

6864
@Override
@@ -97,6 +93,11 @@ public void onError(DatabaseError error) {
9793
Log.w(TAG, error.toException());
9894
}
9995

96+
@Override
97+
public ObservableSnapshotArray<T> getSnapshots() {
98+
return mSnapshots;
99+
}
100+
100101
@Override
101102
public T getItem(int position) {
102103
return mSnapshots.getObject(position);

0 commit comments

Comments
 (0)