1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-02 15:01:01 +01:00

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;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import org.schabi.newpipe.databinding.SettingsPreferencesearchListItemResultBinding;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
class PreferenceSearchAdapter
extends RecyclerView.Adapter<PreferenceSearchAdapter.PreferenceViewHolder> {
private List<PreferenceSearchItem> dataset = new ArrayList<>();
extends ListAdapter<PreferenceSearchItem, PreferenceSearchAdapter.PreferenceViewHolder> {
private Consumer<PreferenceSearchItem> onItemClickListener;
PreferenceSearchAdapter() {
super(new PreferenceCallback());
}
@NonNull
@Override
public PreferenceViewHolder onCreateViewHolder(
@NonNull final ViewGroup parent,
final int viewType
) {
return new PreferenceViewHolder(
SettingsPreferencesearchListItemResultBinding.inflate(
LayoutInflater.from(parent.getContext()),
parent,
false));
public PreferenceViewHolder onCreateViewHolder(@NonNull final ViewGroup parent,
final int viewType) {
return new PreferenceViewHolder(SettingsPreferencesearchListItemResultBinding.inflate(
LayoutInflater.from(parent.getContext()), parent, false));
}
@Override
public void onBindViewHolder(
@NonNull final PreferenceViewHolder holder,
final int position
) {
final PreferenceSearchItem item = dataset.get(position);
public void onBindViewHolder(@NonNull final PreferenceViewHolder holder, final int position) {
final PreferenceSearchItem item = getItem(position);
holder.binding.title.setText(item.getTitle());
if (TextUtils.isEmpty(item.getSummary())) {
if (item.getSummary().isEmpty()) {
holder.binding.summary.setVisibility(View.GONE);
} else {
holder.binding.summary.setVisibility(View.VISIBLE);
holder.binding.summary.setText(item.getSummary());
}
if (TextUtils.isEmpty(item.getBreadcrumbs())) {
if (item.getBreadcrumbs().isEmpty()) {
holder.binding.breadcrumbs.setVisibility(View.GONE);
} else {
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) {
this.onItemClickListener = onItemClickListener;
}
@ -84,4 +68,19 @@ class PreferenceSearchAdapter
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;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -13,7 +12,6 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import org.schabi.newpipe.databinding.SettingsPreferencesearchFragmentBinding;
import java.util.ArrayList;
import java.util.List;
/**
@ -54,13 +52,8 @@ public class PreferenceSearchFragment extends Fragment {
return;
}
final List<PreferenceSearchItem> results =
!TextUtils.isEmpty(keyword)
? searcher.searchFor(keyword)
: new ArrayList<>();
adapter.setContent(new ArrayList<>(results));
final List<PreferenceSearchItem> results = searcher.searchFor(keyword);
adapter.submitList(results);
setEmptyViewShown(results.isEmpty());
}

View File

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