From b997963808a418948e46841c406f03deafd21f94 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 17 Nov 2019 23:41:58 +0800 Subject: Add a Fab to scroll down parent comments. Use volume up and down keys to navigate parent comments. --- .../Activity/ViewPostDetailActivity.java | 65 +++++++++++++++++++++- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 38 +++++++++++++ .../Utils/SharedPreferencesUtils.java | 1 + .../res/drawable/ic_keyboard_arrow_down_24dp.xml | 9 +++ .../main/res/layout/activity_view_post_detail.xml | 10 ++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/main_preferences.xml | 11 +++- 7 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/drawable/ic_keyboard_arrow_down_24dp.xml 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 66457f9a..11b99830 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -6,6 +6,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -23,6 +24,7 @@ import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; @@ -30,6 +32,7 @@ import com.bumptech.glide.RequestManager; import com.evernote.android.state.State; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import com.livefront.bridge.Bridge; import com.r0adkll.slidr.Slidr; @@ -142,6 +145,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS ImageView mFetchPostInfoImageView; @BindView(R.id.fetch_post_info_text_view_view_post_detail_activity) TextView mFetchPostInfoTextView; + @BindView(R.id.fab_view_post_detail_activity) + FloatingActionButton fab; @Inject @Named("no_oauth") Retrofit mRetrofit; @@ -164,8 +169,10 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS private boolean mShowElapsedTime; private boolean showToast = false; private boolean isSortingComments = false; + private boolean mVolumeKeysNavigateComments; private LinearLayoutManager mLinearLayoutManager; private CommentAndPostRecyclerViewAdapter mAdapter; + private RecyclerView.SmoothScroller mSmoothScroller; private PostCommentSortTypeBottomSheetFragment postCommentSortTypeBottomSheetFragment; @Override @@ -241,12 +248,19 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mNeedBlurSpoiler = mSharedPreferences.getBoolean(SharedPreferencesUtils.BLUR_SPOILER_KEY, false); mVoteButtonsOnTheRight = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOTE_BUTTONS_ON_THE_RIGHT_KEY, false); mShowElapsedTime = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY, false); + mVolumeKeysNavigateComments = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOLUME_KEYS_NAVIGATE_COMMENTS, false); mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; mLinearLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLinearLayoutManager); + mSmoothScroller = new LinearSmoothScroller(this) { + @Override + protected int getVerticalSnapPreference() { + return LinearSmoothScroller.SNAP_TO_START; + } + }; mSingleCommentId = getIntent().getStringExtra(EXTRA_SINGLE_COMMENT_ID); if (savedInstanceState == null) { @@ -433,6 +447,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } } } + + fab.setOnClickListener(view -> scrollToNextParentComment()); } @@ -965,6 +981,38 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS refresh(false, true); } + public void scrollToNextParentComment() { + if (mLinearLayoutManager != null) { + int currentPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); + if (mAdapter != null) { + int nextParentPosition = mAdapter.getNextParentCommentPosition(currentPosition); + if (nextParentPosition < 0) { + return; + } + mSmoothScroller.setTargetPosition(nextParentPosition); + if (mLinearLayoutManager != null) { + mLinearLayoutManager.startSmoothScroll(mSmoothScroller); + } + } + } + } + + public void scrollToPreviousParentComment() { + if (mLinearLayoutManager != null) { + int currentPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); + if (mAdapter != null) { + int nextParentPosition = mAdapter.getPreviousParentCommentPosition(currentPosition); + if (nextParentPosition < 0) { + return; + } + mSmoothScroller.setTargetPosition(nextParentPosition); + if (mLinearLayoutManager != null) { + mLinearLayoutManager.startSmoothScroll(mSmoothScroller); + } + } + } + } + @Subscribe public void onPostUpdateEvent(PostUpdateEventToDetailActivity event) { if (mPost.getId().equals(event.post.getId())) { @@ -1294,7 +1342,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS refresh(true, false); } } else if (requestCode == EDIT_COMMENT_REQUEST_CODE) { - if (resultCode == RESULT_OK) { + if (data != null && resultCode == RESULT_OK) { mAdapter.editComment(data.getStringExtra(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT), data.getExtras().getInt(EditCommentActivity.EXTRA_EDITED_COMMENT_POSITION)); } @@ -1324,6 +1372,21 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS Bridge.clear(this); } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (mVolumeKeysNavigateComments) { + switch (keyCode) { + case KeyEvent.KEYCODE_VOLUME_UP: + scrollToPreviousParentComment(); + return true; + case KeyEvent.KEYCODE_VOLUME_DOWN: + scrollToNextParentComment(); + return true; + } + } + return super.onKeyDown(keyCode, event); + } + @Override public void flairSelected(Flair flair) { Map params = new HashMap<>(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index da09328f..3fcea5d7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -1437,6 +1437,44 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter= 0; i--) { + if (mVisibleComments.get(i - 2).getDepth() == 0) { + return i; + } + } + } else { + for (int i = currentPosition - 1; i - 1 >= 0; i--) { + if (mVisibleComments.get(i - 1).getDepth() == 0) { + return i; + } + } + } + } + return -1; + } + @Override public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { if (holder instanceof CommentViewHolder) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index aa612bed..8333db44 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -58,4 +58,5 @@ public class SharedPreferencesUtils { public static final String PULL_NOTIFICATION_TIME = "pull_notification_time"; public static final String SHOW_ELAPSED_TIME_KEY = "show_elapsed_time"; public static final String SWIPE_RIGHT_TO_GO_BACK_FROM_POST_DETAIL = "swipe_to_go_back_from_post_detail"; + public static final String VOLUME_KEYS_NAVIGATE_COMMENTS = "volume_keys_navigate_comments"; } diff --git a/app/src/main/res/drawable/ic_keyboard_arrow_down_24dp.xml b/app/src/main/res/drawable/ic_keyboard_arrow_down_24dp.xml new file mode 100644 index 00000000..9f2d9ba3 --- /dev/null +++ b/app/src/main/res/drawable/ic_keyboard_arrow_down_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_view_post_detail.xml b/app/src/main/res/layout/activity_view_post_detail.xml index eb48f340..98794359 100644 --- a/app/src/main/res/layout/activity_view_post_detail.xml +++ b/app/src/main/res/layout/activity_view_post_detail.xml @@ -70,4 +70,14 @@ + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 88228a44..9452003c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -289,6 +289,7 @@ Amoled Dark Immersive Interface Vote Buttons on the Right + Use Volume Keys to Navigate Comments in Posts Show Elpased Time in Posts and Comments Swipe Right to Go Back From Comments Lazy Mode Interval diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index db4b75a1..894642fa 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -29,17 +29,22 @@ + app:title="@string/settings_vote_buttons_on_the_right_title" /> + app:title="@string/settings_show_elapsed_time" /> + app:title="@string/swipe_to_go_back_from_post_detail" /> + +