1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
package ml.docilealligator.infinityforreddit.comment;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.MutableLiveData;
import androidx.paging.PageKeyedDataSource;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.thing.SortType;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.post.PostPagingSource;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class CommentDataSource extends PageKeyedDataSource<String, Comment> {
private final Retrofit retrofit;
@Nullable
private final String accessToken;
@NonNull
private final String accountName;
private final String username;
private final SortType sortType;
private final boolean areSavedComments;
private final MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private final MutableLiveData<NetworkState> initialLoadStateLiveData;
private final MutableLiveData<Boolean> hasPostLiveData;
private LoadParams<String> params;
private LoadCallback<String, Comment> callback;
CommentDataSource(Retrofit retrofit, @Nullable String accessToken, @NonNull String accountName,
String username, SortType sortType,
boolean areSavedComments) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.accountName = accountName;
this.username = username;
this.sortType = sortType;
this.areSavedComments = areSavedComments;
paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData<>();
hasPostLiveData = new MutableLiveData<>();
}
MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() {
return paginationNetworkStateLiveData;
}
MutableLiveData<NetworkState> getInitialLoadStateLiveData() {
return initialLoadStateLiveData;
}
MutableLiveData<Boolean> hasPostLiveData() {
return hasPostLiveData;
}
void retryLoadingMore() {
loadAfter(params, callback);
}
@Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, Comment> callback) {
initialLoadStateLiveData.postValue(NetworkState.LOADING);
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> commentsCall;
if (areSavedComments) {
commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED,
null, sortType.getType(), sortType.getTime(),
APIUtils.getOAuthHeader(accessToken));
} else {
if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) {
commentsCall = api.getUserComments(username, null, sortType.getType(),
sortType.getTime());
} else {
commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username,
null, sortType.getType(), sortType.getTime());
}
}
commentsCall.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new ParseCommentAsyncTask(response.body(), new ParseCommentAsyncTask.ParseCommentAsyncTaskListener() {
@Override
public void parseSuccessful(ArrayList<Comment> comments, String after) {
if (comments.size() == 0) {
hasPostLiveData.postValue(false);
} else {
hasPostLiveData.postValue(true);
}
if (after == null || after.equals("") || after.equals("null")) {
callback.onResult(comments, null, null);
} else {
callback.onResult(comments, null, after);
}
initialLoadStateLiveData.postValue(NetworkState.LOADED);
}
@Override
public void parseFailed() {
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
}
}).execute();
} else {
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
}
});
}
@Override
public void loadBefore(@NonNull LoadParams<String> params, @NonNull LoadCallback<String, Comment> callback) {
}
@Override
public void loadAfter(@NonNull LoadParams<String> params, @NonNull LoadCallback<String, Comment> callback) {
this.params = params;
this.callback = callback;
paginationNetworkStateLiveData.postValue(NetworkState.LOADING);
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> commentsCall;
if (areSavedComments) {
commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, params.key,
sortType.getType(), sortType.getTime(), APIUtils.getOAuthHeader(accessToken));
} else {
if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) {
commentsCall = api.getUserComments(username, params.key, sortType.getType(),
sortType.getTime());
} else {
commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken),
username, params.key, sortType.getType(), sortType.getTime());
}
}
commentsCall.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new ParseCommentAsyncTask(response.body(), new ParseCommentAsyncTask.ParseCommentAsyncTaskListener() {
@Override
public void parseSuccessful(ArrayList<Comment> comments, String after) {
if (after == null || after.equals("") || after.equals("null")) {
callback.onResult(comments, null);
} else {
callback.onResult(comments, after);
}
paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
}
@Override
public void parseFailed() {
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
}
}).execute();
} else {
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error fetching data"));
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error fetching data"));
}
});
}
private static class ParseCommentAsyncTask extends AsyncTask<Void, ArrayList<Comment>, ArrayList<Comment>> {
private String after;
private JSONArray commentsJSONArray;
private boolean parseFailed;
private final ParseCommentAsyncTaskListener parseCommentAsyncTaskListener;
ParseCommentAsyncTask(String response, ParseCommentAsyncTaskListener parseCommentAsyncTaskListener) {
this.parseCommentAsyncTaskListener = parseCommentAsyncTaskListener;
try {
JSONObject data = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY);
commentsJSONArray = data.getJSONArray(JSONUtils.CHILDREN_KEY);
after = data.getString(JSONUtils.AFTER_KEY);
parseFailed = false;
} catch (JSONException e) {
parseFailed = true;
e.printStackTrace();
}
}
@Override
protected ArrayList<Comment> doInBackground(Void... voids) {
if (parseFailed) {
return null;
}
ArrayList<Comment> comments = new ArrayList<>();
for (int i = 0; i < commentsJSONArray.length(); i++) {
try {
JSONObject commentJSON = commentsJSONArray.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
comments.add(ParseComment.parseSingleComment(commentJSON, 0));
} catch (JSONException ignored) {
}
}
return comments;
}
@Override
protected void onPostExecute(ArrayList<Comment> commentData) {
super.onPostExecute(commentData);
if (commentData != null) {
parseCommentAsyncTaskListener.parseSuccessful(commentData, after);
} else {
parseCommentAsyncTaskListener.parseFailed();
}
}
interface ParseCommentAsyncTaskListener {
void parseSuccessful(ArrayList<Comment> comments, String after);
void parseFailed();
}
}
}
|