15
15
import com .google .firebase .firestore .Query ;
16
16
import com .google .firebase .firestore .QuerySnapshot ;
17
17
18
- import java .util .Collections ;
19
18
import java .util .List ;
20
19
21
20
/**
@@ -60,29 +59,13 @@ public void loadInitial(@NonNull LoadInitialParams<PageKey> params,
60
59
.addOnSuccessListener (new OnSuccessListener <QuerySnapshot >() {
61
60
@ Override
62
61
public void onSuccess (QuerySnapshot snapshot ) {
63
- List <DocumentSnapshot > data = snapshot .getDocuments ();
64
- DocumentSnapshot last = getLast (data );
65
-
66
- PageKey nextPage = new PageKey (last , null );
67
- callback .onResult (data , null , nextPage );
62
+ PageKey nextPage = getNextPageKey (snapshot );
63
+ callback .onResult (snapshot .getDocuments (), null , nextPage );
68
64
69
65
mLoadingState .postValue (LoadingState .LOADED );
70
66
}
71
67
})
72
- .addOnFailureListener (new OnFailureListener () {
73
- @ Override
74
- public void onFailure (@ NonNull Exception e ) {
75
- Log .w (TAG , "loadInitial:failure" , e );
76
-
77
- // On error, return an empty page with the next page key being basically
78
- // equal to the initial query.
79
- PageKey nextPage = new PageKey (null , null );
80
- callback .onResult (Collections .<DocumentSnapshot >emptyList (),
81
- null , nextPage );
82
-
83
- mLoadingState .postValue (LoadingState .ERROR );
84
- }
85
- });
68
+ .addOnFailureListener (new OnLoadFailureListener ());
86
69
87
70
}
88
71
@@ -109,28 +92,21 @@ public void loadAfter(@NonNull LoadParams<PageKey> params,
109
92
.addOnSuccessListener (new OnSuccessListener <QuerySnapshot >() {
110
93
@ Override
111
94
public void onSuccess (QuerySnapshot snapshot ) {
112
- List <DocumentSnapshot > data = snapshot .getDocuments ();
113
- DocumentSnapshot last = getLast (data );
114
-
115
- PageKey nextPage = new PageKey (last , null );
116
- callback .onResult (data , nextPage );
95
+ PageKey nextPage = getNextPageKey (snapshot );
96
+ callback .onResult (snapshot .getDocuments (), nextPage );
117
97
118
98
mLoadingState .postValue (LoadingState .LOADED );
119
99
}
120
100
})
121
- .addOnFailureListener (new OnFailureListener () {
122
- @ Override
123
- public void onFailure (@ NonNull Exception e ) {
124
- Log .w (TAG , "loadAfter:failure" , e );
101
+ .addOnFailureListener (new OnLoadFailureListener ());
125
102
126
- // On error, return an empty page with the next page key being basically
127
- // equal to the initial query.
128
- callback .onResult (Collections .<DocumentSnapshot >emptyList (), key );
103
+ }
129
104
130
- mLoadingState . postValue ( LoadingState . ERROR );
131
- }
132
- } );
105
+ private PageKey getNextPageKey ( @ NonNull QuerySnapshot snapshot ) {
106
+ List < DocumentSnapshot > data = snapshot . getDocuments ();
107
+ DocumentSnapshot last = getLast ( data );
133
108
109
+ return new PageKey (last , null );
134
110
}
135
111
136
112
public LiveData <LoadingState > getLoadingState () {
@@ -145,4 +121,19 @@ private DocumentSnapshot getLast(@NonNull List<DocumentSnapshot> data) {
145
121
return data .get (data .size () - 1 );
146
122
}
147
123
}
124
+
125
+ /**
126
+ * Error listener that just logs and sets the error state.
127
+ */
128
+ private class OnLoadFailureListener implements OnFailureListener {
129
+
130
+ @ Override
131
+ public void onFailure (@ NonNull Exception e ) {
132
+ Log .w (TAG , "load:onFailure" , e );
133
+
134
+ // On error we do NOT post any value to the PagedList, we just tell
135
+ // the developer that we are now in the error state.
136
+ mLoadingState .postValue (LoadingState .ERROR );
137
+ }
138
+ }
148
139
}
0 commit comments