aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java219
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java16
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java10
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java31
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java2
5 files changed, 203 insertions, 75 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
index 41ba65d1..48d21bf0 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
@@ -17,8 +17,9 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private String accessToken;
private Locale locale;
private boolean isBestPost;
+ private String subredditName;
- //private MutableLiveData networkState;
+ private MutableLiveData networkState;
private MutableLiveData initialLoading;
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) {
@@ -26,14 +27,23 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
this.accessToken = accessToken;
this.locale = locale;
this.isBestPost = isBestPost;
- //networkState = new MutableLiveData();
+ networkState = new MutableLiveData();
initialLoading = new MutableLiveData();
}
- /*public MutableLiveData getNetworkState() {
+ PostDataSource(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
+ this.retrofit = retrofit;
+ this.locale = locale;
+ this.isBestPost = isBestPost;
+ this.subredditName = subredditName;
+ networkState = new MutableLiveData();
+ initialLoading = new MutableLiveData();
+ }
+
+ public MutableLiveData getNetworkState() {
return networkState;
- }*/
+ }
public MutableLiveData getInitialLoading() {
return initialLoading;
@@ -42,42 +52,78 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull final LoadInitialCallback<String, Post> callback) {
initialLoading.postValue(NetworkState.LOADING);
- //networkState.postValue(NetworkState.LOADING);
-
- RedditAPI api = retrofit.create(RedditAPI.class);
-
- Call<String> bestPost = api.getBestPost(null, RedditUtils.getOAuthHeader(accessToken));
- bestPost.enqueue(new Callback<String>() {
- @Override
- public void onResponse(Call<String> call, retrofit2.Response<String> response) {
- if (response.isSuccessful()) {
- ParsePost.parsePost(response.body(), locale,
- new ParsePost.ParsePostListener() {
- @Override
- public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
- callback.onResult(newPosts, null, lastItem);
- initialLoading.postValue(NetworkState.LOADED);
- //networkState.postValue(NetworkState.LOADED);
- }
-
- @Override
- public void onParsePostFail() {
- Log.i("Post fetch error", "Error parsing data");
- }
- });
- } else {
- Log.i("Post fetch error", response.message());
- initialLoading.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
- //networkState.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
+ networkState.postValue(NetworkState.LOADING);
+
+ if(isBestPost) {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+
+ Call<String> bestPost = api.getBestPost(null, RedditUtils.getOAuthHeader(accessToken));
+ bestPost.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(Call<String> call, retrofit2.Response<String> response) {
+ if (response.isSuccessful()) {
+ ParsePost.parsePost(response.body(), locale,
+ new ParsePost.ParsePostListener() {
+ @Override
+ public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
+ callback.onResult(newPosts, null, lastItem);
+ initialLoading.postValue(NetworkState.LOADED);
+ networkState.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Post fetch error", "Error parsing data");
+ }
+ });
+ } else {
+ Log.i("Post fetch error", response.message());
+ initialLoading.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
+ networkState.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
+ }
}
- }
-
- @Override
- public void onFailure(Call<String> call, Throwable t) {
- String errorMessage = t == null ? "unknown error" : t.getMessage();
- //networkState.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
- }
- });
+
+ @Override
+ public void onFailure(Call<String> call, Throwable t) {
+ String errorMessage = t == null ? "unknown error" : t.getMessage();
+ networkState.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ }
+ });
+ } else {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+ Call<String> getPost = api.getPost(subredditName, null);
+ getPost.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(Call<String> call, retrofit2.Response<String> response) {
+ if(response.isSuccessful()) {
+ ParsePost.parsePost(response.body(), locale,
+ new ParsePost.ParsePostListener() {
+ @Override
+ public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
+ callback.onResult(newPosts, null, lastItem);
+ initialLoading.postValue(NetworkState.LOADED);
+ networkState.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Post fetch error", "Error parsing data");
+ }
+ });
+ } else {
+ Log.i("Post fetch error", response.message());
+ initialLoading.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
+ networkState.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
+ }
+ }
+
+ @Override
+ public void onFailure(Call<String> call, Throwable t) {
+ String errorMessage = t == null ? "unknown error" : t.getMessage();
+ networkState.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ }
+ });
+ }
}
@Override
@@ -87,38 +133,71 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void loadAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) {
- //networkState.postValue(NetworkState.LOADING);
-
- RedditAPI api = retrofit.create(RedditAPI.class);
- Call<String> bestPost = api.getBestPost(params.key, RedditUtils.getOAuthHeader(accessToken));
-
- bestPost.enqueue(new Callback<String>() {
- @Override
- public void onResponse(Call<String> call, retrofit2.Response<String> response) {
- if(response.isSuccessful()) {
- ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() {
- @Override
- public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
- callback.onResult(newPosts, lastItem);
- //networkState.postValue(NetworkState.LOADED);
- }
+ networkState.postValue(NetworkState.LOADING);
+
+ if(isBestPost) {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+ Call<String> bestPost = api.getBestPost(params.key, RedditUtils.getOAuthHeader(accessToken));
+
+ bestPost.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(Call<String> call, retrofit2.Response<String> response) {
+ if(response.isSuccessful()) {
+ ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() {
+ @Override
+ public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
+ callback.onResult(newPosts, lastItem);
+ networkState.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Best post", "Error parsing data");
+ }
+ });
+ } else {
+ Log.i("best post", response.message());
+ networkState.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
+ }
+ }
- @Override
- public void onParsePostFail() {
- Log.i("Best post", "Error parsing data");
- }
- });
- } else {
- Log.i("best post", response.message());
- //networkState.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
+ @Override
+ public void onFailure(Call<String> call, Throwable t) {
+ String errorMessage = t == null ? "unknown error" : t.getMessage();
+ networkState.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ }
+ });
+ } else {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+ Call<String> getPost = api.getPost(subredditName, params.key);
+ getPost.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(Call<String> call, retrofit2.Response<String> response) {
+ if(response.isSuccessful()) {
+ ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() {
+ @Override
+ public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
+ callback.onResult(newPosts, lastItem);
+ networkState.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Best post", "Error parsing data");
+ }
+ });
+ } else {
+ Log.i("best post", response.message());
+ networkState.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
+ }
+ }
+
+ @Override
+ public void onFailure(Call<String> call, Throwable t) {
+ String errorMessage = t == null ? "unknown error" : t.getMessage();
+ networkState.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
}
- }
-
- @Override
- public void onFailure(Call<String> call, Throwable t) {
- String errorMessage = t == null ? "unknown error" : t.getMessage();
- //networkState.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
- }
- });
+ });
+ }
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
index 20cda5ca..358802dc 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
@@ -13,6 +13,7 @@ class PostDataSourceFactory extends DataSource.Factory {
private String accessToken;
private Locale locale;
private boolean isBestPost;
+ private String subredditName;
private MutableLiveData<PageKeyedDataSource<String, Post>> mutableLiveData;
@@ -24,9 +25,22 @@ class PostDataSourceFactory extends DataSource.Factory {
mutableLiveData = new MutableLiveData<>();
}
+ PostDataSourceFactory(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
+ this.retrofit = retrofit;
+ this.locale = locale;
+ this.isBestPost = isBestPost;
+ mutableLiveData = new MutableLiveData<>();
+ this.subredditName = subredditName;
+ }
+
@Override
public DataSource create() {
- PostDataSource postDataSource = new PostDataSource(retrofit, accessToken, locale, isBestPost);
+ PostDataSource postDataSource;
+ if(isBestPost) {
+ postDataSource = new PostDataSource(retrofit, accessToken, locale, isBestPost);
+ } else {
+ postDataSource = new PostDataSource(retrofit, locale, isBestPost, subredditName);
+ }
mutableLiveData.postValue(postDataSource);
return postDataSource;
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
index 52c92813..f6e86fc8 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
@@ -124,8 +124,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
String accessToken = getActivity().getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
- PostViewModel.Factory factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
- getResources().getConfiguration().locale, mIsBestPost);
+ PostViewModel.Factory factory;
+ if(mIsBestPost) {
+ factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
+ getResources().getConfiguration().locale, mIsBestPost);
+ } else {
+ factory = new PostViewModel.Factory(mRetrofit,
+ getResources().getConfiguration().locale, mIsBestPost, mSubredditName);
+ }
mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class);
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
index 0be971eb..3d3596c9 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
@@ -36,6 +36,23 @@ public class PostViewModel extends ViewModel {
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
}
+ public PostViewModel(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
+ //executor = Executors.newFixedThreadPool(5);
+
+ PostDataSourceFactory postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, isBestPost, subredditName);
+ /*networkState = Transformations.switchMap(postDataSourceFactory.getMutableLiveData(),
+ (Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getNetworkState);*/
+ liveDataSource = postDataSourceFactory.getMutableLiveData();
+
+ PagedList.Config pagedListConfig =
+ (new PagedList.Config.Builder())
+ .setEnablePlaceholders(false)
+ .setPageSize(25)
+ .build();
+
+ posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
+ }
+
LiveData<PagedList<Post>> getPosts() {
return posts;
}
@@ -49,6 +66,7 @@ public class PostViewModel extends ViewModel {
private String accessToken;
private Locale locale;
private boolean isBestPost;
+ private String subredditName;
public Factory(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) {
this.retrofit = retrofit;
@@ -57,10 +75,21 @@ public class PostViewModel extends ViewModel {
this.isBestPost = isBestPost;
}
+ public Factory(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
+ this.retrofit = retrofit;
+ this.locale = locale;
+ this.isBestPost = isBestPost;
+ this.subredditName = subredditName;
+ }
+
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
- return (T) new PostViewModel(retrofit, accessToken, locale, isBestPost);
+ if(isBestPost) {
+ return (T) new PostViewModel(retrofit, accessToken, locale, isBestPost);
+ } else {
+ return (T) new PostViewModel(retrofit, locale, isBestPost, subredditName);
+ }
}
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
index e150ab26..94da1c58 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
@@ -36,7 +36,7 @@ public interface RedditAPI {
@GET("best?raw_json=1")
Call<String> getBestPost(@Query("after") String lastItem, @HeaderMap Map<String, String> headers);
- @GET("r/{subredditName}.json?raw_json=1")
+ @GET("r/{subredditName}.json?raw_json=1&limit=25")
Call<String> getPost(@Path("subredditName") String subredditName, @Query("after") String lastItem);
@GET("user/{username}/about.json/raw_json=1")