From 91e6174aa5b2fba9d2777e3ea33b20a833a487bf Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 21 Jan 2019 22:32:17 +0800 Subject: Added feature: View users' best posts. --- .../infinityforreddit/JSONUtils.java | 1 + .../infinityforreddit/MainActivity.java | 2 +- .../infinityforreddit/ParsePost.java | 4 + .../infinityforreddit/PostDataSource.java | 368 +++++++++++++-------- .../infinityforreddit/PostDataSourceFactory.java | 16 +- .../infinityforreddit/PostFragment.java | 28 +- .../infinityforreddit/PostRecyclerViewAdapter.java | 10 +- .../infinityforreddit/PostViewModel.java | 24 +- .../infinityforreddit/RedditAPI.java | 7 +- .../ViewSubredditDetailActivity.java | 4 +- .../infinityforreddit/ViewUserDetailActivity.java | 16 + 11 files changed, 301 insertions(+), 179 deletions(-) (limited to 'app/src/main/java') 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 { + 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 paginationNetworkStateLiveData; private MutableLiveData initialLoadStateLiveData; @@ -27,23 +30,22 @@ class PostDataSource extends PageKeyedDataSource { private LoadParams params; private LoadCallback 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 { initialLoadStateLiveData.postValue(NetworkState.LOADING); - if(isBestPost) { - RedditAPI api = retrofit.create(RedditAPI.class); - - Call bestPost = api.getBestPost(null, RedditUtils.getOAuthHeader(accessToken)); - bestPost.enqueue(new Callback() { - @Override - public void onResponse(Call call, retrofit2.Response response) { - if (response.isSuccessful()) { - ParsePost.parsePost(response.body(), locale, - new ParsePost.ParsePostListener() { - @Override - public void onParsePostSuccess(ArrayList 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 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 getPost = api.getPost(subredditName, null); - getPost.enqueue(new Callback() { - @Override - public void onResponse(Call call, retrofit2.Response response) { - if(response.isSuccessful()) { - ParsePost.parsePost(response.body(), locale, - new ParsePost.ParsePostListener() { - @Override - public void onParsePostSuccess(ArrayList 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 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 { paginationNetworkStateLiveData.postValue(NetworkState.LOADING); - if(isBestPost) { - RedditAPI api = retrofit.create(RedditAPI.class); - Call bestPost = api.getBestPost(params.key, RedditUtils.getOAuthHeader(accessToken)); - - bestPost.enqueue(new Callback() { - @Override - public void onResponse(Call call, retrofit2.Response response) { - if(response.isSuccessful()) { - ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { - @Override - public void onParsePostSuccess(ArrayList 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 callback) { + RedditAPI api = retrofit.create(RedditAPI.class); + + Call bestPost = api.getBestPosts(null, RedditUtils.getOAuthHeader(accessToken)); + bestPost.enqueue(new Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + if (response.isSuccessful()) { + ParsePost.parsePost(response.body(), locale, + new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(ArrayList 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 call, Throwable t) { + String errorMessage = t == null ? "unknown error" : t.getMessage(); + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); + } + }); + } + + private void loadBestPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback) { + RedditAPI api = retrofit.create(RedditAPI.class); + Call bestPost = api.getBestPosts(params.key, RedditUtils.getOAuthHeader(accessToken)); + + bestPost.enqueue(new Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + if(response.isSuccessful()) { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(ArrayList 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 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 callback) { + RedditAPI api = retrofit.create(RedditAPI.class); + Call getPost = api.getSubredditBestPosts(name, null); + getPost.enqueue(new Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + if(response.isSuccessful()) { + ParsePost.parsePost(response.body(), locale, + new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(ArrayList 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 call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); + @Override + public void onFailure(Call call, Throwable t) { + String errorMessage = t == null ? "unknown error" : t.getMessage(); + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); + } + }); + } + + private void loadSubredditPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback) { + RedditAPI api = retrofit.create(RedditAPI.class); + Call getPost = api.getSubredditBestPosts(name, params.key); + getPost.enqueue(new Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + if(response.isSuccessful()) { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(ArrayList 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 getPost = api.getPost(subredditName, params.key); - getPost.enqueue(new Callback() { - @Override - public void onResponse(Call call, retrofit2.Response response) { - if(response.isSuccessful()) { - ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { - @Override - public void onParsePostSuccess(ArrayList 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 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 callback) { + RedditAPI api = retrofit.create(RedditAPI.class); + Call getPost = api.getUserBestPosts(name, null); + getPost.enqueue(new Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + if(response.isSuccessful()) { + ParsePost.parsePost(response.body(), locale, + new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(ArrayList 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 call, Throwable t) { + String errorMessage = t == null ? "unknown error" : t.getMessage(); + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); + } + }); + } - @Override - public void onFailure(Call call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); + private void loadUserPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback) { + RedditAPI api = retrofit.create(RedditAPI.class); + Call getPost = api.getUserBestPosts(name, params.key); + getPost.enqueue(new Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + if(response.isSuccessful()) { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(ArrayList 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 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 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 { @@ -355,7 +355,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter initialLoadingState; private LiveData> 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 create(@NonNull Class 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 voteThing(@HeaderMap Map headers, @FieldMap Map params); @GET("best?raw_json=1") - Call getBestPost(@Query("after") String lastItem, @HeaderMap Map headers); + Call getBestPosts(@Query("after") String lastItem, @HeaderMap Map headers); @GET("r/{subredditName}.json?raw_json=1&limit=25") - Call getPost(@Path("subredditName") String subredditName, @Query("after") String lastItem); + Call getSubredditBestPosts(@Path("subredditName") String subredditName, @Query("after") String lastItem); + + @GET("user/{userName}.json?raw_json=1&limit=25") + Call getUserBestPosts(@Path("userName") String userName, @Query("after") String lastItem); @GET("user/{username}/about.json?raw_json=1") Call 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 -- cgit v1.2.3