2017-09-03 08:04:18 +02:00
|
|
|
package org.schabi.newpipe.fragments.list.search;
|
2016-08-02 15:06:02 +02:00
|
|
|
|
2017-09-28 15:06:48 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.ViewGroup;
|
2016-08-02 15:06:02 +02:00
|
|
|
|
2022-03-18 18:15:44 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2022-07-31 12:23:04 +02:00
|
|
|
import androidx.recyclerview.widget.DiffUtil;
|
|
|
|
import androidx.recyclerview.widget.ListAdapter;
|
2020-03-31 19:20:15 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2017-09-28 15:06:48 +02:00
|
|
|
import org.schabi.newpipe.R;
|
2022-07-31 12:23:04 +02:00
|
|
|
import org.schabi.newpipe.databinding.ItemSearchSuggestionBinding;
|
2016-08-02 15:06:02 +02:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public class SuggestionListAdapter
|
2022-07-31 12:23:04 +02:00
|
|
|
extends ListAdapter<SuggestionItem, SuggestionListAdapter.SuggestionItemHolder> {
|
2017-09-28 15:06:48 +02:00
|
|
|
private OnSuggestionItemSelected listener;
|
2016-08-02 15:06:02 +02:00
|
|
|
|
2022-07-31 12:23:04 +02:00
|
|
|
public SuggestionListAdapter() {
|
|
|
|
super(new SuggestionItemCallback());
|
2017-09-28 15:06:48 +02:00
|
|
|
}
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public void setListener(final OnSuggestionItemSelected listener) {
|
2017-09-28 15:06:48 +02:00
|
|
|
this.listener = listener;
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2022-03-18 18:15:44 +01:00
|
|
|
@NonNull
|
2016-08-02 15:06:02 +02:00
|
|
|
@Override
|
2022-03-18 18:15:44 +01:00
|
|
|
public SuggestionItemHolder onCreateViewHolder(@NonNull final ViewGroup parent,
|
|
|
|
final int viewType) {
|
2022-07-31 12:23:04 +02:00
|
|
|
return new SuggestionItemHolder(ItemSearchSuggestionBinding
|
|
|
|
.inflate(LayoutInflater.from(parent.getContext()), parent, false));
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2017-09-28 15:06:48 +02:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void onBindViewHolder(final SuggestionItemHolder holder, final int position) {
|
2017-09-28 15:06:48 +02:00
|
|
|
final SuggestionItem currentItem = getItem(position);
|
|
|
|
holder.updateFrom(currentItem);
|
2022-07-31 12:23:04 +02:00
|
|
|
holder.itemBinding.suggestionSearch.setOnClickListener(v -> {
|
2020-03-31 19:20:15 +02:00
|
|
|
if (listener != null) {
|
|
|
|
listener.onSuggestionItemSelected(currentItem);
|
|
|
|
}
|
2017-09-28 15:06:48 +02:00
|
|
|
});
|
2022-07-31 12:23:04 +02:00
|
|
|
holder.itemBinding.suggestionSearch.setOnLongClickListener(v -> {
|
2020-03-31 19:20:15 +02:00
|
|
|
if (listener != null) {
|
|
|
|
listener.onSuggestionItemLongClick(currentItem);
|
|
|
|
}
|
|
|
|
return true;
|
2017-09-28 15:06:48 +02:00
|
|
|
});
|
2022-07-31 12:23:04 +02:00
|
|
|
holder.itemBinding.suggestionInsert.setOnClickListener(v -> {
|
2020-03-31 19:20:15 +02:00
|
|
|
if (listener != null) {
|
|
|
|
listener.onSuggestionItemInserted(currentItem);
|
|
|
|
}
|
2017-11-14 19:51:44 +01:00
|
|
|
});
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public interface OnSuggestionItemSelected {
|
|
|
|
void onSuggestionItemSelected(SuggestionItem item);
|
|
|
|
|
|
|
|
void onSuggestionItemInserted(SuggestionItem item);
|
|
|
|
|
|
|
|
void onSuggestionItemLongClick(SuggestionItem item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final class SuggestionItemHolder extends RecyclerView.ViewHolder {
|
2022-07-31 12:23:04 +02:00
|
|
|
private final ItemSearchSuggestionBinding itemBinding;
|
2017-09-28 15:06:48 +02:00
|
|
|
|
2022-07-31 12:23:04 +02:00
|
|
|
private SuggestionItemHolder(final ItemSearchSuggestionBinding binding) {
|
|
|
|
super(binding.getRoot());
|
|
|
|
this.itemBinding = binding;
|
|
|
|
}
|
2017-09-28 15:06:48 +02:00
|
|
|
|
2022-07-31 12:23:04 +02:00
|
|
|
private void updateFrom(final SuggestionItem item) {
|
|
|
|
itemBinding.itemSuggestionIcon.setImageResource(item.fromHistory ? R.drawable.ic_history
|
|
|
|
: R.drawable.ic_search);
|
|
|
|
itemBinding.itemSuggestionQuery.setText(item.query);
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 19:51:44 +01:00
|
|
|
|
2022-07-31 12:23:04 +02:00
|
|
|
private static class SuggestionItemCallback extends DiffUtil.ItemCallback<SuggestionItem> {
|
|
|
|
@Override
|
|
|
|
public boolean areItemsTheSame(@NonNull final SuggestionItem oldItem,
|
|
|
|
@NonNull final SuggestionItem newItem) {
|
2022-09-14 14:01:59 +02:00
|
|
|
return oldItem.fromHistory == newItem.fromHistory
|
|
|
|
&& oldItem.query.equals(newItem.query);
|
2017-09-28 15:06:48 +02:00
|
|
|
}
|
|
|
|
|
2022-07-31 12:23:04 +02:00
|
|
|
@Override
|
|
|
|
public boolean areContentsTheSame(@NonNull final SuggestionItem oldItem,
|
|
|
|
@NonNull final SuggestionItem newItem) {
|
2022-09-14 14:01:59 +02:00
|
|
|
return true; // items' contents never change; the list of items themselves does
|
2020-03-31 19:20:15 +02:00
|
|
|
}
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
2017-09-28 15:06:48 +02:00
|
|
|
}
|