From 67d4d9cc4f887a87450bbdca041a75a41a103d2b Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 12 Jul 2021 19:41:52 +0800 Subject: Searching comments in ViewPostDetailActivity is available. --- .../activities/ViewPostDetailActivity.java | 26 +++++++++++ .../adapters/CommentsRecyclerViewAdapter.java | 17 ++++++++ .../fragments/ViewPostDetailFragment.java | 50 ++++++++++++++++++++++ 3 files changed, 93 insertions(+) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index f093c1f6..a00f4c8d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -300,6 +300,28 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele if (savedInstanceState == null) { viewPager2.setCurrentItem(getIntent().getIntExtra(EXTRA_POST_LIST_POSITION, 0), false); } + nextResultImageView.setOnClickListener(view -> { + ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); + if (fragment != null) { + searchComment(fragment, true); + } + }); + + previousResultImageView.setOnClickListener(view -> { + ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); + if (fragment != null) { + searchComment(fragment, false); + } + }); + + closeSearchPanelImageView.setOnClickListener(view -> { + ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); + if (fragment != null) { + fragment.resetSearchCommentIndex(); + } + + searchPanelMaterialCardView.setVisibility(View.GONE); + }); } public boolean isNsfwSubreddit() { @@ -396,6 +418,10 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele } } + public void searchComment(ViewPostDetailFragment fragment, boolean searchNextComment) { + fragment.searchComment(searchTextInputEditText.getText().toString(), searchNextComment); + } + @Subscribe public void onAccountSwitchEvent(SwitchAccountEvent event) { if (!getClass().getName().equals(event.excludeActivityClassName)) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index f88012b9..7e00a97f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -139,6 +139,8 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter visibleComments = mCommentsAdapter.getVisibleComments(); + int currentSearchIndex = mCommentsAdapter.getSearchCommentIndex(); + if (visibleComments != null) { + if (searchNextComment) { + for (int i = currentSearchIndex + 1; i < visibleComments.size(); i++) { + if (visibleComments.get(i).getCommentRawText() != null && visibleComments.get(i).getCommentRawText().contains(query)) { + if (mCommentsAdapter != null) { + if (mCommentsRecyclerView == null) { + mRecyclerView.smoothScrollToPosition(i + 1); + } else { + mCommentsRecyclerView.smoothScrollToPosition(i); + } + mCommentsAdapter.highlightSearchResult(i); + mCommentsAdapter.notifyItemChanged(i); + } + return; + } + } + + return; + } else { + for (int i = currentSearchIndex - 1; i >= 0; i--) { + if (visibleComments.get(i).getCommentRawText() !=null && visibleComments.get(i).getCommentRawText().contains(query)) { + if (mCommentsAdapter != null) { + if (mCommentsRecyclerView == null) { + mRecyclerView.smoothScrollToPosition(i + 1); + } else { + mCommentsRecyclerView.smoothScrollToPosition(i); + } + mCommentsAdapter.highlightSearchResult(i); + mCommentsAdapter.notifyItemChanged(i); + } + return; + } + } + + return; + } + } + } + } + + public void resetSearchCommentIndex() { + if (mCommentsAdapter != null) { + mCommentsAdapter.resetCommentSearchIndex(); + } + } + @Override public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { inflater.inflate(R.menu.view_post_detail_fragment, menu); -- cgit v1.2.3