Use custom dialog to edit and delete local playlists at once

This commit is contained in:
Xiang Rong Lin 2020-01-21 20:56:06 +01:00
parent 77aa12dd81
commit 0ed3354cee
4 changed files with 118 additions and 54 deletions

View File

@ -1,13 +1,9 @@
package org.schabi.newpipe.local.bookmark;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
@ -26,6 +22,7 @@ import org.schabi.newpipe.database.playlist.PlaylistLocalItem;
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity;
import org.schabi.newpipe.local.BaseLocalListFragment;
import org.schabi.newpipe.local.dialog.BookmarkDialog;
import org.schabi.newpipe.local.playlist.LocalPlaylistManager;
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
import org.schabi.newpipe.report.UserAction;
@ -124,34 +121,7 @@ public final class BookmarkFragment
@Override
public void held(LocalItem selectedItem) {
if (selectedItem instanceof PlaylistMetadataEntry) {
final Resources resources = getContext().getResources();
String[] commands = new String[]{
resources.getString(R.string.rename_playlist),
resources.getString(R.string.delete_playlist)
};
final DialogInterface.OnClickListener actions = (dialogInterface, i) -> {
switch (i) {
case 0:
showLocalRenameDialog((PlaylistMetadataEntry) selectedItem);
break;
case 1:
showLocalDeleteDialog((PlaylistMetadataEntry) selectedItem);
break;
}
};
final View bannerView = View.inflate(activity, R.layout.dialog_title, null);
bannerView.setSelected(true);
TextView titleView = bannerView.findViewById(R.id.itemTitleView);
titleView.setText(((PlaylistMetadataEntry) selectedItem).name);
new AlertDialog.Builder(getActivity())
.setCustomTitle(bannerView)
.setItems(commands, actions)
.create()
.show();
showLocalDialog((PlaylistMetadataEntry) selectedItem);
} else if (selectedItem instanceof PlaylistRemoteEntity) {
showRemoteDeleteDialog((PlaylistRemoteEntity) selectedItem);
}
@ -279,14 +249,27 @@ public final class BookmarkFragment
// Utils
///////////////////////////////////////////////////////////////////////////
private void showLocalDeleteDialog(final PlaylistMetadataEntry item) {
showDeleteDialog(item.name, localPlaylistManager.deletePlaylist(item.uid));
}
private void showRemoteDeleteDialog(final PlaylistRemoteEntity item) {
showDeleteDialog(item.getName(), remotePlaylistManager.deletePlaylist(item.getUid()));
}
private void showLocalDialog(PlaylistMetadataEntry selectedItem) {
BookmarkDialog dialog = new BookmarkDialog(getContext(),
selectedItem.name, new BookmarkDialog.OnClickListener() {
@Override
public void onDeleteClicked() {
showDeleteDialog(selectedItem.name,
localPlaylistManager.deletePlaylist(selectedItem.uid));
}
@Override
public void onSaveClicked(@NonNull String name) {
changeLocalPlaylistName(selectedItem.uid, name);
}
});
dialog.show();
}
private void showDeleteDialog(final String name, final Single<Integer> deleteReactor) {
if (activity == null || disposables == null) return;
@ -303,24 +286,6 @@ public final class BookmarkFragment
.show();
}
private void showLocalRenameDialog(PlaylistMetadataEntry selectedItem) {
final View dialogView = View.inflate(getContext(), R.layout.dialog_playlist_name, null);
EditText nameEdit = dialogView.findViewById(R.id.playlist_name);
nameEdit.setText(selectedItem.name);
nameEdit.setSelection(nameEdit.getText().length());
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(
getContext())
.setTitle(R.string.rename_playlist)
.setView(dialogView)
.setCancelable(true)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.rename, (dialogInterface, i) -> {
changeLocalPlaylistName(selectedItem.uid, nameEdit.getText().toString());
});
dialogBuilder.show();
}
private void changeLocalPlaylistName(long id, String name) {
if (localPlaylistManager == null) {
return;

View File

@ -0,0 +1,47 @@
package org.schabi.newpipe.local.dialog
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Window
import android.widget.Button
import android.widget.EditText
import org.schabi.newpipe.R
class BookmarkDialog(
context: Context,
private val playlistName: String,
val listener: OnClickListener)
: Dialog(context) {
private lateinit var editText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.dialog_bookmark)
initListeners()
}
private fun initListeners() {
editText = findViewById(R.id.playlist_name_edit_text);
editText.setText(playlistName)
findViewById<Button>(R.id.bookmark_delete).setOnClickListener {
listener.onDeleteClicked()
dismiss()
}
findViewById<Button>(R.id.bookmark_cancel).setOnClickListener {
dismiss()
}
findViewById<Button>(R.id.bookmark_save).setOnClickListener {
listener.onSaveClicked(editText.text.toString())
dismiss()
}
}
interface OnClickListener {
fun onDeleteClicked()
fun onSaveClicked(name: String)
}
}

View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/playlist_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="no"
android:inputType="text"
android:maxLines="1"
android:layout_margin="10dp"
android:hint="@string/playlist_name_input"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="10dp"
android:padding="5dp">
<Button
android:id="@+id/bookmark_delete"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:text="@string/delete" />
<Button
android:id="@+id/bookmark_cancel"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@+id/bookmark_save"
android:text="@android:string/cancel" />
<Button
android:id="@+id/bookmark_save"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="@string/save" />
</RelativeLayout>
</LinearLayout>

View File

@ -589,4 +589,5 @@
<string name="downloads_storage_use_saf_title">Use SAF</string>
<string name="downloads_storage_use_saf_summary">The Storage Access Framework allows downloads to an external SD card.\nNote: some devices are not compatible</string>
<string name="choose_instance_prompt">Choose an instance</string>
<string name="save">save</string>
</resources>