mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 16:40:32 +00:00
Use ListAdapter in SuggestionListAdapter.
This commit is contained in:
parent
4f6b5b3b89
commit
67669c286b
@ -200,7 +200,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||||||
showLocalSuggestions = NewPipeSettings.showLocalSearchSuggestions(activity, prefs);
|
showLocalSuggestions = NewPipeSettings.showLocalSearchSuggestions(activity, prefs);
|
||||||
showRemoteSuggestions = NewPipeSettings.showRemoteSearchSuggestions(activity, prefs);
|
showRemoteSuggestions = NewPipeSettings.showRemoteSearchSuggestions(activity, prefs);
|
||||||
|
|
||||||
suggestionListAdapter = new SuggestionListAdapter(activity);
|
suggestionListAdapter = new SuggestionListAdapter();
|
||||||
historyRecordManager = new HistoryRecordManager(context);
|
historyRecordManager = new HistoryRecordManager(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -530,7 +530,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||||||
searchBinding.correctSuggestion.setVisibility(View.GONE);
|
searchBinding.correctSuggestion.setVisibility(View.GONE);
|
||||||
|
|
||||||
searchEditText.setText("");
|
searchEditText.setText("");
|
||||||
suggestionListAdapter.setItems(new ArrayList<>());
|
suggestionListAdapter.submitList(null);
|
||||||
showKeyboardSearch();
|
showKeyboardSearch();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -945,7 +945,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||||||
Log.d(TAG, "handleSuggestions() called with: suggestions = [" + suggestions + "]");
|
Log.d(TAG, "handleSuggestions() called with: suggestions = [" + suggestions + "]");
|
||||||
}
|
}
|
||||||
searchBinding.suggestionsList.smoothScrollToPosition(0);
|
searchBinding.suggestionsList.smoothScrollToPosition(0);
|
||||||
searchBinding.suggestionsList.post(() -> suggestionListAdapter.setItems(suggestions));
|
suggestionListAdapter.submitList(suggestions);
|
||||||
|
|
||||||
if (suggestionsPanelVisible && isErrorPanelVisible()) {
|
if (suggestionsPanelVisible && isErrorPanelVisible()) {
|
||||||
hideLoading();
|
hideLoading();
|
||||||
@ -1066,14 +1066,14 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
final SuggestionItem item = suggestionListAdapter.getItem(position);
|
final SuggestionItem item = suggestionListAdapter.getCurrentList().get(position);
|
||||||
return item.fromHistory ? makeMovementFlags(0,
|
return item.fromHistory ? makeMovementFlags(0,
|
||||||
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) : 0;
|
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onSuggestionItemSwiped(@NonNull final RecyclerView.ViewHolder viewHolder) {
|
public void onSuggestionItemSwiped(@NonNull final RecyclerView.ViewHolder viewHolder) {
|
||||||
final int position = viewHolder.getBindingAdapterPosition();
|
final int position = viewHolder.getBindingAdapterPosition();
|
||||||
final String query = suggestionListAdapter.getItem(position).query;
|
final String query = suggestionListAdapter.getCurrentList().get(position).query;
|
||||||
final Disposable onDelete = historyRecordManager.deleteSearchHistory(query)
|
final Disposable onDelete = historyRecordManager.deleteSearchHistory(query)
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe(
|
.subscribe(
|
||||||
|
@ -1,34 +1,22 @@
|
|||||||
package org.schabi.newpipe.fragments.list.search;
|
package org.schabi.newpipe.fragments.list.search;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ImageView;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.recyclerview.widget.DiffUtil;
|
||||||
|
import androidx.recyclerview.widget.ListAdapter;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
|
import org.schabi.newpipe.databinding.ItemSearchSuggestionBinding;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SuggestionListAdapter
|
public class SuggestionListAdapter
|
||||||
extends RecyclerView.Adapter<SuggestionListAdapter.SuggestionItemHolder> {
|
extends ListAdapter<SuggestionItem, SuggestionListAdapter.SuggestionItemHolder> {
|
||||||
private final ArrayList<SuggestionItem> items = new ArrayList<>();
|
|
||||||
private final Context context;
|
|
||||||
private OnSuggestionItemSelected listener;
|
private OnSuggestionItemSelected listener;
|
||||||
|
|
||||||
public SuggestionListAdapter(final Context context) {
|
public SuggestionListAdapter() {
|
||||||
this.context = context;
|
super(new SuggestionItemCallback());
|
||||||
}
|
|
||||||
|
|
||||||
public void setItems(final List<SuggestionItem> items) {
|
|
||||||
this.items.clear();
|
|
||||||
this.items.addAll(items);
|
|
||||||
notifyDataSetChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setListener(final OnSuggestionItemSelected listener) {
|
public void setListener(final OnSuggestionItemSelected listener) {
|
||||||
@ -39,45 +27,32 @@ public class SuggestionListAdapter
|
|||||||
@Override
|
@Override
|
||||||
public SuggestionItemHolder onCreateViewHolder(@NonNull final ViewGroup parent,
|
public SuggestionItemHolder onCreateViewHolder(@NonNull final ViewGroup parent,
|
||||||
final int viewType) {
|
final int viewType) {
|
||||||
return new SuggestionItemHolder(LayoutInflater.from(context)
|
return new SuggestionItemHolder(ItemSearchSuggestionBinding
|
||||||
.inflate(R.layout.item_search_suggestion, parent, false));
|
.inflate(LayoutInflater.from(parent.getContext()), parent, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(final SuggestionItemHolder holder, final int position) {
|
public void onBindViewHolder(final SuggestionItemHolder holder, final int position) {
|
||||||
final SuggestionItem currentItem = getItem(position);
|
final SuggestionItem currentItem = getItem(position);
|
||||||
holder.updateFrom(currentItem);
|
holder.updateFrom(currentItem);
|
||||||
holder.queryView.setOnClickListener(v -> {
|
holder.itemBinding.suggestionSearch.setOnClickListener(v -> {
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
listener.onSuggestionItemSelected(currentItem);
|
listener.onSuggestionItemSelected(currentItem);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
holder.queryView.setOnLongClickListener(v -> {
|
holder.itemBinding.suggestionSearch.setOnLongClickListener(v -> {
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
listener.onSuggestionItemLongClick(currentItem);
|
listener.onSuggestionItemLongClick(currentItem);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
holder.insertView.setOnClickListener(v -> {
|
holder.itemBinding.suggestionInsert.setOnClickListener(v -> {
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
listener.onSuggestionItemInserted(currentItem);
|
listener.onSuggestionItemInserted(currentItem);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
SuggestionItem getItem(final int position) {
|
|
||||||
return items.get(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getItemCount() {
|
|
||||||
return items.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return getItemCount() == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface OnSuggestionItemSelected {
|
public interface OnSuggestionItemSelected {
|
||||||
void onSuggestionItemSelected(SuggestionItem item);
|
void onSuggestionItemSelected(SuggestionItem item);
|
||||||
|
|
||||||
@ -87,30 +62,31 @@ public class SuggestionListAdapter
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static final class SuggestionItemHolder extends RecyclerView.ViewHolder {
|
public static final class SuggestionItemHolder extends RecyclerView.ViewHolder {
|
||||||
private final TextView itemSuggestionQuery;
|
private final ItemSearchSuggestionBinding itemBinding;
|
||||||
private final ImageView suggestionIcon;
|
|
||||||
private final View queryView;
|
|
||||||
private final View insertView;
|
|
||||||
|
|
||||||
// Cache some ids, as they can potentially be constantly updated/recycled
|
private SuggestionItemHolder(final ItemSearchSuggestionBinding binding) {
|
||||||
private final int historyResId;
|
super(binding.getRoot());
|
||||||
private final int searchResId;
|
this.itemBinding = binding;
|
||||||
|
|
||||||
private SuggestionItemHolder(final View rootView) {
|
|
||||||
super(rootView);
|
|
||||||
suggestionIcon = rootView.findViewById(R.id.item_suggestion_icon);
|
|
||||||
itemSuggestionQuery = rootView.findViewById(R.id.item_suggestion_query);
|
|
||||||
|
|
||||||
queryView = rootView.findViewById(R.id.suggestion_search);
|
|
||||||
insertView = rootView.findViewById(R.id.suggestion_insert);
|
|
||||||
|
|
||||||
historyResId = R.drawable.ic_history;
|
|
||||||
searchResId = R.drawable.ic_search;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateFrom(final SuggestionItem item) {
|
private void updateFrom(final SuggestionItem item) {
|
||||||
suggestionIcon.setImageResource(item.fromHistory ? historyResId : searchResId);
|
itemBinding.itemSuggestionIcon.setImageResource(item.fromHistory ? R.drawable.ic_history
|
||||||
itemSuggestionQuery.setText(item.query);
|
: R.drawable.ic_search);
|
||||||
|
itemBinding.itemSuggestionQuery.setText(item.query);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SuggestionItemCallback extends DiffUtil.ItemCallback<SuggestionItem> {
|
||||||
|
@Override
|
||||||
|
public boolean areItemsTheSame(@NonNull final SuggestionItem oldItem,
|
||||||
|
@NonNull final SuggestionItem newItem) {
|
||||||
|
return oldItem.query.equals(newItem.query);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean areContentsTheSame(@NonNull final SuggestionItem oldItem,
|
||||||
|
@NonNull final SuggestionItem newItem) {
|
||||||
|
return oldItem.equals(newItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user