diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-10-19 11:26:46 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-10-19 11:26:46 +0000 |
commit | c9cbc6b5ffaeb3eb2fc4d87c0bc14d04f9676afc (patch) | |
tree | 35551506fb0d291082120527d9149645c736c18a /app/src/main/java | |
parent | 6b49a8f8e107b532465f8f1e9425abdd2a2ab8ec (diff) | |
download | infinity-for-reddit-c9cbc6b5ffaeb3eb2fc4d87c0bc14d04f9676afc.tar infinity-for-reddit-c9cbc6b5ffaeb3eb2fc4d87c0bc14d04f9676afc.tar.gz infinity-for-reddit-c9cbc6b5ffaeb3eb2fc4d87c0bc14d04f9676afc.tar.bz2 infinity-for-reddit-c9cbc6b5ffaeb3eb2fc4d87c0bc14d04f9676afc.tar.lz infinity-for-reddit-c9cbc6b5ffaeb3eb2fc4d87c0bc14d04f9676afc.tar.xz infinity-for-reddit-c9cbc6b5ffaeb3eb2fc4d87c0bc14d04f9676afc.tar.zst infinity-for-reddit-c9cbc6b5ffaeb3eb2fc4d87c0bc14d04f9676afc.zip |
Sorting post comments is now available.
Diffstat (limited to 'app/src/main/java')
6 files changed, 233 insertions, 49 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 12b4f908..1c497ea9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -61,6 +61,7 @@ import ml.docilealligator.infinityforreddit.FetchComment; import ml.docilealligator.infinityforreddit.FetchPost; import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Fragment.FlairBottomSheetFragment; +import ml.docilealligator.infinityforreddit.Fragment.PostCommentSortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.HidePost; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.ParseComment; @@ -73,6 +74,8 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditUtils; import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; +import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -81,7 +84,8 @@ import retrofit2.Retrofit; import static ml.docilealligator.infinityforreddit.Activity.CommentActivity.EXTRA_COMMENT_DATA_KEY; import static ml.docilealligator.infinityforreddit.Activity.CommentActivity.WRITE_COMMENT_REQUEST_CODE; -public class ViewPostDetailActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback { +public class ViewPostDetailActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback, + SortTypeSelectionCallback { public static final String EXTRA_POST_DATA = "EPD"; public static final String EXTRA_POST_LIST_POSITION = "EPLI"; @@ -154,8 +158,10 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS private boolean mNeedBlurNsfw; private boolean mNeedBlurSpoiler; private boolean showToast = false; + private boolean isSortingComments = false; private LinearLayoutManager mLinearLayoutManager; private CommentAndPostRecyclerViewAdapter mAdapter; + private PostCommentSortTypeBottomSheetFragment postCommentSortTypeBottomSheetFragment; @Override protected void onCreate(Bundle savedInstanceState) { @@ -251,6 +257,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS if (getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) { postListPosition = getIntent().getIntExtra(EXTRA_POST_LIST_POSITION, -1); } + + postCommentSortTypeBottomSheetFragment = new PostCommentSortTypeBottomSheetFragment(); } @Override @@ -321,6 +329,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); + mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); if (mAccessToken != null) { if (mPost.isSaved()) { @@ -418,20 +427,25 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mProgressBar.setVisibility(View.VISIBLE); mGlide.clear(mFetchPostInfoImageView); + String sortType = mSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.BEST.value).toLowerCase(); + + Call<String> postAndComments; if (mAccessToken == null) { if (isSingleCommentThreadMode && mSingleCommentId != null) { - postAndComments = mRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadById(subredditId, mSingleCommentId); + postAndComments = mRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadById( + subredditId, mSingleCommentId, sortType); } else { - postAndComments = mRetrofit.create(RedditAPI.class).getPostAndCommentsById(subredditId); + postAndComments = mRetrofit.create(RedditAPI.class).getPostAndCommentsById(subredditId, + sortType); } } else { if (isSingleCommentThreadMode && mSingleCommentId != null) { postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadByIdOauth(subredditId, - mSingleCommentId, RedditUtils.getOAuthHeader(mAccessToken)); + mSingleCommentId, sortType, RedditUtils.getOAuthHeader(mAccessToken)); } else { postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsByIdOauth(subredditId, - RedditUtils.getOAuthHeader(mAccessToken)); + sortType, RedditUtils.getOAuthHeader(mAccessToken)); } } postAndComments.enqueue(new Callback<String>() { @@ -450,6 +464,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); + mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); if (mAccessToken != null) { if (post.isSaved()) { @@ -560,7 +575,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS }); } - private void fetchComments(boolean changeRefreshState) { + private void fetchComments(boolean changeRefreshState, boolean checkSortState, String sortType) { mAdapter.setSingleComment(mSingleCommentId, isSingleCommentThreadMode); mAdapter.initiallyLoading(); String commentId = null; @@ -569,47 +584,70 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; - FetchComment.fetchComments(retrofit, mAccessToken, mPost.getId(), commentId, mLocale, new FetchComment.FetchCommentListener() { - @Override - public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments, - String parentId, ArrayList<String> children) { - ViewPostDetailActivity.this.children = children; + FetchComment.fetchComments(retrofit, mAccessToken, mPost.getId(), commentId, sortType, mLocale, + new FetchComment.FetchCommentListener() { + @Override + public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments, + String parentId, ArrayList<String> children) { + if (checkSortState && isSortingComments) { + if (changeRefreshState) { + isRefreshing = false; + } + + return; + } - comments = expandedComments; - hasMoreChildren = children.size() != 0; - mAdapter.addComments(expandedComments, hasMoreChildren); + ViewPostDetailActivity.this.children = children; - if (children.size() > 0) { - mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); + comments = expandedComments; + hasMoreChildren = children.size() != 0; + mAdapter.addComments(expandedComments, hasMoreChildren); - if (!isLoadingMoreChildren && loadMoreChildrenSuccess) { - int visibleItemCount = mLinearLayoutManager.getChildCount(); - int totalItemCount = mLinearLayoutManager.getItemCount(); - int firstVisibleItemPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); + if (children.size() > 0) { + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); - if ((visibleItemCount + firstVisibleItemPosition >= totalItemCount) && firstVisibleItemPosition >= 0) { - fetchMoreComments(); + if (!isLoadingMoreChildren && loadMoreChildrenSuccess) { + int visibleItemCount = mLinearLayoutManager.getChildCount(); + int totalItemCount = mLinearLayoutManager.getItemCount(); + int firstVisibleItemPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); + + if ((visibleItemCount + firstVisibleItemPosition >= totalItemCount) && firstVisibleItemPosition >= 0) { + fetchMoreComments(); + } + } } + }); + } + if (changeRefreshState) { + isRefreshing = false; + } + } + + @Override + public void onFetchCommentFailed() { + if (checkSortState && isSortingComments) { + if (changeRefreshState) { + isRefreshing = false; } + + return; } - }); - } - if (changeRefreshState) { - isRefreshing = false; - } - } - @Override - public void onFetchCommentFailed() { - mAdapter.initiallyLoadCommentsFailed(); - if (changeRefreshState) { - isRefreshing = false; - } - } - }); + mAdapter.initiallyLoadCommentsFailed(); + if (changeRefreshState) { + isRefreshing = false; + } + } + }); + } + + private void fetchComments(boolean changeRefreshState) { + String sortType = mSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, + SortType.Type.BEST.value).toLowerCase(); + fetchComments(changeRefreshState, true, sortType); } void fetchMoreComments() { @@ -676,6 +714,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); + mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); if (mAccessToken != null) { if (post.isSaved()) { @@ -970,6 +1009,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); + mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); if (mAccessToken != null) { if (mPost.isSaved()) { @@ -1098,6 +1138,11 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } } return true; + case R.id.action_sort_view_post_detail_activity: + if (mPost != null) { + postCommentSortTypeBottomSheetFragment.show(getSupportFragmentManager(), postCommentSortTypeBottomSheetFragment.getTag()); + } + return true; case R.id.action_view_crosspost_parent_view_post_detail_activity: Intent crosspostIntent = new Intent(this, ViewPostDetailActivity.class); crosspostIntent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, mPost.getCrosspostParentId()); @@ -1286,4 +1331,14 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } }); } + + @Override + public void sortTypeSelected(SortType sortType) { + mFetchPostInfoLinearLayout.setVisibility(View.GONE); + mGlide.clear(mFetchPostInfoImageView); + mChildrenStartingIndex = 0; + children.clear(); + fetchComments(false, false, sortType.getType().value); + mSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, sortType.getType().name()).apply(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index b3cc8115..68255c21 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -12,21 +12,23 @@ import retrofit2.Response; import retrofit2.Retrofit; public class FetchComment { - public static void fetchComments(Retrofit retrofit, @Nullable String accessToken, String article, String commentId, - Locale locale, FetchCommentListener fetchCommentListener) { + public static void fetchComments(Retrofit retrofit, @Nullable String accessToken, String article, + String commentId, String sortType, Locale locale, + FetchCommentListener fetchCommentListener) { RedditAPI api = retrofit.create(RedditAPI.class); Call<String> comments; if (accessToken == null) { if (commentId == null) { - comments = api.getPostAndCommentsById(article); + comments = api.getPostAndCommentsById(article, sortType); } else { - comments = api.getPostAndCommentsSingleThreadById(article, commentId); + comments = api.getPostAndCommentsSingleThreadById(article, commentId, sortType); } } else { if (commentId == null) { - comments = api.getPostAndCommentsByIdOauth(article, RedditUtils.getOAuthHeader(accessToken)); + comments = api.getPostAndCommentsByIdOauth(article, sortType, RedditUtils.getOAuthHeader(accessToken)); } else { - comments = api.getPostAndCommentsSingleThreadByIdOauth(article, commentId, RedditUtils.getOAuthHeader(accessToken)); + comments = api.getPostAndCommentsSingleThreadByIdOauth(article, commentId, sortType, + RedditUtils.getOAuthHeader(accessToken)); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostCommentSortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostCommentSortTypeBottomSheetFragment.java new file mode 100644 index 00000000..2b78a220 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostCommentSortTypeBottomSheetFragment.java @@ -0,0 +1,119 @@ +package ml.docilealligator.infinityforreddit.Fragment; + + +import android.app.Activity; +import android.content.Context; +import android.content.res.Configuration; +import android.os.Build; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; + +import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; + +/** + * A simple {@link Fragment} subclass. + */ +public class PostCommentSortTypeBottomSheetFragment extends RoundedBottomSheetDialogFragment { + + @BindView(R.id.best_type_text_view_post_comment_sort_type_bottom_sheet_fragment) + TextView bestTypeTextView; + @BindView(R.id.confidence_type_text_view_post_comment_sort_type_bottom_sheet_fragment) + TextView confidenceTypeTextView; + @BindView(R.id.top_type_text_view_post_comment_sort_type_bottom_sheet_fragment) + TextView topTypeTextView; + @BindView(R.id.new_type_text_view_post_comment_sort_type_bottom_sheet_fragment) + TextView newTypeTextView; + @BindView(R.id.controversial_type_text_view_post_comment_sort_type_bottom_sheet_fragment) + TextView controversialTypeTextView; + @BindView(R.id.old_type_text_view_post_comment_sort_type_bottom_sheet_fragment) + TextView oldTypeTextView; + @BindView(R.id.random_type_text_view_post_comment_sort_type_bottom_sheet_fragment) + TextView randomTypeTextView; + @BindView(R.id.qa_type_text_view_post_comment_sort_type_bottom_sheet_fragment) + TextView qaTypeTextView; + @BindView(R.id.live_type_text_view_post_comment_sort_type_bottom_sheet_fragment) + TextView liveTypeTextView; + private Activity activity; + public PostCommentSortTypeBottomSheetFragment() { + // Required empty public constructor + } + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View rootView = inflater.inflate(R.layout.fragment_post_comment_sort_type_bottom_sheet, container, false); + ButterKnife.bind(this, rootView); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + && (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { + rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + + bestTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.BEST)); + dismiss(); + }); + + confidenceTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.CONFIDENCE)); + dismiss(); + }); + + topTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.TOP)); + dismiss(); + }); + + newTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.NEW)); + dismiss(); + }); + + controversialTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.CONTROVERSIAL)); + dismiss(); + }); + + oldTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.OLD)); + dismiss(); + }); + + randomTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.RANDOM)); + dismiss(); + }); + + qaTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.QA)); + dismiss(); + }); + + liveTypeTextView.setOnClickListener(view -> { + ((SortTypeSelectionCallback) activity).sortTypeSelected(new SortType(SortType.Type.LIVE)); + dismiss(); + }); + + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + this.activity = (Activity) context; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index a5fd0844..52fdb0ed 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -193,16 +193,19 @@ public interface RedditAPI { @GET("/comments/{id}/placeholder/{singleCommentId}.json?context=8&raw_json=1") Call<String> getPostAndCommentsSingleThreadByIdOauth(@Path("id") String id, @Path("singleCommentId") String singleCommentId, + @Query("sort") String sortType, @HeaderMap Map<String, String> headers); @GET("/comments/{id}.json?raw_json=1") - Call<String> getPostAndCommentsByIdOauth(@Path("id") String id, @HeaderMap Map<String, String> headers); + Call<String> getPostAndCommentsByIdOauth(@Path("id") String id, @Query("sort") String sortType, + @HeaderMap Map<String, String> headers); @GET("/comments/{id}/placeholder/{singleCommentId}.json?context=8&raw_json=1") - Call<String> getPostAndCommentsSingleThreadById(@Path("id") String id, @Path("singleCommentId") String singleCommentId); + Call<String> getPostAndCommentsSingleThreadById(@Path("id") String id, @Path("singleCommentId") String singleCommentId, + @Query("sort") String sortType); @GET("/comments/{id}.json?raw_json=1") - Call<String> getPostAndCommentsById(@Path("id") String id); + Call<String> getPostAndCommentsById(@Path("id") String id, @Query("sort") String sortType); @Multipart @POST(".") diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java index 372eaabb..334aff47 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java @@ -45,4 +45,5 @@ public class SharedPreferencesUtils { public static final String SORT_TIME_USER_COMMENT = "sort_time_user_comment"; public static final String SORT_TYPE_SEARCH_SUBREDDIT = "sort_type_search_subreddit"; public static final String SORT_TYPE_SEARCH_USER = "sort_type_search_user"; + public static final String SORT_TYPE_POST_COMMENT = "sort_type_post_comment"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java index 39502a2d..fe50d884 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java @@ -32,7 +32,11 @@ public class SortType { CONTROVERSIAL("controversial"), RELEVANCE("relevance"), COMMENTS("comments"), - ACTIVITY("activity"); + ACTIVITY("activity"), + CONFIDENCE("confidence"), + OLD("old"), + QA("qa"), + LIVE("live"); public final String value; |