Use ListAdapter in PreferenceSearchAdapter.

This commit is contained in:
Isira Seneviratne 2022-08-01 06:11:35 +05:30
parent 67669c286b
commit 5e0788b99c
3 changed files with 34 additions and 41 deletions

View file

@ -1,54 +1,48 @@
package org.schabi.newpipe.settings.preferencesearch; package org.schabi.newpipe.settings.preferencesearch;
import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
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.databinding.SettingsPreferencesearchListItemResultBinding; import org.schabi.newpipe.databinding.SettingsPreferencesearchListItemResultBinding;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
class PreferenceSearchAdapter class PreferenceSearchAdapter
extends RecyclerView.Adapter<PreferenceSearchAdapter.PreferenceViewHolder> { extends ListAdapter<PreferenceSearchItem, PreferenceSearchAdapter.PreferenceViewHolder> {
private List<PreferenceSearchItem> dataset = new ArrayList<>();
private Consumer<PreferenceSearchItem> onItemClickListener; private Consumer<PreferenceSearchItem> onItemClickListener;
PreferenceSearchAdapter() {
super(new PreferenceCallback());
}
@NonNull @NonNull
@Override @Override
public PreferenceViewHolder onCreateViewHolder( public PreferenceViewHolder onCreateViewHolder(@NonNull final ViewGroup parent,
@NonNull final ViewGroup parent, final int viewType) {
final int viewType return new PreferenceViewHolder(SettingsPreferencesearchListItemResultBinding.inflate(
) { LayoutInflater.from(parent.getContext()), parent, false));
return new PreferenceViewHolder(
SettingsPreferencesearchListItemResultBinding.inflate(
LayoutInflater.from(parent.getContext()),
parent,
false));
} }
@Override @Override
public void onBindViewHolder( public void onBindViewHolder(@NonNull final PreferenceViewHolder holder, final int position) {
@NonNull final PreferenceViewHolder holder, final PreferenceSearchItem item = getItem(position);
final int position
) {
final PreferenceSearchItem item = dataset.get(position);
holder.binding.title.setText(item.getTitle()); holder.binding.title.setText(item.getTitle());
if (TextUtils.isEmpty(item.getSummary())) { if (item.getSummary().isEmpty()) {
holder.binding.summary.setVisibility(View.GONE); holder.binding.summary.setVisibility(View.GONE);
} else { } else {
holder.binding.summary.setVisibility(View.VISIBLE); holder.binding.summary.setVisibility(View.VISIBLE);
holder.binding.summary.setText(item.getSummary()); holder.binding.summary.setText(item.getSummary());
} }
if (TextUtils.isEmpty(item.getBreadcrumbs())) { if (item.getBreadcrumbs().isEmpty()) {
holder.binding.breadcrumbs.setVisibility(View.GONE); holder.binding.breadcrumbs.setVisibility(View.GONE);
} else { } else {
holder.binding.breadcrumbs.setVisibility(View.VISIBLE); holder.binding.breadcrumbs.setVisibility(View.VISIBLE);
@ -62,16 +56,6 @@ class PreferenceSearchAdapter
}); });
} }
void setContent(final List<PreferenceSearchItem> items) {
dataset = new ArrayList<>(items);
this.notifyDataSetChanged();
}
@Override
public int getItemCount() {
return dataset.size();
}
void setOnItemClickListener(final Consumer<PreferenceSearchItem> onItemClickListener) { void setOnItemClickListener(final Consumer<PreferenceSearchItem> onItemClickListener) {
this.onItemClickListener = onItemClickListener; this.onItemClickListener = onItemClickListener;
} }
@ -84,4 +68,19 @@ class PreferenceSearchAdapter
this.binding = binding; this.binding = binding;
} }
} }
private static class PreferenceCallback extends DiffUtil.ItemCallback<PreferenceSearchItem> {
@Override
public boolean areItemsTheSame(@NonNull final PreferenceSearchItem oldItem,
@NonNull final PreferenceSearchItem newItem) {
return oldItem.getKey().equals(newItem.getKey());
}
@Override
public boolean areContentsTheSame(@NonNull final PreferenceSearchItem oldItem,
@NonNull final PreferenceSearchItem newItem) {
return oldItem.getAllRelevantSearchFields().equals(newItem
.getAllRelevantSearchFields());
}
}
} }

View file

@ -1,7 +1,6 @@
package org.schabi.newpipe.settings.preferencesearch; package org.schabi.newpipe.settings.preferencesearch;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -13,7 +12,6 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import org.schabi.newpipe.databinding.SettingsPreferencesearchFragmentBinding; import org.schabi.newpipe.databinding.SettingsPreferencesearchFragmentBinding;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -54,13 +52,8 @@ public class PreferenceSearchFragment extends Fragment {
return; return;
} }
final List<PreferenceSearchItem> results = final List<PreferenceSearchItem> results = searcher.searchFor(keyword);
!TextUtils.isEmpty(keyword) adapter.submitList(results);
? searcher.searchFor(keyword)
: new ArrayList<>();
adapter.setContent(new ArrayList<>(results));
setEmptyViewShown(results.isEmpty()); setEmptyViewShown(results.isEmpty());
} }

View file

@ -3,6 +3,7 @@ package org.schabi.newpipe.settings.preferencesearch;
import android.text.TextUtils; import android.text.TextUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -21,7 +22,7 @@ public class PreferenceSearcher {
List<PreferenceSearchItem> searchFor(final String keyword) { List<PreferenceSearchItem> searchFor(final String keyword) {
if (TextUtils.isEmpty(keyword)) { if (TextUtils.isEmpty(keyword)) {
return new ArrayList<>(); return Collections.emptyList();
} }
return configuration.getSearcher() return configuration.getSearcher()