Merge pull request #8719 from Isira-Seneviratne/Use_ListAdapter
Use ListAdapter for search predictions.
This commit is contained in:
commit
2089f3e54c
|
@ -200,7 +200,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||
showLocalSuggestions = NewPipeSettings.showLocalSearchSuggestions(activity, prefs);
|
||||
showRemoteSuggestions = NewPipeSettings.showRemoteSearchSuggestions(activity, prefs);
|
||||
|
||||
suggestionListAdapter = new SuggestionListAdapter(activity);
|
||||
suggestionListAdapter = new SuggestionListAdapter();
|
||||
historyRecordManager = new HistoryRecordManager(context);
|
||||
}
|
||||
|
||||
|
@ -530,7 +530,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||
searchBinding.correctSuggestion.setVisibility(View.GONE);
|
||||
|
||||
searchEditText.setText("");
|
||||
suggestionListAdapter.setItems(new ArrayList<>());
|
||||
suggestionListAdapter.submitList(null);
|
||||
showKeyboardSearch();
|
||||
});
|
||||
|
||||
|
@ -945,7 +945,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||
Log.d(TAG, "handleSuggestions() called with: suggestions = [" + suggestions + "]");
|
||||
}
|
||||
searchBinding.suggestionsList.smoothScrollToPosition(0);
|
||||
searchBinding.suggestionsList.post(() -> suggestionListAdapter.setItems(suggestions));
|
||||
suggestionListAdapter.submitList(suggestions);
|
||||
|
||||
if (suggestionsPanelVisible && isErrorPanelVisible()) {
|
||||
hideLoading();
|
||||
|
@ -1066,14 +1066,14 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
|
|||
return 0;
|
||||
}
|
||||
|
||||
final SuggestionItem item = suggestionListAdapter.getItem(position);
|
||||
final SuggestionItem item = suggestionListAdapter.getCurrentList().get(position);
|
||||
return item.fromHistory ? makeMovementFlags(0,
|
||||
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) : 0;
|
||||
}
|
||||
|
||||
public void onSuggestionItemSwiped(@NonNull final RecyclerView.ViewHolder viewHolder) {
|
||||
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)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
|
|
|
@ -1,34 +1,22 @@
|
|||
package org.schabi.newpipe.fragments.list.search;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ListAdapter;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.schabi.newpipe.databinding.ItemSearchSuggestionBinding;
|
||||
|
||||
public class SuggestionListAdapter
|
||||
extends RecyclerView.Adapter<SuggestionListAdapter.SuggestionItemHolder> {
|
||||
private final ArrayList<SuggestionItem> items = new ArrayList<>();
|
||||
private final Context context;
|
||||
extends ListAdapter<SuggestionItem, SuggestionListAdapter.SuggestionItemHolder> {
|
||||
private OnSuggestionItemSelected listener;
|
||||
|
||||
public SuggestionListAdapter(final Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void setItems(final List<SuggestionItem> items) {
|
||||
this.items.clear();
|
||||
this.items.addAll(items);
|
||||
notifyDataSetChanged();
|
||||
public SuggestionListAdapter() {
|
||||
super(new SuggestionItemCallback());
|
||||
}
|
||||
|
||||
public void setListener(final OnSuggestionItemSelected listener) {
|
||||
|
@ -39,45 +27,32 @@ public class SuggestionListAdapter
|
|||
@Override
|
||||
public SuggestionItemHolder onCreateViewHolder(@NonNull final ViewGroup parent,
|
||||
final int viewType) {
|
||||
return new SuggestionItemHolder(LayoutInflater.from(context)
|
||||
.inflate(R.layout.item_search_suggestion, parent, false));
|
||||
return new SuggestionItemHolder(ItemSearchSuggestionBinding
|
||||
.inflate(LayoutInflater.from(parent.getContext()), parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(final SuggestionItemHolder holder, final int position) {
|
||||
final SuggestionItem currentItem = getItem(position);
|
||||
holder.updateFrom(currentItem);
|
||||
holder.queryView.setOnClickListener(v -> {
|
||||
holder.itemBinding.suggestionSearch.setOnClickListener(v -> {
|
||||
if (listener != null) {
|
||||
listener.onSuggestionItemSelected(currentItem);
|
||||
}
|
||||
});
|
||||
holder.queryView.setOnLongClickListener(v -> {
|
||||
holder.itemBinding.suggestionSearch.setOnLongClickListener(v -> {
|
||||
if (listener != null) {
|
||||
listener.onSuggestionItemLongClick(currentItem);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
holder.insertView.setOnClickListener(v -> {
|
||||
holder.itemBinding.suggestionInsert.setOnClickListener(v -> {
|
||||
if (listener != null) {
|
||||
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 {
|
||||
void onSuggestionItemSelected(SuggestionItem item);
|
||||
|
||||
|
@ -87,30 +62,31 @@ public class SuggestionListAdapter
|
|||
}
|
||||
|
||||
public static final class SuggestionItemHolder extends RecyclerView.ViewHolder {
|
||||
private final TextView itemSuggestionQuery;
|
||||
private final ImageView suggestionIcon;
|
||||
private final View queryView;
|
||||
private final View insertView;
|
||||
private final ItemSearchSuggestionBinding itemBinding;
|
||||
|
||||
// Cache some ids, as they can potentially be constantly updated/recycled
|
||||
private final int historyResId;
|
||||
private final int searchResId;
|
||||
|
||||
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 SuggestionItemHolder(final ItemSearchSuggestionBinding binding) {
|
||||
super(binding.getRoot());
|
||||
this.itemBinding = binding;
|
||||
}
|
||||
|
||||
private void updateFrom(final SuggestionItem item) {
|
||||
suggestionIcon.setImageResource(item.fromHistory ? historyResId : searchResId);
|
||||
itemSuggestionQuery.setText(item.query);
|
||||
itemBinding.itemSuggestionIcon.setImageResource(item.fromHistory ? R.drawable.ic_history
|
||||
: 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,28 +12,27 @@ import android.view.MenuItem;
|
|||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.widget.AppCompatImageView;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ItemTouchHelper;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.ListAdapter;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.grack.nanojson.JsonStringWriter;
|
||||
import com.grack.nanojson.JsonWriter;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
import org.schabi.newpipe.databinding.DialogEditTextBinding;
|
||||
import org.schabi.newpipe.databinding.FragmentInstanceListBinding;
|
||||
import org.schabi.newpipe.databinding.ItemInstanceBinding;
|
||||
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
|
||||
import org.schabi.newpipe.util.Constants;
|
||||
import org.schabi.newpipe.util.PeertubeHelper;
|
||||
|
@ -41,7 +40,6 @@ import org.schabi.newpipe.util.ThemeHelper;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.rxjava3.core.Single;
|
||||
|
@ -50,12 +48,11 @@ import io.reactivex.rxjava3.disposables.Disposable;
|
|||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
public class PeertubeInstanceListFragment extends Fragment {
|
||||
private final List<PeertubeInstance> instanceList = new ArrayList<>();
|
||||
private PeertubeInstance selectedInstance;
|
||||
private String savedInstanceListKey;
|
||||
private InstanceListAdapter instanceListAdapter;
|
||||
|
||||
private ProgressBar progressBar;
|
||||
private FragmentInstanceListBinding binding;
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
||||
private CompositeDisposable disposables = new CompositeDisposable();
|
||||
|
@ -71,7 +68,6 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext());
|
||||
savedInstanceListKey = getString(R.string.peertube_instance_list_key);
|
||||
selectedInstance = PeertubeHelper.getCurrentInstance();
|
||||
updateInstanceList();
|
||||
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
@ -79,7 +75,8 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
@Override
|
||||
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
|
||||
final Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_instance_list, container, false);
|
||||
binding = FragmentInstanceListBinding.inflate(inflater, container, false);
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -87,26 +84,17 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
@Nullable final Bundle savedInstanceState) {
|
||||
super.onViewCreated(rootView, savedInstanceState);
|
||||
|
||||
initViews(rootView);
|
||||
}
|
||||
|
||||
private void initViews(@NonNull final View rootView) {
|
||||
final TextView instanceHelpTV = rootView.findViewById(R.id.instanceHelpTV);
|
||||
instanceHelpTV.setText(getString(R.string.peertube_instance_url_help,
|
||||
binding.instanceHelpTV.setText(getString(R.string.peertube_instance_url_help,
|
||||
getString(R.string.peertube_instance_list_url)));
|
||||
|
||||
initButton(rootView);
|
||||
|
||||
final RecyclerView listInstances = rootView.findViewById(R.id.instances);
|
||||
listInstances.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||
binding.addInstanceButton.setOnClickListener(v -> showAddItemDialog(requireContext()));
|
||||
binding.instances.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||
|
||||
final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(getItemTouchCallback());
|
||||
itemTouchHelper.attachToRecyclerView(listInstances);
|
||||
itemTouchHelper.attachToRecyclerView(binding.instances);
|
||||
|
||||
instanceListAdapter = new InstanceListAdapter(requireContext(), itemTouchHelper);
|
||||
listInstances.setAdapter(instanceListAdapter);
|
||||
|
||||
progressBar = rootView.findViewById(R.id.loading_progress_bar);
|
||||
binding.instances.setAdapter(instanceListAdapter);
|
||||
instanceListAdapter.submitList(PeertubeHelper.getInstanceList(requireContext()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -131,6 +119,12 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
disposables = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
binding = null;
|
||||
super.onDestroyView();
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////////////////
|
||||
// Menu
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
@ -156,11 +150,6 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
// Utils
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
private void updateInstanceList() {
|
||||
instanceList.clear();
|
||||
instanceList.addAll(PeertubeHelper.getInstanceList(requireContext()));
|
||||
}
|
||||
|
||||
private void selectInstance(final PeertubeInstance instance) {
|
||||
selectedInstance = PeertubeHelper.selectInstance(instance, requireContext());
|
||||
sharedPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, true).apply();
|
||||
|
@ -168,7 +157,7 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
|
||||
private void saveChanges() {
|
||||
final JsonStringWriter jsonWriter = JsonWriter.string().object().array("instances");
|
||||
for (final PeertubeInstance instance : instanceList) {
|
||||
for (final PeertubeInstance instance : instanceListAdapter.getCurrentList()) {
|
||||
jsonWriter.object();
|
||||
jsonWriter.value("name", instance.getName());
|
||||
jsonWriter.value("url", instance.getUrl());
|
||||
|
@ -179,28 +168,21 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
}
|
||||
|
||||
private void restoreDefaults() {
|
||||
new AlertDialog.Builder(requireContext())
|
||||
final Context context = requireContext();
|
||||
new AlertDialog.Builder(context)
|
||||
.setTitle(R.string.restore_defaults)
|
||||
.setMessage(R.string.restore_defaults_confirmation)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setPositiveButton(R.string.ok, (dialog, which) -> {
|
||||
sharedPreferences.edit().remove(savedInstanceListKey).apply();
|
||||
selectInstance(PeertubeInstance.DEFAULT_INSTANCE);
|
||||
updateInstanceList();
|
||||
instanceListAdapter.notifyDataSetChanged();
|
||||
instanceListAdapter.submitList(PeertubeHelper.getInstanceList(context));
|
||||
})
|
||||
.show();
|
||||
}
|
||||
|
||||
private void initButton(final View rootView) {
|
||||
final FloatingActionButton fab = rootView.findViewById(R.id.addInstanceButton);
|
||||
fab.setOnClickListener(v ->
|
||||
showAddItemDialog(requireContext()));
|
||||
}
|
||||
|
||||
private void showAddItemDialog(final Context c) {
|
||||
final DialogEditTextBinding dialogBinding =
|
||||
DialogEditTextBinding.inflate(getLayoutInflater());
|
||||
final var dialogBinding = DialogEditTextBinding.inflate(getLayoutInflater());
|
||||
dialogBinding.dialogEditText.setInputType(
|
||||
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
|
||||
dialogBinding.dialogEditText.setHint(R.string.peertube_instance_add_help);
|
||||
|
@ -222,17 +204,17 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
if (cleanUrl == null) {
|
||||
return;
|
||||
}
|
||||
progressBar.setVisibility(View.VISIBLE);
|
||||
binding.loadingProgressBar.setVisibility(View.VISIBLE);
|
||||
final Disposable disposable = Single.fromCallable(() -> {
|
||||
final PeertubeInstance instance = new PeertubeInstance(cleanUrl);
|
||||
instance.fetchInstanceMetaData();
|
||||
return instance;
|
||||
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe((instance) -> {
|
||||
progressBar.setVisibility(View.GONE);
|
||||
binding.loadingProgressBar.setVisibility(View.GONE);
|
||||
add(instance);
|
||||
}, e -> {
|
||||
progressBar.setVisibility(View.GONE);
|
||||
binding.loadingProgressBar.setVisibility(View.GONE);
|
||||
Toast.makeText(getActivity(), R.string.peertube_instance_add_fail,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
});
|
||||
|
@ -255,7 +237,7 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
return null;
|
||||
}
|
||||
// only allow if not already exists
|
||||
for (final PeertubeInstance instance : instanceList) {
|
||||
for (final PeertubeInstance instance : instanceListAdapter.getCurrentList()) {
|
||||
if (instance.getUrl().equals(cleanUrl)) {
|
||||
Toast.makeText(getActivity(), R.string.peertube_instance_add_exists,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
|
@ -266,8 +248,9 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
}
|
||||
|
||||
private void add(final PeertubeInstance instance) {
|
||||
instanceList.add(instance);
|
||||
instanceListAdapter.notifyDataSetChanged();
|
||||
final var list = new ArrayList<>(instanceListAdapter.getCurrentList());
|
||||
list.add(instance);
|
||||
instanceListAdapter.submitList(list);
|
||||
}
|
||||
|
||||
private ItemTouchHelper.SimpleCallback getItemTouchCallback() {
|
||||
|
@ -281,8 +264,7 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
final long msSinceStartScroll) {
|
||||
final int standardSpeed = super.interpolateOutOfBoundsScroll(recyclerView, viewSize,
|
||||
viewSizeOutOfBounds, totalSize, msSinceStartScroll);
|
||||
final int minimumAbsVelocity = Math.max(12,
|
||||
Math.abs(standardSpeed));
|
||||
final int minimumAbsVelocity = Math.max(12, Math.abs(standardSpeed));
|
||||
return minimumAbsVelocity * (int) Math.signum(viewSizeOutOfBounds);
|
||||
}
|
||||
|
||||
|
@ -316,17 +298,19 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
final int swipeDir) {
|
||||
final int position = viewHolder.getBindingAdapterPosition();
|
||||
// do not allow swiping the selected instance
|
||||
if (instanceList.get(position).getUrl().equals(selectedInstance.getUrl())) {
|
||||
if (instanceListAdapter.getCurrentList().get(position).getUrl()
|
||||
.equals(selectedInstance.getUrl())) {
|
||||
instanceListAdapter.notifyItemChanged(position);
|
||||
return;
|
||||
}
|
||||
instanceList.remove(position);
|
||||
instanceListAdapter.notifyItemRemoved(position);
|
||||
final var list = new ArrayList<>(instanceListAdapter.getCurrentList());
|
||||
list.remove(position);
|
||||
|
||||
if (instanceList.isEmpty()) {
|
||||
instanceList.add(selectedInstance);
|
||||
instanceListAdapter.notifyItemInserted(0);
|
||||
if (list.isEmpty()) {
|
||||
list.add(selectedInstance);
|
||||
}
|
||||
|
||||
instanceListAdapter.submitList(list);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -336,96 +320,94 @@ public class PeertubeInstanceListFragment extends Fragment {
|
|||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
private class InstanceListAdapter
|
||||
extends RecyclerView.Adapter<InstanceListAdapter.TabViewHolder> {
|
||||
extends ListAdapter<PeertubeInstance, InstanceListAdapter.TabViewHolder> {
|
||||
private final LayoutInflater inflater;
|
||||
private final ItemTouchHelper itemTouchHelper;
|
||||
private RadioButton lastChecked;
|
||||
|
||||
InstanceListAdapter(final Context context, final ItemTouchHelper itemTouchHelper) {
|
||||
super(new PeertubeInstanceCallback());
|
||||
this.itemTouchHelper = itemTouchHelper;
|
||||
this.inflater = LayoutInflater.from(context);
|
||||
}
|
||||
|
||||
public void swapItems(final int fromPosition, final int toPosition) {
|
||||
Collections.swap(instanceList, fromPosition, toPosition);
|
||||
notifyItemMoved(fromPosition, toPosition);
|
||||
final var list = new ArrayList<>(getCurrentList());
|
||||
Collections.swap(list, fromPosition, toPosition);
|
||||
submitList(list);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public InstanceListAdapter.TabViewHolder onCreateViewHolder(@NonNull final ViewGroup parent,
|
||||
final int viewType) {
|
||||
final View view = inflater.inflate(R.layout.item_instance, parent, false);
|
||||
return new InstanceListAdapter.TabViewHolder(view);
|
||||
return new InstanceListAdapter.TabViewHolder(ItemInstanceBinding.inflate(inflater,
|
||||
parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull final InstanceListAdapter.TabViewHolder holder,
|
||||
final int position) {
|
||||
holder.bind(position, holder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return instanceList.size();
|
||||
holder.bind(position);
|
||||
}
|
||||
|
||||
class TabViewHolder extends RecyclerView.ViewHolder {
|
||||
private final AppCompatImageView instanceIconView;
|
||||
private final TextView instanceNameView;
|
||||
private final TextView instanceUrlView;
|
||||
private final RadioButton instanceRB;
|
||||
private final ImageView handle;
|
||||
private final ItemInstanceBinding itemBinding;
|
||||
|
||||
TabViewHolder(final View itemView) {
|
||||
super(itemView);
|
||||
|
||||
instanceIconView = itemView.findViewById(R.id.instanceIcon);
|
||||
instanceNameView = itemView.findViewById(R.id.instanceName);
|
||||
instanceUrlView = itemView.findViewById(R.id.instanceUrl);
|
||||
instanceRB = itemView.findViewById(R.id.selectInstanceRB);
|
||||
handle = itemView.findViewById(R.id.handle);
|
||||
TabViewHolder(final ItemInstanceBinding binding) {
|
||||
super(binding.getRoot());
|
||||
this.itemBinding = binding;
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
void bind(final int position, final TabViewHolder holder) {
|
||||
handle.setOnTouchListener(getOnTouchListener(holder));
|
||||
|
||||
final PeertubeInstance instance = instanceList.get(position);
|
||||
instanceNameView.setText(instance.getName());
|
||||
instanceUrlView.setText(instance.getUrl());
|
||||
instanceRB.setOnCheckedChangeListener(null);
|
||||
if (selectedInstance.getUrl().equals(instance.getUrl())) {
|
||||
if (lastChecked != null && lastChecked != instanceRB) {
|
||||
lastChecked.setChecked(false);
|
||||
}
|
||||
instanceRB.setChecked(true);
|
||||
lastChecked = instanceRB;
|
||||
}
|
||||
instanceRB.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
if (isChecked) {
|
||||
selectInstance(instance);
|
||||
if (lastChecked != null && lastChecked != instanceRB) {
|
||||
lastChecked.setChecked(false);
|
||||
}
|
||||
lastChecked = instanceRB;
|
||||
}
|
||||
});
|
||||
instanceIconView.setImageResource(R.drawable.ic_placeholder_peertube);
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private View.OnTouchListener getOnTouchListener(final RecyclerView.ViewHolder item) {
|
||||
return (view, motionEvent) -> {
|
||||
void bind(final int position) {
|
||||
itemBinding.handle.setOnTouchListener((view, motionEvent) -> {
|
||||
if (motionEvent.getActionMasked() == MotionEvent.ACTION_DOWN) {
|
||||
if (itemTouchHelper != null && getItemCount() > 1) {
|
||||
itemTouchHelper.startDrag(item);
|
||||
itemTouchHelper.startDrag(this);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
});
|
||||
|
||||
final PeertubeInstance instance = getItem(position);
|
||||
itemBinding.instanceName.setText(instance.getName());
|
||||
itemBinding.instanceUrl.setText(instance.getUrl());
|
||||
itemBinding.selectInstanceRB.setOnCheckedChangeListener(null);
|
||||
if (selectedInstance.getUrl().equals(instance.getUrl())) {
|
||||
if (lastChecked != null && lastChecked != itemBinding.selectInstanceRB) {
|
||||
lastChecked.setChecked(false);
|
||||
}
|
||||
itemBinding.selectInstanceRB.setChecked(true);
|
||||
lastChecked = itemBinding.selectInstanceRB;
|
||||
}
|
||||
itemBinding.selectInstanceRB.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
if (isChecked) {
|
||||
selectInstance(instance);
|
||||
if (lastChecked != null && lastChecked != itemBinding.selectInstanceRB) {
|
||||
lastChecked.setChecked(false);
|
||||
}
|
||||
lastChecked = itemBinding.selectInstanceRB;
|
||||
}
|
||||
});
|
||||
itemBinding.instanceIcon.setImageResource(R.drawable.ic_placeholder_peertube);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class PeertubeInstanceCallback extends DiffUtil.ItemCallback<PeertubeInstance> {
|
||||
@Override
|
||||
public boolean areItemsTheSame(@NonNull final PeertubeInstance oldItem,
|
||||
@NonNull final PeertubeInstance newItem) {
|
||||
return oldItem.getUrl().equals(newItem.getUrl());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(@NonNull final PeertubeInstance oldItem,
|
||||
@NonNull final PeertubeInstance newItem) {
|
||||
return oldItem.getName().equals(newItem.getName())
|
||||
&& oldItem.getUrl().equals(newItem.getUrl());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue