Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DiffUtil;
Expand Down Expand Up @@ -134,6 +135,17 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
@NonNull final MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_chooser_fragment, menu);
final SwitchCompat editSwitch =
(SwitchCompat) menu.findItem(R.id.menu_item_edit_mode).getActionView();
if (editSwitch != null) {
editSwitch.setChecked(
instanceListAdapter != null && instanceListAdapter.editModeEnabled);
editSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (instanceListAdapter != null) {
instanceListAdapter.setEditMode(isChecked);
}
});
}
}

@Override
Expand Down Expand Up @@ -199,6 +211,24 @@ private void showAddItemDialog(final Context c) {
.show();
}

private void showDeleteInstanceDialog(final PeertubeInstance instance, final int position) {
new AlertDialog.Builder(requireContext())
.setTitle(instance.getName())
.setCancelable(true)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.delete, (dialog, which) -> {
final var list = new ArrayList<>(instanceListAdapter.getCurrentList());
list.remove(position);

if (list.isEmpty()) {
list.add(selectedInstance);
}

instanceListAdapter.submitList(list);
})
.show();
}

private void addInstance(final String url) {
final String cleanUrl = cleanUrl(url);
if (cleanUrl == null) {
Expand Down Expand Up @@ -324,6 +354,12 @@ private class InstanceListAdapter
private final LayoutInflater inflater;
private final ItemTouchHelper itemTouchHelper;
private RadioButton lastChecked;
boolean editModeEnabled = false;

void setEditMode(final boolean enabled) {
editModeEnabled = enabled;
notifyDataSetChanged();
}

InstanceListAdapter(final Context context, final ItemTouchHelper itemTouchHelper) {
super(new PeertubeInstanceCallback());
Expand Down Expand Up @@ -357,6 +393,9 @@ class TabViewHolder extends RecyclerView.ViewHolder {
TabViewHolder(final ItemInstanceBinding binding) {
super(binding.getRoot());
this.itemBinding = binding;

itemView.setOnLongClickListener(null);
itemView.setOnClickListener(null);
}

@SuppressLint("ClickableViewAccessibility")
Expand Down Expand Up @@ -392,6 +431,34 @@ void bind(final int position) {
}
});
itemBinding.instanceIcon.setImageResource(R.drawable.ic_placeholder_peertube);

itemBinding.handle.setVisibility(editModeEnabled ? View.GONE : View.VISIBLE);
itemBinding.itemActions.setVisibility(editModeEnabled ? View.VISIBLE : View.GONE);
if (editModeEnabled) {
final boolean isActiveInstance =
instance.getUrl().equals(selectedInstance.getUrl());
itemBinding.btnMoveUp.setEnabled(position > 0);
itemBinding.btnMoveDown.setEnabled(position < getItemCount() - 1);
itemBinding.btnDelete.setEnabled(!isActiveInstance);
itemBinding.btnMoveUp.setOnClickListener(v -> {
final int pos = getBindingAdapterPosition();
if (pos > 0) {
swapItems(pos, pos - 1);
}
});
itemBinding.btnMoveDown.setOnClickListener(v -> {
final int pos = getBindingAdapterPosition();
if (pos < getItemCount() - 1) {
swapItems(pos, pos + 1);
}
});
itemBinding.btnDelete.setOnClickListener(v -> {
final int pos = getBindingAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
showDeleteInstanceDialog(getItem(pos), pos);
}
});
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand Down Expand Up @@ -111,6 +113,17 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
restoreDefaults();
return true;
});
final SwitchCompat editSwitch =
(SwitchCompat) menu.findItem(R.id.menu_item_edit_mode).getActionView();
if (editSwitch != null) {
editSwitch.setChecked(
selectedTabsAdapter != null && selectedTabsAdapter.editModeEnabled);
editSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (selectedTabsAdapter != null) {
selectedTabsAdapter.setEditMode(isChecked);
}
});
}
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -139,6 +152,23 @@ private void restoreDefaults() {
.show();
}

private void showDeleteTabDialog(final int position, final String tabName) {
new AlertDialog.Builder(requireContext())
.setTitle(tabName)
.setCancelable(true)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.delete, (dialog, which) -> {
tabList.remove(position);
selectedTabsAdapter.notifyItemRemoved(position);

if (tabList.isEmpty()) {
tabList.add(Tab.Type.BLANK.getTab());
selectedTabsAdapter.notifyItemInserted(0);
}
})
.show();
}

