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 <activity
android:name=".activities.ViewPostDetailActivity" android:name=".activities.ViewPostDetailActivity"
android:parentActivityName=".activities.MainActivity" android:parentActivityName=".activities.MainActivity"
android:theme="@style/AppTheme.Slidable" /> android:theme="@style/AppTheme.Slidable"
android:windowSoftInputMode="adjustPan" />
<activity <activity
android:name=".activities.ViewSubredditDetailActivity" android:name=".activities.ViewSubredditDetailActivity"
android:parentActivityName=".activities.MainActivity" android:parentActivityName=".activities.MainActivity"

View File

@ -300,6 +300,28 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
if (savedInstanceState == null) { if (savedInstanceState == null) {
viewPager2.setCurrentItem(getIntent().getIntExtra(EXTRA_POST_LIST_POSITION, 0), false); 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() { 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 @Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) { public void onAccountSwitchEvent(SwitchAccountEvent event) {
if (!getClass().getName().equals(event.excludeActivityClassName)) { if (!getClass().getName().equals(event.excludeActivityClassName)) {

View File

@ -139,6 +139,8 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
private Drawable mCommentIcon; private Drawable mCommentIcon;
private int mSearchCommentIndex = -1;
public CommentsRecyclerViewAdapter(AppCompatActivity activity, ViewPostDetailFragment fragment, public CommentsRecyclerViewAdapter(AppCompatActivity activity, ViewPostDetailFragment fragment,
CustomThemeWrapper customThemeWrapper, CustomThemeWrapper customThemeWrapper,
Executor executor, Retrofit retrofit, Retrofit oauthRetrofit, Executor executor, Retrofit retrofit, Retrofit oauthRetrofit,
@ -396,6 +398,9 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
if (holder instanceof CommentViewHolder) { if (holder instanceof CommentViewHolder) {
Comment comment = getCurrentComment(position); Comment comment = getCurrentComment(position);
if (comment != null) { if (comment != null) {
if (position == mSearchCommentIndex) {
holder.itemView.setBackgroundColor(Color.parseColor("#03A9F4"));
}
if (mIsSingleCommentThreadMode && comment.getId().equals(mSingleCommentId)) { if (mIsSingleCommentThreadMode && comment.getId().equals(mSingleCommentId)) {
holder.itemView.setBackgroundColor(mSingleCommentThreadBackgroundColor); holder.itemView.setBackgroundColor(mSingleCommentThreadBackgroundColor);
} else if (comment.getAwards() != null && !comment.getAwards().equals("")) { } 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 @Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof CommentViewHolder) { 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 @Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.view_post_detail_fragment, menu); inflater.inflate(R.menu.view_post_detail_fragment, menu);