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) {
searchPanelMaterialCardView.setVisibility(View.VISIBLE);
return false;
} else {
searchPanelMaterialCardView.setVisibility(View.GONE);
return true;
}
}

View File

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

View File

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

View File

@ -52,13 +52,14 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="#FFFFFF"
android:visibility="gone"
app:cardElevation="16dp">
<LinearLayout
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
android:id="@+id/search_text_input_layout_view_post_detail_activity"