Skip to content

Commit 97e8fa1

Browse files
committed
Add possibility to remove items using long press,
from tab list and Peertube instance list, for devices that do not support drag and drop, e.g. Android TV.
1 parent a44e65e commit 97e8fa1

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎app/src/main/java/org/schabi/newpipe/settings/PeertubeInstanceListFragment.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ private void showAddItemDialog(final Context c) {
199199
.show();
200200
}
201201

202+
private void showDeleteInstanceDialog(final PeertubeInstance instance, final int position) {
203+
new AlertDialog.Builder(requireContext())
204+
.setTitle(instance.getName())
205+
.setCancelable(true)
206+
.setNegativeButton(R.string.cancel, null)
207+
.setPositiveButton(R.string.delete, (dialog, which) -> {
208+
final var list = new ArrayList<>(instanceListAdapter.getCurrentList());
209+
list.remove(position);
210+
211+
if (list.isEmpty()) {
212+
list.add(selectedInstance);
213+
}
214+
215+
instanceListAdapter.submitList(list);
216+
})
217+
.show();
218+
}
219+
202220
private void addInstance(final String url) {
203221
final String cleanUrl = cleanUrl(url);
204222
if (cleanUrl == null) {
@@ -357,6 +375,22 @@ class TabViewHolder extends RecyclerView.ViewHolder {
357375
TabViewHolder(final ItemInstanceBinding binding) {
358376
super(binding.getRoot());
359377
this.itemBinding = binding;
378+
379+
itemView.setOnLongClickListener(this::onInstanceLongClick);
380+
}
381+
382+
private boolean onInstanceLongClick(final View view) {
383+
final int position = getBindingAdapterPosition();
384+
if (position != RecyclerView.NO_POSITION) {
385+
final PeertubeInstance instance = getItem(position);
386+
// do not allow removing the selected instance
387+
if (instance.getUrl().equals(selectedInstance.getUrl())) {
388+
return true;
389+
}
390+
391+
showDeleteInstanceDialog(instance, position);
392+
}
393+
return true;
360394
}
361395

362396
@SuppressLint("ClickableViewAccessibility")

‎app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ private void restoreDefaults() {
139139
.show();
140140
}
141141

142+
private void showDeleteTabDialog(final int position, final String tabName) {
143+
new AlertDialog.Builder(requireContext())
144+
.setTitle(tabName)
145+
.setCancelable(true)
146+
.setNegativeButton(R.string.cancel, null)
147+
.setPositiveButton(R.string.delete, (dialog, which) -> {
148+
tabList.remove(position);
149+
selectedTabsAdapter.notifyItemRemoved(position);
150+
151+
if (tabList.isEmpty()) {
152+
tabList.add(Tab.Type.BLANK.getTab());
153+
selectedTabsAdapter.notifyItemInserted(0);
154+
}
155+
})
156+
.show();
157+
}
158+
142159
private void initButton(final View rootView) {
143160
final FloatingActionButton fab = rootView.findViewById(R.id.addTabsButton);
144161
fab.setOnClickListener(v -> {
@@ -375,6 +392,16 @@ class TabViewHolder extends RecyclerView.ViewHolder {
375392
tabNameView = itemView.findViewById(R.id.tabName);
376393
tabIconView = itemView.findViewById(R.id.tabIcon);
377394
handle = itemView.findViewById(R.id.handle);
395+
396+
itemView.setOnLongClickListener(this::onTabLongClick);
397+
}
398+
399+
private boolean onTabLongClick(final View view) {
400+
final int position = getBindingAdapterPosition();
401+
if (position != RecyclerView.NO_POSITION) {
402+
showDeleteTabDialog(position, tabNameView.getText().toString());
403+
}
404+
return true;
378405
}
379406

380407
@SuppressLint("ClickableViewAccessibility")

0 commit comments

Comments
 (0)