Skip to content

Commit

Permalink
Clean up persistentsearchview module code
Browse files Browse the repository at this point in the history
  • Loading branch information
mars885 committed Aug 16, 2020
1 parent e6ebeee commit 2e962d4
Show file tree
Hide file tree
Showing 18 changed files with 838 additions and 1,487 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -35,62 +35,58 @@
/**
* A recycler view adapter implementation for suggestion items.
*/
public class SuggestionsRecyclerViewAdapter extends TrackableRecyclerViewAdapter<Long, SuggestionItem, SuggestionItem.ViewHolder> {
public class SuggestionsRecyclerViewAdapter extends TrackableRecyclerViewAdapter<
Long,
SuggestionItem,
SuggestionItem.ViewHolder
> {


private ItemResources mResources;
private ItemResources resources;

private OnItemClickListener<SuggestionItem> mOnItemClickListener;
private OnItemClickListener<SuggestionItem> mOnItemRemoveButtonClickListener;
private OnItemClickListener<SuggestionItem> onItemClickListener;
private OnItemClickListener<SuggestionItem> onItemRemoveButtonClickListener;




public SuggestionsRecyclerViewAdapter(Context context,
List<SuggestionItem> items,
@NonNull SuggestionItemResources resources) {
public SuggestionsRecyclerViewAdapter(
Context context,
List<SuggestionItem> items,
@NonNull SuggestionItemResources resources
) {
super(context, items);
setResources(resources);
}




@Override
protected final void assignListeners(SuggestionItem.ViewHolder holder, int position, SuggestionItem item) {
super.assignListeners(holder, position, item);

item.setOnItemClickListener(holder, mOnItemClickListener);
item.setOnItemRemoveButtonClickListener(holder, mOnItemRemoveButtonClickListener);
item.setOnItemClickListener(holder, onItemClickListener);
item.setOnItemRemoveButtonClickListener(holder, onItemRemoveButtonClickListener);
}




/**
* Sets the listener to invoke when the item is clicked.
*
* @param onItemClickListener The listener to set
*/
public final void setOnItemClickListener(OnItemClickListener<SuggestionItem> onItemClickListener) {
mOnItemClickListener = onItemClickListener;
this.onItemClickListener = onItemClickListener;
}




/**
* Sets the listener to invoke when the remove button is clicked.
*
* @param onItemRemoveButtonClickListener The listener to set
*/
public final void setOnItemRemoveButtonClickListener(OnItemClickListener<SuggestionItem> onItemRemoveButtonClickListener) {
mOnItemRemoveButtonClickListener = onItemRemoveButtonClickListener;
this.onItemRemoveButtonClickListener = onItemRemoveButtonClickListener;
}




/**
* Sets the resources to be used by the adapter.
*
Expand All @@ -99,19 +95,15 @@ public final void setOnItemRemoveButtonClickListener(OnItemClickListener<Suggest
public final void setResources(@NonNull SuggestionItemResources resources) {
Preconditions.nonNull(resources);

mResources = resources;
this.resources = resources;
notifyDataSetChanged();
}




@Override
public final ItemResources getResources() {
return mResources;
return resources;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
/**
* A recycler view item for a suggestion.
*/
public class SuggestionItem extends BaseItem<Suggestion, SuggestionItem.ViewHolder, SuggestionItemResources> implements Trackable<Long> {
public class SuggestionItem extends BaseItem<
Suggestion,
SuggestionItem.ViewHolder,
SuggestionItemResources
> implements Trackable<Long> {


/**
Expand All @@ -58,8 +62,6 @@ public class SuggestionItem extends BaseItem<Suggestion, SuggestionItem.ViewHold
public static final int MAIN_LAYOUT_ID = R.layout.persistent_search_view_suggestion_item_layout;




/**
* Creates a suggestion item with the specified text.
*
Expand All @@ -72,8 +74,6 @@ public static SuggestionItem of(@NonNull String suggestionText) {
}




/**
* Creates a suggestion item with the specified id and text.
*
Expand All @@ -93,72 +93,73 @@ public static SuggestionItem of(long id, @NonNull String suggestionText) {
}




public SuggestionItem(Suggestion itemModel) {
super(itemModel);
}




@Override
public ViewHolder init(Adapter adapter,
ViewGroup parent,
LayoutInflater inflater,
SuggestionItemResources resources) {
final View view = inflater.inflate(MAIN_LAYOUT_ID, parent, false);
return new ViewHolder(view);
public ViewHolder init(
Adapter adapter,
ViewGroup parent,
LayoutInflater inflater,
SuggestionItemResources resources
) {
return new ViewHolder(inflater.inflate(MAIN_LAYOUT_ID, parent, false));
}




@Override
public void bind(@Nullable Adapter adapter,
@NonNull ViewHolder viewHolder,
@Nullable SuggestionItemResources resources) {
public void bind(
@Nullable Adapter adapter,
@NonNull ViewHolder viewHolder,
@Nullable SuggestionItemResources resources
) {
super.bind(adapter, viewHolder, resources);

final Suggestion suggestion = getItemModel();
final boolean isRecentSearchSuggestion = Suggestion.TYPE_RECENT_SEARCH_SUGGESTION.equals(suggestion.getType());

// text related
viewHolder.mTextTv.setTextColor(resources.getTextColor());
handleText(viewHolder, resources);
bindText(viewHolder, resources);
bindIcon(isRecentSearchSuggestion, viewHolder, resources);
bindButton(isRecentSearchSuggestion, viewHolder, resources);
}

// icon related
viewHolder.mIconIv.setImageDrawable(Utils.getColoredDrawable(
viewHolder.mIconIv.getContext(),
(isRecentSearchSuggestion ? R.drawable.ic_history_black_24dp : R.drawable.ic_magnify_black_24dp),
(isRecentSearchSuggestion ? resources.getRecentSearchIconColor() : resources.getSearchSuggestionIconColor())
));

// remove button related
if(isRecentSearchSuggestion) {
viewHolder.mRemoveBtnIv.setImageDrawable(Utils.getColoredDrawable(
viewHolder.mRemoveBtnIv.getContext(),
R.drawable.ic_close_black_24dp,
resources.getIconColor()
));
makeVisible(viewHolder.mRemoveBtnIv);
} else {
makeGone(viewHolder.mRemoveBtnIv);
}
private void bindText(
ViewHolder viewHolder,
SuggestionItemResources resources
) {
viewHolder.textTv.setTextColor(resources.getTextColor());
handleText(viewHolder, resources);
}


private void bindIcon(
boolean isRecentSearchSuggestion,
ViewHolder viewHolder,
SuggestionItemResources resources
) {
viewHolder.iconIv.setImageDrawable(
Utils.getColoredDrawable(
viewHolder.iconIv.getContext(),
(isRecentSearchSuggestion ? R.drawable.ic_history_black_24dp : R.drawable.ic_magnify_black_24dp),
(isRecentSearchSuggestion ? resources.getRecentSearchIconColor() : resources.getSearchSuggestionIconColor())
)
);
}


private void handleText(ViewHolder viewHolder, SuggestionItemResources resources) {
final Suggestion suggestion = getItemModel();
final String text = suggestion.getText();
final int startIndex = suggestion.getText().toLowerCase().indexOf(resources.getCurrentQuery().toLowerCase());
final int endIndex = Math.min(resources.getCurrentQuery().length(), text.length());
final boolean isCurrentQueryValid = !TextUtils.isEmpty(resources.getCurrentQuery());
final boolean isStartIndexValid = (startIndex != -1);
final boolean isEndIndexValid = (startIndex <= endIndex);

if(!TextUtils.isEmpty(resources.getCurrentQuery())
&& (startIndex != -1)
&& (startIndex <= endIndex)) {
if(isCurrentQueryValid && isStartIndexValid && isEndIndexValid) {
final SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(
new ForegroundColorSpan(resources.getSelectedTextColor()),
Expand All @@ -167,13 +168,31 @@ private void handleText(ViewHolder viewHolder, SuggestionItemResources resources
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);

viewHolder.mTextTv.setText(spannableString);
viewHolder.textTv.setText(spannableString);
} else {
viewHolder.mTextTv.setText(text);
viewHolder.textTv.setText(text);
}
}


private void bindButton(
boolean isRecentSearchSuggestion,
ViewHolder viewHolder,
SuggestionItemResources resources
) {
if(isRecentSearchSuggestion) {
viewHolder.removeBtnIv.setImageDrawable(
Utils.getColoredDrawable(
viewHolder.removeBtnIv.getContext(),
R.drawable.ic_close_black_24dp,
resources.getIconColor()
)
);
makeVisible(viewHolder.removeBtnIv);
} else {
makeGone(viewHolder.removeBtnIv);
}
}


/**
Expand All @@ -182,74 +201,72 @@ private void handleText(ViewHolder viewHolder, SuggestionItemResources resources
* @param viewHolder The view holder
* @param onItemClickListener The listener to set
*/
public void setOnItemClickListener(ViewHolder viewHolder, OnItemClickListener<SuggestionItem> onItemClickListener) {
viewHolder.itemView.setOnClickListener(new ItemClickListener<>(
this,
viewHolder.getAdapterPosition(),
onItemClickListener
));
public void setOnItemClickListener(
ViewHolder viewHolder,
OnItemClickListener<SuggestionItem> onItemClickListener
) {
viewHolder.itemView.setOnClickListener(
new ItemClickListener<>(
this,
viewHolder.getAdapterPosition(),
onItemClickListener
)
);
}




/**
* Sets a listener to invoke when the remove button is clicked.
*
* @param viewHolder The view holder
* @param onItemRemoveButtonClickListener The listener to set
*/
public void setOnItemRemoveButtonClickListener(ViewHolder viewHolder, OnItemClickListener<SuggestionItem> onItemRemoveButtonClickListener) {
viewHolder.mRemoveBtnIv.setOnClickListener(new ItemClickListener<>(
this,
viewHolder.getAdapterPosition(),
onItemRemoveButtonClickListener
));
public void setOnItemRemoveButtonClickListener(
ViewHolder viewHolder,
OnItemClickListener<SuggestionItem> onItemRemoveButtonClickListener
) {
viewHolder.removeBtnIv.setOnClickListener(
new ItemClickListener<>(
this,
viewHolder.getAdapterPosition(),
onItemRemoveButtonClickListener
)
);
}




@Override
public int getLayout() {
return MAIN_LAYOUT_ID;
}




@Override
public Long getTrackKey() {
final Suggestion suggestion = getItemModel();
return (suggestion.hasValidId() ? suggestion.getId() : ((long) suggestion.getText().hashCode()));
}




/**
* A view holder containing suggestion item related views.
*/
public static class ViewHolder extends BaseItem.ViewHolder<Suggestion> {

private TextView mTextTv;

private ImageView mIconIv;
private ImageView mRemoveBtnIv;
private TextView textTv;

private ImageView iconIv;
private ImageView removeBtnIv;

public ViewHolder(View itemView) {
super(itemView);

mTextTv = itemView.findViewById(R.id.textTv);
mIconIv = itemView.findViewById(R.id.iconIv);
mRemoveBtnIv = itemView.findViewById(R.id.removeBtnIv);
textTv = itemView.findViewById(R.id.textTv);
iconIv = itemView.findViewById(R.id.iconIv);
removeBtnIv = itemView.findViewById(R.id.removeBtnIv);
}


}




}
Loading

0 comments on commit 2e962d4

Please sign in to comment.