Improve the experience of searching in comments.

This commit is contained in:
Alex Ning 2021-07-12 23:09:15 +08:00
parent 8c89603f19
commit 67416fa3e4
4 changed files with 31 additions and 19 deletions

View File

@ -424,11 +424,13 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
} }
} }
public void toggleSearchPanelVisibility() { public boolean toggleSearchPanelVisibility() {
if (searchPanelMaterialCardView.getVisibility() == View.GONE) { if (searchPanelMaterialCardView.getVisibility() == View.GONE) {
searchPanelMaterialCardView.setVisibility(View.VISIBLE); searchPanelMaterialCardView.setVisibility(View.VISIBLE);
return false;
} else { } else {
searchPanelMaterialCardView.setVisibility(View.GONE); searchPanelMaterialCardView.setVisibility(View.GONE);
return true;
} }
} }

View File

@ -13,6 +13,7 @@ import android.text.Spanned;
import android.text.TextPaint; import android.text.TextPaint;
import android.text.style.ClickableSpan; import android.text.style.ClickableSpan;
import android.text.util.Linkify; import android.text.util.Linkify;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -314,14 +315,12 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
@Override @Override
public int getItemViewType(int position) { public int getItemViewType(int position) {
if (mVisibleComments.size() == 0) { if (mVisibleComments.size() == 0) {
if (position == 0) { if (isInitiallyLoading) {
if (isInitiallyLoading) { return VIEW_TYPE_FIRST_LOADING;
return VIEW_TYPE_FIRST_LOADING; } else if (isInitiallyLoadingFailed) {
} else if (isInitiallyLoadingFailed) { return VIEW_TYPE_FIRST_LOADING_FAILED;
return VIEW_TYPE_FIRST_LOADING_FAILED; } else {
} else { return VIEW_TYPE_NO_COMMENT_PLACEHOLDER;
return VIEW_TYPE_NO_COMMENT_PLACEHOLDER;
}
} }
} }
@ -400,6 +399,8 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
if (comment != null) { if (comment != null) {
if (position == mSearchCommentIndex) { if (position == mSearchCommentIndex) {
holder.itemView.setBackgroundColor(Color.parseColor("#03A9F4")); holder.itemView.setBackgroundColor(Color.parseColor("#03A9F4"));
} else {
holder.itemView.setBackgroundColor(mCommentBackgroundColor);
} }
if (mIsSingleCommentThreadMode && comment.getId().equals(mSingleCommentId)) { if (mIsSingleCommentThreadMode && comment.getId().equals(mSingleCommentId)) {
holder.itemView.setBackgroundColor(mSingleCommentThreadBackgroundColor); holder.itemView.setBackgroundColor(mSingleCommentThreadBackgroundColor);
@ -900,13 +901,14 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
} }
public void initiallyLoading() { public void initiallyLoading() {
resetCommentSearchIndex();
if (mVisibleComments.size() != 0) { if (mVisibleComments.size() != 0) {
int previousSize = mVisibleComments.size(); int previousSize = mVisibleComments.size();
mVisibleComments.clear(); mVisibleComments.clear();
if (mIsSingleCommentThreadMode) { if (mIsSingleCommentThreadMode) {
notifyItemRangeRemoved(0, previousSize + 1); notifyItemRangeRemoved(0, previousSize + ((mHasMoreComments || loadMoreCommentsFailed) ? 1 : 0) + 1);
} else { } else {
notifyItemRangeRemoved(0, previousSize); notifyItemRangeRemoved(0, previousSize + ((mHasMoreComments || loadMoreCommentsFailed) ? 1 : 0));
} }
} }
@ -1056,6 +1058,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
@Override @Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof CommentViewHolder) { if (holder instanceof CommentViewHolder) {
holder.itemView.setBackgroundColor(mCommentBackgroundColor);
((CommentViewHolder) holder).authorTextView.setTextColor(mUsernameColor); ((CommentViewHolder) holder).authorTextView.setTextColor(mUsernameColor);
((CommentViewHolder) holder).authorFlairTextView.setVisibility(View.GONE); ((CommentViewHolder) holder).authorFlairTextView.setVisibility(View.GONE);
((CommentViewHolder) holder).authorTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); ((CommentViewHolder) holder).authorTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
@ -1067,13 +1070,13 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
((CommentViewHolder) holder).scoreTextView.setTextColor(mCommentIconAndInfoColor); ((CommentViewHolder) holder).scoreTextView.setTextColor(mCommentIconAndInfoColor);
((CommentViewHolder) holder).downvoteButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).downvoteButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).replyButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).replyButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).itemView.setBackgroundColor(mCommentBackgroundColor);
} }
} }
@Override @Override
public int getItemCount() { public int getItemCount() {
if (isInitiallyLoading || isInitiallyLoadingFailed || mVisibleComments.size() == 0) { if (isInitiallyLoading || isInitiallyLoadingFailed || mVisibleComments.size() == 0) {
Log.i("adfasdf", "sds");
return 1; return 1;
} }

View File

@ -12,6 +12,7 @@ import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log;
import android.view.HapticFeedbackConstants; import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -770,18 +771,21 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
if (mCommentsAdapter != null) { if (mCommentsAdapter != null) {
ArrayList<Comment> visibleComments = mCommentsAdapter.getVisibleComments(); ArrayList<Comment> visibleComments = mCommentsAdapter.getVisibleComments();
int currentSearchIndex = mCommentsAdapter.getSearchCommentIndex(); int currentSearchIndex = mCommentsAdapter.getSearchCommentIndex();
if (currentSearchIndex >= 0) {
mCommentsAdapter.notifyItemChanged(currentSearchIndex);
}
if (visibleComments != null) { if (visibleComments != null) {
if (searchNextComment) { if (searchNextComment) {
for (int i = currentSearchIndex + 1; i < visibleComments.size(); i++) { for (int i = currentSearchIndex + 1; i < visibleComments.size(); i++) {
if (visibleComments.get(i).getCommentRawText() != null && visibleComments.get(i).getCommentRawText().contains(query)) { if (visibleComments.get(i).getCommentRawText() != null && visibleComments.get(i).getCommentRawText().contains(query)) {
if (mCommentsAdapter != null) { if (mCommentsAdapter != null) {
mCommentsAdapter.highlightSearchResult(i);
mCommentsAdapter.notifyItemChanged(i);
if (mCommentsRecyclerView == null) { if (mCommentsRecyclerView == null) {
mRecyclerView.smoothScrollToPosition(i + 1); mRecyclerView.smoothScrollToPosition(i + 1);
} else { } else {
mCommentsRecyclerView.smoothScrollToPosition(i); mCommentsRecyclerView.smoothScrollToPosition(i);
} }
mCommentsAdapter.highlightSearchResult(i);
mCommentsAdapter.notifyItemChanged(i);
} }
return; return;
} }
@ -792,13 +796,13 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
for (int i = currentSearchIndex - 1; i >= 0; i--) { for (int i = currentSearchIndex - 1; i >= 0; i--) {
if (visibleComments.get(i).getCommentRawText() !=null && visibleComments.get(i).getCommentRawText().contains(query)) { if (visibleComments.get(i).getCommentRawText() !=null && visibleComments.get(i).getCommentRawText().contains(query)) {
if (mCommentsAdapter != null) { if (mCommentsAdapter != null) {
mCommentsAdapter.highlightSearchResult(i);
mCommentsAdapter.notifyItemChanged(i);
if (mCommentsRecyclerView == null) { if (mCommentsRecyclerView == null) {
mRecyclerView.smoothScrollToPosition(i + 1); mRecyclerView.smoothScrollToPosition(i + 1);
} else { } else {
mCommentsRecyclerView.smoothScrollToPosition(i); mCommentsRecyclerView.smoothScrollToPosition(i);
} }
mCommentsAdapter.highlightSearchResult(i);
mCommentsAdapter.notifyItemChanged(i);
} }
return; return;
} }
@ -830,7 +834,9 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId(); int itemId = item.getItemId();
if (itemId == R.id.action_search_view_post_detail_fragment) { if (itemId == R.id.action_search_view_post_detail_fragment) {
activity.toggleSearchPanelVisibility(); if (activity.toggleSearchPanelVisibility() && mCommentsAdapter != null) {
mCommentsAdapter.resetCommentSearchIndex();
}
} else if (itemId == R.id.action_refresh_view_post_detail_fragment) { } else if (itemId == R.id.action_refresh_view_post_detail_fragment) {
refresh(true, true); refresh(true, true);
return true; return true;

View File

@ -52,13 +52,14 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:backgroundTint="#FFFFFF"
android:visibility="gone" android:visibility="gone"
app:cardElevation="16dp"> app:cardElevation="16dp">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:paddingStart="0dp"
android:paddingEnd="8dp">
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/search_text_input_layout_view_post_detail_activity" android:id="@+id/search_text_input_layout_view_post_detail_activity"