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. --- app/src/main/AndroidManifest.xml | 3 +- .../activities/ViewPostDetailActivity.java | 26 +++++++++++ .../adapters/CommentsRecyclerViewAdapter.java | 17 ++++++++ .../fragments/ViewPostDetailFragment.java | 50 ++++++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f320cb64..fb659b23 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -406,7 +406,8 @@ + android:theme="@style/AppTheme.Slidable" + android:windowSoftInputMode="adjustPan" /> { + 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