From 77aa12dd8195dae7a2f782f97d3439e4e39fd1a1 Mon Sep 17 00:00:00 2001 From: Xiang Rong Lin <41164160+XiangRongLin@users.noreply.github.com> Date: Mon, 13 Jan 2020 20:24:24 +0100 Subject: [PATCH 01/20] Rename local playlist by long-clicking in BookmarkFragment. After long clicking on a local playlist, show a dialog with 2 options for "rename" and "delete" Rename shows another dialog to let the user rename the playlist. Delete lets the user delete a playlist like before. --- .../local/bookmark/BookmarkFragment.java | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java index 8f67367aa..6a7f16025 100644 --- a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java @@ -1,8 +1,13 @@ 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; @@ -10,6 +15,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import io.reactivex.disposables.Disposable; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import org.schabi.newpipe.NewPipeDatabase; @@ -118,7 +124,33 @@ public final class BookmarkFragment @Override public void held(LocalItem selectedItem) { if (selectedItem instanceof PlaylistMetadataEntry) { - showLocalDeleteDialog((PlaylistMetadataEntry) selectedItem); + 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(); } else if (selectedItem instanceof PlaylistRemoteEntity) { showRemoteDeleteDialog((PlaylistRemoteEntity) selectedItem); @@ -271,6 +303,39 @@ 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; + } + + Log.d(TAG, "Updating playlist id=[" + id + + "] with new name=[" + name + "] items"); + + localPlaylistManager.renamePlaylist(id, name); + final Disposable disposable = localPlaylistManager.renamePlaylist(id, name) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(longs -> {/*Do nothing on success*/}, this::onError); + disposables.add(disposable); + } + private static List merge(final List localPlaylists, final List remotePlaylists) { List items = new ArrayList<>( From 0ed3354cee6e4e3021263ecdd1b2253dfe144838 Mon Sep 17 00:00:00 2001 From: Xiang Rong Lin <41164160+XiangRongLin@users.noreply.github.com> Date: Tue, 21 Jan 2020 20:56:06 +0100 Subject: [PATCH 02/20] Use custom dialog to edit and delete local playlists at once --- .../local/bookmark/BookmarkFragment.java | 73 +++++-------------- .../newpipe/local/dialog/BookmarkDialog.kt | 47 ++++++++++++ app/src/main/res/layout/dialog_bookmark.xml | 51 +++++++++++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 118 insertions(+), 54 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/local/dialog/BookmarkDialog.kt create mode 100644 app/src/main/res/layout/dialog_bookmark.xml diff --git a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java index 6a7f16025..bfd90acda 100644 --- a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java @@ -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 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; diff --git a/app/src/main/java/org/schabi/newpipe/local/dialog/BookmarkDialog.kt b/app/src/main/java/org/schabi/newpipe/local/dialog/BookmarkDialog.kt new file mode 100644 index 000000000..dd20e88a0 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/local/dialog/BookmarkDialog.kt @@ -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