aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2019-01-21 14:32:17 +0000
committerAlex Ning <chineseperson5@gmail.com>2019-01-21 14:32:17 +0000
commit91e6174aa5b2fba9d2777e3ea33b20a833a487bf (patch)
treedddcc95beb6dd4ae9f74ea8252742e2d5bc22d15
parentebc2cac0a6bf61ec4a161aa907cf68f751d03071 (diff)
downloadinfinity-for-reddit-91e6174aa5b2fba9d2777e3ea33b20a833a487bf.tar
infinity-for-reddit-91e6174aa5b2fba9d2777e3ea33b20a833a487bf.tar.gz
infinity-for-reddit-91e6174aa5b2fba9d2777e3ea33b20a833a487bf.tar.bz2
infinity-for-reddit-91e6174aa5b2fba9d2777e3ea33b20a833a487bf.tar.lz
infinity-for-reddit-91e6174aa5b2fba9d2777e3ea33b20a833a487bf.tar.xz
infinity-for-reddit-91e6174aa5b2fba9d2777e3ea33b20a833a487bf.tar.zst
infinity-for-reddit-91e6174aa5b2fba9d2777e3ea33b20a833a487bf.zip
Added feature: View users' best posts.
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java1
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java4
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java368
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java16
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java28
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java10
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java24
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java7
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java4
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java16
11 files changed, 301 insertions, 179 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java
index 7e3a2aa5..c168e71a 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java
@@ -67,4 +67,5 @@ public class JSONUtils {
static final String LINK_ID_KEY = "link_id";
public static final String IS_GOLD_KEY = "is_gold";
public static final String IS_FRIEND_KEY = "is_friend";
+ static final String KIND_KEY = "kind";
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
index 3b814e94..bfc88685 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
@@ -116,7 +116,7 @@ public class MainActivity extends AppCompatActivity {
if(savedInstanceState == null) {
mFragment = new PostFragment();
Bundle bundle = new Bundle();
- bundle.putBoolean(PostFragment.IS_BEST_POST_KEY, true);
+ bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_FRONT_PAGE);
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
} else {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java
index eb404f95..21dfcca0 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java
@@ -55,6 +55,10 @@ class ParsePost {
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
for(int i = 0; i < allData.length(); i++) {
+ String kind = allData.getJSONObject(i).getString(JSONUtils.KIND_KEY);
+ if(!kind.equals("t3")) {
+ continue;
+ }
JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
String id = data.getString(JSONUtils.ID_KEY);
String fullName = data.getString(JSONUtils.NAME_KEY);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
index 87c65fad..b2c54c32 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
@@ -13,11 +13,14 @@ import retrofit2.Callback;
import retrofit2.Retrofit;
class PostDataSource extends PageKeyedDataSource<String, Post> {
+ static final int TYPE_FRONT_PAGE = 0;
+ static final int TYPE_SUBREDDIT = 1;
+ static final int TYPE_USER = 2;
private Retrofit retrofit;
private String accessToken;
private Locale locale;
- private boolean isBestPost;
- private String subredditName;
+ private String name;
+ private int postType;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData;
@@ -27,23 +30,22 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private LoadParams<String> params;
private LoadCallback<String, Post> callback;
- PostDataSource(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) {
+ PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
- this.isBestPost = isBestPost;
paginationNetworkStateLiveData = new MutableLiveData();
initialLoadStateLiveData = new MutableLiveData();
-
+ this.postType = postType;
}
- PostDataSource(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
+ PostDataSource(Retrofit retrofit, Locale locale, String name, int postType) {
this.retrofit = retrofit;
this.locale = locale;
- this.isBestPost = isBestPost;
- this.subredditName = subredditName;
+ this.name = name;
paginationNetworkStateLiveData = new MutableLiveData();
initialLoadStateLiveData = new MutableLiveData();
+ this.postType = postType;
}
MutableLiveData getPaginationNetworkStateLiveData() {
@@ -61,73 +63,16 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
initialLoadStateLiveData.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);
- initialLoadStateLiveData.postValue(NetworkState.LOADED);
- }
-
- @Override
- public void onParsePostFail() {
- Log.i("Post fetch error", "Error parsing data");
- initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
- }
- });
- } else {
- Log.i("Post fetch error", response.message());
- initialLoadStateLiveData.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();
- initialLoadStateLiveData.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);
- initialLoadStateLiveData.postValue(NetworkState.LOADED);
- }
-
- @Override
- public void onParsePostFail() {
- Log.i("Post fetch error", "Error parsing data");
- initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
- }
- });
- } else {
- Log.i("Post fetch error", response.message());
- initialLoadStateLiveData.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();
- initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
- }
- });
+ switch (postType) {
+ case TYPE_FRONT_PAGE:
+ loadBestPostsInitial(callback);
+ break;
+ case TYPE_SUBREDDIT:
+ loadSubredditPostsInitial(callback);
+ break;
+ case TYPE_USER:
+ loadUserPostsInitial(callback);
+ break;
}
}
@@ -143,72 +88,225 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
paginationNetworkStateLiveData.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);
- paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
- }
-
- @Override
- public void onParsePostFail() {
- Log.i("Best post", "Error parsing data");
- paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
- }
- });
- } else {
- Log.i("best post", response.message());
- paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
- }
+ switch (postType) {
+ case TYPE_FRONT_PAGE:
+ loadBestPostsAfter(params, callback);
+ break;
+ case TYPE_SUBREDDIT:
+ loadSubredditPostsAfter(params, callback);
+ break;
+ case TYPE_USER:
+ loadUserPostsAfter(params, callback);
+ }
+ }
+
+ private void loadBestPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback) {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+
+ Call<String> bestPost = api.getBestPosts(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);
+ initialLoadStateLiveData.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Post fetch error", "Error parsing data");
+ initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
+ }
+ });
+ } else {
+ Log.i("Post fetch error", response.message());
+ initialLoadStateLiveData.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();
+ initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ }
+ });
+ }
+
+ private void loadBestPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+ Call<String> bestPost = api.getBestPosts(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);
+ paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Best post", "Error parsing data");
+ paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
+ }
+ });
+ } else {
+ Log.i("best post", response.message());
+ paginationNetworkStateLiveData.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();
+ paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ }
+ });
+ }
+
+ private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback) {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+ Call<String> getPost = api.getSubredditBestPosts(name, 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);
+ initialLoadStateLiveData.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Post fetch error", "Error parsing data");
+ initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
+ }
+ });
+ } else {
+ Log.i("Post fetch error", response.message());
+ initialLoadStateLiveData.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();
- paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ @Override
+ public void onFailure(Call<String> call, Throwable t) {
+ String errorMessage = t == null ? "unknown error" : t.getMessage();
+ initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ }
+ });
+ }
+
+ private void loadSubredditPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+ Call<String> getPost = api.getSubredditBestPosts(name, 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);
+ paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Best post", "Error parsing data");
+ paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
+ }
+ });
+ } else {
+ Log.i("Best post", response.message());
+ paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
}
- });
- } 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);
- paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
- }
-
- @Override
- public void onParsePostFail() {
- Log.i("Best post", "Error parsing data");
- paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
- }
- });
- } else {
- Log.i("Best post", response.message());
- paginationNetworkStateLiveData.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();
+ paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ }
+ });
+ }
+
+ private void loadUserPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback) {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+ Call<String> getPost = api.getUserBestPosts(name, 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);
+ initialLoadStateLiveData.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Post fetch error", "Error parsing data");
+ initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
+ }
+ });
+ } else {
+ Log.i("Post fetch error", response.message());
+ initialLoadStateLiveData.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();
+ initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ }
+ });
+ }
- @Override
- public void onFailure(Call<String> call, Throwable t) {
- String errorMessage = t == null ? "unknown error" : t.getMessage();
- paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ private void loadUserPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+ Call<String> getPost = api.getUserBestPosts(name, 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);
+ paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
+ }
+
+ @Override
+ public void onParsePostFail() {
+ Log.i("Best post", "Error parsing data");
+ paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
+ }
+ });
+ } else {
+ Log.i("Best post", response.message());
+ paginationNetworkStateLiveData.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();
+ paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage));
+ }
+ });
}
void retry() {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
index 9ee9b399..19fe9a06 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java
@@ -12,33 +12,33 @@ class PostDataSourceFactory extends DataSource.Factory {
private String accessToken;
private Locale locale;
private String subredditName;
- private boolean isBestPost;
+ private int postType;
private PostDataSource postDataSource;
private MutableLiveData<PostDataSource> postDataSourceLiveData;
- PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) {
+ PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
postDataSourceLiveData = new MutableLiveData<>();
- this.isBestPost = isBestPost;
+ this.postType = postType;
}
- PostDataSourceFactory(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
+ PostDataSourceFactory(Retrofit retrofit, Locale locale, String subredditName, int postType) {
this.retrofit = retrofit;
this.locale = locale;
this.subredditName = subredditName;
postDataSourceLiveData = new MutableLiveData<>();
- this.isBestPost = isBestPost;
+ this.postType = postType;
}
@Override
public DataSource create() {
- if(isBestPost) {
- postDataSource = new PostDataSource(retrofit, accessToken, locale, isBestPost);
+ if(postType == PostDataSource.TYPE_FRONT_PAGE) {
+ postDataSource = new PostDataSource(retrofit, accessToken, locale, postType);
} else {
- postDataSource = new PostDataSource(retrofit, locale, isBestPost, subredditName);
+ postDataSource = new PostDataSource(retrofit, locale, subredditName, postType);
}
postDataSourceLiveData.postValue(postDataSource);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
index fd7f3407..995cee9a 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java
@@ -31,8 +31,8 @@ import retrofit2.Retrofit;
*/
public class PostFragment extends Fragment implements FragmentCommunicator {
- static final String SUBREDDIT_NAME_KEY = "SNK";
- static final String IS_BEST_POST_KEY = "IBPK";
+ static final String NAME_KEY = "NK";
+ static final String POST_TYPE_KEY = "PTK";
private CoordinatorLayout mCoordinatorLayout;
private RecyclerView mPostRecyclerView;
@@ -41,8 +41,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private LinearLayout mFetchPostErrorLinearLayout;
private ImageView mFetchPostErrorImageView;
- private boolean mIsBestPost;
- private String mSubredditName;
+ private String mName;
+ private int mPostType;
private PostRecyclerViewAdapter mAdapter;
@@ -93,20 +93,20 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}
});*/
- mIsBestPost = getArguments().getBoolean(IS_BEST_POST_KEY);
+ mPostType = getArguments().getInt(POST_TYPE_KEY);
- if(!mIsBestPost) {
- mSubredditName = getArguments().getString(SUBREDDIT_NAME_KEY);
+ if(mPostType != PostDataSource.TYPE_FRONT_PAGE) {
+ mName = getArguments().getString(NAME_KEY);
} else {
mFetchPostErrorLinearLayout.setOnClickListener(view -> mPostViewModel.retry());
}
- if(mIsBestPost) {
+ if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
mAdapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit,
- mSharedPreferences, mIsBestPost, () -> mPostViewModel.retryLoadingMore());
+ mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore());
} else {
mAdapter = new PostRecyclerViewAdapter(getActivity(), mRetrofit,
- mSharedPreferences, mIsBestPost, () -> mPostViewModel.retryLoadingMore());
+ mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore());
}
mPostRecyclerView.setAdapter(mAdapter);
@@ -114,12 +114,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
PostViewModel.Factory factory;
- if(mIsBestPost) {
+ if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
- getResources().getConfiguration().locale, mIsBestPost);
+ getResources().getConfiguration().locale, mPostType);
} else {
factory = new PostViewModel.Factory(mRetrofit,
- getResources().getConfiguration().locale, mIsBestPost, mSubredditName);
+ getResources().getConfiguration().locale, mName, mPostType);
}
mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class);
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
@@ -149,7 +149,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private void showErrorView() {
mProgressBar.setVisibility(View.GONE);
- if(mIsBestPost) {
+ if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
if(getActivity() != null && isAdded()) {
mFetchPostErrorLinearLayout.setVisibility(View.VISIBLE);
Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchPostErrorImageView);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java
index a789b03b..da60c5a5 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java
@@ -60,7 +60,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
private SubredditDao subredditDao;
private UserDao userDao;
private boolean canStartActivity = true;
- private boolean hasMultipleSubreddits;
+ private int postType;
private static final int VIEW_TYPE_DATA = 0;
private static final int VIEW_TYPE_ERROR = 1;
@@ -73,14 +73,14 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
void retryLoadingMore();
}
- PostRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, SharedPreferences sharedPreferences, boolean hasMultipleSubreddits,
+ PostRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, SharedPreferences sharedPreferences, int postType,
RetryLoadingMoreCallback retryLoadingMoreCallback) {
super(DIFF_CALLBACK);
if(context != null) {
mContext = context;
mOauthRetrofit = oauthRetrofit;
mSharedPreferences = sharedPreferences;
- this.hasMultipleSubreddits = hasMultipleSubreddits;
+ this.postType = postType;
glide = Glide.with(mContext.getApplicationContext());
subredditDao = SubredditRoomDatabase.getDatabase(mContext.getApplicationContext()).subredditDao();
userDao = UserRoomDatabase.getDatabase(mContext.getApplicationContext()).userDao();
@@ -148,7 +148,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
}
});
- if(hasMultipleSubreddits) {
+ if(postType != PostDataSource.TYPE_SUBREDDIT) {
if(post.getSubredditIconUrl() == null) {
new LoadSubredditIconAsyncTask(subredditDao, subredditName,
iconImageUrl -> {
@@ -355,7 +355,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
loadImage(holder, post);
}
- if(!hasMultipleSubreddits && post.isStickied()) {
+ if(postType == PostDataSource.TYPE_SUBREDDIT && post.isStickied()) {
((DataViewHolder) holder).stickiedPostImageView.setVisibility(View.VISIBLE);
glide.load(R.drawable.thumbtack).into(((DataViewHolder) holder).stickiedPostImageView);
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
index e25e841f..939ca68c 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java
@@ -18,8 +18,8 @@ public class PostViewModel extends ViewModel {
private LiveData<NetworkState> initialLoadingState;
private LiveData<PagedList<Post>> posts;
- public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) {
- postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, isBestPost);
+ public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType) {
+ postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData());
@@ -34,8 +34,8 @@ public class PostViewModel extends ViewModel {
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
}
- public PostViewModel(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
- postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, isBestPost, subredditName);
+ public PostViewModel(Retrofit retrofit, Locale locale, String subredditName, int postType) {
+ postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, subredditName, postType);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData());
@@ -79,30 +79,30 @@ public class PostViewModel extends ViewModel {
private Retrofit retrofit;
private String accessToken;
private Locale locale;
- private boolean isBestPost;
private String subredditName;
+ private int postType;
- public Factory(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) {
+ public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
- this.isBestPost = isBestPost;
+ this.postType = postType;
}
- public Factory(Retrofit retrofit, Locale locale, boolean isBestPost, String subredditName) {
+ public Factory(Retrofit retrofit, Locale locale, String subredditName, int postType) {
this.retrofit = retrofit;
this.locale = locale;
- this.isBestPost = isBestPost;
this.subredditName = subredditName;
+ this.postType = postType;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
- if(isBestPost) {
- return (T) new PostViewModel(retrofit, accessToken, locale, isBestPost);
+ if(postType == PostDataSource.TYPE_FRONT_PAGE) {
+ return (T) new PostViewModel(retrofit, accessToken, locale, postType);
} else {
- return (T) new PostViewModel(retrofit, locale, isBestPost, subredditName);
+ return (T) new PostViewModel(retrofit, locale, subredditName, postType);
}
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
index 25f83bbd..41fe1567 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
@@ -36,10 +36,13 @@ public interface RedditAPI {
Call<String> voteThing(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
@GET("best?raw_json=1")
- Call<String> getBestPost(@Query("after") String lastItem, @HeaderMap Map<String, String> headers);
+ Call<String> getBestPosts(@Query("after") String lastItem, @HeaderMap Map<String, String> headers);
@GET("r/{subredditName}.json?raw_json=1&limit=25")
- Call<String> getPost(@Path("subredditName") String subredditName, @Query("after") String lastItem);
+ Call<String> getSubredditBestPosts(@Path("subredditName") String subredditName, @Query("after") String lastItem);
+
+ @GET("user/{userName}.json?raw_json=1&limit=25")
+ Call<String> getUserBestPosts(@Path("userName") String userName, @Query("after") String lastItem);
@GET("user/{username}/about.json?raw_json=1")
Call<String> getUserData(@Path("username") String username);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java
index 18b26349..1a083b3c 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java
@@ -293,8 +293,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
if(savedInstanceState == null) {
mFragment = new PostFragment();
Bundle bundle = new Bundle();
- bundle.putString(PostFragment.SUBREDDIT_NAME_KEY, subredditName);
- bundle.putBoolean(PostFragment.IS_BEST_POST_KEY, false);
+ bundle.putString(PostFragment.NAME_KEY, subredditName);
+ bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_SUBREDDIT);
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit();
} else {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java
index 97a6830a..1922f47a 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java
@@ -13,6 +13,7 @@ import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
+import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
@@ -49,6 +50,8 @@ public class ViewUserDetailActivity extends AppCompatActivity {
static final String EXTRA_USER_NAME_KEY = "EUNK";
+ private static final String FRAGMENT_OUT_STATE_KEY = "FOSK";
+
@BindView(R.id.coordinator_layout_view_user_detail_activity) CoordinatorLayout coordinatorLayout;
@BindView(R.id.banner_image_view_view_user_detail_activity) ImageView bannerImageView;
@BindView(R.id.icon_gif_image_view_view_user_detail_activity) GifImageView iconGifImageView;
@@ -56,6 +59,7 @@ public class ViewUserDetailActivity extends AppCompatActivity {
@BindView(R.id.subscribe_user_chip_view_user_detail_activity) Chip subscribeUserChip;
@BindView(R.id.karma_text_view_view_user_detail_activity) TextView karmaTextView;
+ private Fragment mFragment;
private SubscribedUserDao subscribedUserDao;
private RequestManager glide;
private UserViewModel userViewModel;
@@ -268,6 +272,18 @@ public class ViewUserDetailActivity extends AppCompatActivity {
makeSnackbar(R.string.cannot_fetch_user_info);
}
});
+
+ if(savedInstanceState == null) {
+ mFragment = new PostFragment();
+ Bundle bundle = new Bundle();
+ bundle.putString(PostFragment.NAME_KEY, userName);
+ bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_USER);
+ mFragment.setArguments(bundle);
+ getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_user_detail_activity, mFragment).commit();
+ } else {
+ mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY);
+ getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_user_detail_activity, mFragment).commit();
+ }
}
@Override