private void initButton(final View rootView) {
final FloatingActionButton fab = rootView.findViewById(R.id.addTabsButton);
fab.setOnClickListener(v -> {
Expand Down Expand Up @@ -333,6 +363,12 @@ private class SelectedTabsAdapter
extends RecyclerView.Adapter<ChooseTabsFragment.SelectedTabsAdapter.TabViewHolder> {
private final LayoutInflater inflater;
private final ItemTouchHelper itemTouchHelper;
boolean editModeEnabled = false;

void setEditMode(final boolean enabled) {
editModeEnabled = enabled;
notifyDataSetChanged();
}

SelectedTabsAdapter(final Context context, final ItemTouchHelper itemTouchHelper) {
this.itemTouchHelper = itemTouchHelper;
Expand Down Expand Up @@ -368,13 +404,21 @@ class TabViewHolder extends RecyclerView.ViewHolder {
private final AppCompatImageView tabIconView;
private final TextView tabNameView;
private final ImageView handle;
private final View itemActions;
private final ImageButton btnMoveUp;
private final ImageButton btnMoveDown;
private final ImageButton btnDelete;

TabViewHolder(final View itemView) {
super(itemView);

tabNameView = itemView.findViewById(R.id.tabName);
tabIconView = itemView.findViewById(R.id.tabIcon);
handle = itemView.findViewById(R.id.handle);
itemActions = itemView.findViewById(R.id.itemActions);
btnMoveUp = itemView.findViewById(R.id.btnMoveUp);
btnMoveDown = itemView.findViewById(R.id.btnMoveDown);
btnDelete = itemView.findViewById(R.id.btnDelete);
}

@SuppressLint("ClickableViewAccessibility")
Expand All @@ -390,6 +434,33 @@ void bind(final int position, final TabViewHolder holder) {

tabNameView.setText(getTabName(type, tab));
tabIconView.setImageResource(tab.getTabIconRes(requireContext()));

handle.setVisibility(editModeEnabled ? View.GONE : View.VISIBLE);
itemActions.setVisibility(editModeEnabled ? View.VISIBLE : View.GONE);
if (editModeEnabled) {
btnMoveUp.setEnabled(position > 0);
btnMoveDown.setEnabled(position < getItemCount() - 1);
btnMoveUp.setOnClickListener(v -> {
final int pos = getBindingAdapterPosition();
if (pos > 0) {
Collections.swap(tabList, pos, pos - 1);
notifyDataSetChanged();
}
});
btnMoveDown.setOnClickListener(v -> {
final int pos = getBindingAdapterPosition();
if (pos < getItemCount() - 1) {
Collections.swap(tabList, pos, pos + 1);
notifyDataSetChanged();
}
});
btnDelete.setOnClickListener(v -> {
final int pos = getBindingAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
showDeleteTabDialog(pos, tabNameView.getText().toString());
}
});
}
}

private String getTabName(@NonNull final Tab.Type type, @NonNull final Tab tab) {
Expand Down
61 changes: 52 additions & 9 deletions app/src/main/res/layout/item_instance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,62 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/handle" />
android:layout_toLeftOf="@id/rightContainer" />

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/handle"
<LinearLayout
android:id="@+id/rightContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingRight="10dp"
android:paddingBottom="12dp"
android:src="@drawable/ic_drag_handle"
tools:ignore="ContentDescription,RtlHardcoded" />
android:orientation="horizontal"
tools:ignore="RtlHardcoded">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingRight="10dp"
android:paddingBottom="12dp"
android:src="@drawable/ic_drag_handle"
tools:ignore="ContentDescription,RtlHardcoded" />

<LinearLayout
android:id="@+id/itemActions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone">

<ImageButton
android:id="@+id/btnMoveUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/move_up"
android:padding="8dp"
android:src="@drawable/ic_arrow_drop_up" />

<ImageButton
android:id="@+id/btnMoveDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/move_down"
android:padding="8dp"
android:src="@drawable/ic_arrow_drop_down" />

<ImageButton
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/delete"
android:padding="8dp"
android:src="@drawable/ic_delete" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
61 changes: 52 additions & 9 deletions app/src/main/res/layout/list_choose_tabs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,68 @@
android:layout_marginLeft="16dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_toLeftOf="@+id/handle"
android:layout_toLeftOf="@+id/rightContainer"
android:layout_toRightOf="@+id/tabIcon"
android:ellipsize="end"
android:maxLines="2"
android:textAppearance="?textAppearanceListItem"
tools:ignore="RtlHardcoded"
tools:text="Lorem ipsum dolor sit amet" />

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/handle"
<LinearLayout
android:id="@+id/rightContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingRight="16dp"
android:paddingBottom="12dp"
android:src="@drawable/ic_drag_handle"
tools:ignore="ContentDescription,RtlHardcoded" />
android:orientation="horizontal"
tools:ignore="RtlHardcoded">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingRight="16dp"
android:paddingBottom="12dp"
android:src="@drawable/ic_drag_handle"
tools:ignore="ContentDescription,RtlHardcoded" />

<LinearLayout
android:id="@+id/itemActions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone">

<ImageButton
android:id="@+id/btnMoveUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/move_up"
android:padding="8dp"
android:src="@drawable/ic_arrow_drop_up" />

<ImageButton
android:id="@+id/btnMoveDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/move_down"
android:padding="8dp"
android:src="@drawable/ic_arrow_drop_down" />

<ImageButton
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/delete"
android:padding="8dp"
android:src="@drawable/ic_delete" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
5 changes: 5 additions & 0 deletions app/src/main/res/menu/menu_chooser_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
android:title="@string/restore_defaults"
android:icon="@drawable/ic_settings_backup_restore"
app:showAsAction="always" />
<item
android:id="@+id/menu_item_edit_mode"
android:title="@string/arrange"
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
app:showAsAction="always" />
</menu>
Loading
Loading