Searching comments in ViewPostDetailActivity is available.

This commit is contained in:
Alex Ning 2021-07-12 19:41:52 +08:00
parent 5fc3212f98
commit 67d4d9cc4f
4 changed files with 95 additions and 1 deletions

View File

@ -406,7 +406,8 @@
<activity
android:name=".activities.ViewPostDetailActivity"
android:parentActivityName=".activities.MainActivity"
android:theme="@style/AppTheme.Slidable" />
android:theme="@style/AppTheme.Slidable"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".activities.ViewSubredditDetailActivity"
android:parentActivityName=".activities.MainActivity"

View File

@ -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)) {

View File

@ -139,6 +139,8 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
private Drawable mCommentIcon;
private int mSearchCommentIndex = -1;
public CommentsRecyclerViewAdapter(AppCompatActivity activity, ViewPostDetailFragment fragment,
CustomThemeWrapper customThemeWrapper,
Executor executor, Retrofit retrofit, Retrofit oauthRetrofit,
@ -396,6 +398,9 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
if (holder instanceof CommentViewHolder) {
Comment comment = getCurrentComment(position);
if (comment != null) {
if (position == mSearchCommentIndex) {
holder.itemView.setBackgroundColor(Color.parseColor("#03A9F4"));
}
if (mIsSingleCommentThreadMode && comment.getId().equals(mSingleCommentId)) {
holder.itemView.setBackgroundColor(mSingleCommentThreadBackgroundColor);
} else if (comment.getAwards() != null && !comment.getAwards().equals("")) {
@ -1036,6 +1041,18 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
}
}
public int getSearchCommentIndex() {
return mSearchCommentIndex;
}
public void highlightSearchResult(int searchCommentIndex) {
mSearchCommentIndex = searchCommentIndex;
}
public void resetCommentSearchIndex() {
mSearchCommentIndex = -1;
}
@Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof CommentViewHolder) {

View File

@ -766,6 +766,56 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
}
}
public void searchComment(String query, boolean searchNextComment) {
if (mCommentsAdapter != null) {
ArrayList<Comment> 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);