Tried to implement the feature

This commit is contained in:
Jared Fantaye 2022-12-07 02:32:53 +01:00
parent 8fc5fa979d
commit 10a5741f36
6 changed files with 80 additions and 39 deletions

View File

@ -15,7 +15,7 @@ public class PlaylistEntity {
public static final String PLAYLIST_ID = "uid";
public static final String PLAYLIST_NAME = "name";
public static final String PLAYLIST_THUMBNAIL_URL = "thumbnail_url";
//TODO: add field
public static final String PLAYLIST_THUMBNAIL_SET = "isThumbnailSet";
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = PLAYLIST_ID)
@ -27,9 +27,14 @@ public class PlaylistEntity {
@ColumnInfo(name = PLAYLIST_THUMBNAIL_URL)
private String thumbnailUrl;
public PlaylistEntity(final String name, final String thumbnailUrl) {
@ColumnInfo(name = PLAYLIST_THUMBNAIL_SET)
private boolean isThumbnailSet;
public PlaylistEntity(final String name, final String thumbnailUrl,
final boolean isThumbnailSet) {
this.name = name;
this.thumbnailUrl = thumbnailUrl;
this.isThumbnailSet = isThumbnailSet;
}
public long getUid() {
@ -55,4 +60,13 @@ public class PlaylistEntity {
public void setThumbnailUrl(final String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public boolean getIsThumbnailSet() {
return isThumbnailSet;
}
public void setIsThumbnailSet(final boolean isThumbnailSet) {
this.isThumbnailSet = isThumbnailSet;
}
}

View File

@ -8,6 +8,8 @@ import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -24,7 +26,6 @@ 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.databinding.DialogEditTextBinding;
import org.schabi.newpipe.databinding.DialogTitleBinding;
import org.schabi.newpipe.error.ErrorInfo;
import org.schabi.newpipe.error.UserAction;
import org.schabi.newpipe.local.BaseLocalListFragment;
@ -258,32 +259,51 @@ public final class BookmarkFragment extends BaseLocalListFragment<List<PlaylistL
}
private void showLocalDialog(final PlaylistMetadataEntry selectedItem) {
final boolean isPlaylistThumbnailSet = localPlaylistManager
.getIsPlaylistThumbnailSet(selectedItem.uid);
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
//TODO
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_list_item_1) {
@Override
public View getView(final int position, final View convertView,
final ViewGroup parent) {
final View v = super.getView(position, convertView, parent);
final TextView textView = v.findViewById(android.R.id.text1);
final DialogTitleBinding dialogBinding =
DialogTitleBinding.inflate(LayoutInflater.from(requireContext()));
if (!isPlaylistThumbnailSet && position == 2) {
textView.setEnabled(false);
return v;
}
dialogBinding.itemRoot.setVisibility(View.GONE);
dialogBinding.itemTitleView.setVisibility(View.GONE);
dialogBinding.itemAdditionalDetails.setVisibility(View.GONE);
final String[] items = new String[]{"Delete", "Rename", "Thumbnail"};
final DialogInterface.OnClickListener action = (d, index) -> {
textView.setEnabled(true);
return v;
}
};
arrayAdapter.addAll(getString(R.string.rename), getString(R.string.delete),
getString(R.string.unset_playlist_thumbnail));
// Rename = 0; Delete = 1; Unset Thumbnail = 2
final DialogInterface.OnClickListener action = (dialog, index) -> {
switch (index) {
case 0: showRenameDialog(selectedItem);
break;
case 1:
showDeleteDialog(selectedItem.name,
localPlaylistManager.deletePlaylist(selectedItem.uid));
dialog.dismiss();
break;
case 2:
if (isPlaylistThumbnailSet) {
final String ur = "drawable://" + R.drawable.placeholder_thumbnail_playlist;
localPlaylistManager.changePlaylistThumbnail(selectedItem.uid, ur,
false).observeOn(AndroidSchedulers.mainThread()).subscribe();
}
break;
}
};
//TODO add rename dialog
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setItems(items, action)
builder.setAdapter(arrayAdapter, action)
.create()
.show();
}
@ -302,11 +322,6 @@ public final class BookmarkFragment extends BaseLocalListFragment<List<PlaylistL
selectedItem.uid,
dialogBinding.dialogEditText.getText().toString()))
.setNegativeButton(R.string.cancel, null)
.setNeutralButton(R.string.delete, (dialog, which) -> {
showDeleteDialog(selectedItem.name,
localPlaylistManager.deletePlaylist(selectedItem.uid));
dialog.dismiss();
})
.create()
.show();
}

View File

@ -134,7 +134,7 @@ public final class PlaylistAppendDialog extends PlaylistDialog {
if (playlist.thumbnailUrl
.equals("drawable://" + R.drawable.placeholder_thumbnail_playlist)) {
playlistDisposables.add(manager
.changePlaylistThumbnail(playlist.uid, streams.get(0).getThumbnailUrl())
.changePlaylistThumbnail(playlist.uid, streams.get(0).getThumbnailUrl(), false)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(ignored -> successToast.show()));
}

View File

@ -405,8 +405,9 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
.zipWith(historyIdsMaybe, (playlist, historyStreamIds) -> {
// Remove Watched, Functionality data
final List<PlaylistStreamEntry> notWatchedItems = new ArrayList<>();
final boolean isThumbnailSet = playlistManager
.getIsPlaylistThumbnailSet(playlistId);
boolean thumbnailVideoRemoved = false;
//TODO: add blocker here
if (removePartiallyWatched) {
for (final var playlistItem : playlist) {
@ -415,7 +416,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
if (indexInHistory < 0) {
notWatchedItems.add(playlistItem);
} else if (!thumbnailVideoRemoved
} else if (!isThumbnailSet && !thumbnailVideoRemoved
&& playlistManager.getPlaylistThumbnail(playlistId)
.equals(playlistItem.getStreamEntity().getThumbnailUrl())) {
thumbnailVideoRemoved = true;
@ -436,7 +437,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
if (indexInHistory < 0 || (streamStateEntity != null
&& !streamStateEntity.isFinished(duration))) {
notWatchedItems.add(playlistItem);
} else if (!thumbnailVideoRemoved
} else if (!isThumbnailSet && !thumbnailVideoRemoved
&& playlistManager.getPlaylistThumbnail(playlistId)
.equals(playlistItem.getStreamEntity().getThumbnailUrl())) {
thumbnailVideoRemoved = true;
@ -586,13 +587,12 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
disposables.add(disposable);
}
private void changeThumbnailUrl(final String thumbnailUrl) {
if (playlistManager == null) {
private void changeThumbnailUrl(final String thumbnailUrl, final boolean isPermanent) {
if (playlistManager == null || (!isPermanent && playlistManager
.getIsPlaylistThumbnailSet(playlistId))) {
return;
}
//TODO add blocker here
final Toast successToast = Toast.makeText(getActivity(),
R.string.playlist_thumbnail_change_success,
Toast.LENGTH_SHORT);
@ -603,7 +603,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
}
final Disposable disposable = playlistManager
.changePlaylistThumbnail(playlistId, thumbnailUrl)
.changePlaylistThumbnail(playlistId, thumbnailUrl, isPermanent)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(ignore -> successToast.show(), throwable ->
showError(new ErrorInfo(throwable, UserAction.REQUESTED_BOOKMARK,
@ -612,8 +612,11 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
}
private void updateThumbnailUrl() {
if (playlistManager.getIsPlaylistThumbnailSet(playlistId)) {
return;
}
final String newThumbnailUrl;
//TODO: add blocker here
if (!itemListAdapter.getItemsList().isEmpty()) {
newThumbnailUrl = ((PlaylistStreamEntry) itemListAdapter.getItemsList().get(0))
.getStreamEntity().getThumbnailUrl();
@ -621,7 +624,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
newThumbnailUrl = "drawable://" + R.drawable.placeholder_thumbnail_playlist;
}
changeThumbnailUrl(newThumbnailUrl);
changeThumbnailUrl(newThumbnailUrl, false);
}
private void deleteItem(final PlaylistStreamEntry item) {
@ -789,7 +792,8 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
.setAction(
StreamDialogDefaultEntry.SET_AS_PLAYLIST_THUMBNAIL,
(f, i) ->
changeThumbnailUrl(item.getStreamEntity().getThumbnailUrl()))
changeThumbnailUrl(item.getStreamEntity().getThumbnailUrl(),
true))
.setAction(
StreamDialogDefaultEntry.DELETE,
(f, i) -> deleteItem(item))

View File

@ -41,7 +41,7 @@ public class LocalPlaylistManager {
}
final StreamEntity defaultStream = streams.get(0);
final PlaylistEntity newPlaylist =
new PlaylistEntity(name, defaultStream.getThumbnailUrl());
new PlaylistEntity(name, defaultStream.getThumbnailUrl(), false);
return Maybe.fromCallable(() -> database.runInTransaction(() ->
upsertStreams(playlistTable.insert(newPlaylist), streams, 0))
@ -96,24 +96,29 @@ public class LocalPlaylistManager {
}
public Maybe<Integer> renamePlaylist(final long playlistId, final String name) {
return modifyPlaylist(playlistId, name, null);
return modifyPlaylist(playlistId, name, null, false);
}
public Maybe<Integer> changePlaylistThumbnail(final long playlistId,
final String thumbnailUrl) {
return modifyPlaylist(playlistId, null, thumbnailUrl);
final String thumbnailUrl,
final boolean isPermanent) {
return modifyPlaylist(playlistId, null, thumbnailUrl, isPermanent);
}
public String getPlaylistThumbnail(final long playlistId) {
return playlistTable.getPlaylist(playlistId).blockingFirst().get(0).getThumbnailUrl();
}
public boolean getIsPlaylistThumbnailSet(final long playlistId) {
return playlistTable.getPlaylist(playlistId).blockingFirst().get(0).getIsThumbnailSet();
}
private Maybe<Integer> modifyPlaylist(final long playlistId,
@Nullable final String name,
@Nullable final String thumbnailUrl) {
@Nullable final String thumbnailUrl,
final boolean isPermanent) {
return playlistTable.getPlaylist(playlistId)
.firstElement()
.filter(playlistEntities -> !playlistEntities.isEmpty())
.map(playlistEntities -> {
final PlaylistEntity playlist = playlistEntities.get(0);
if (name != null) {
@ -121,6 +126,7 @@ public class LocalPlaylistManager {
}
if (thumbnailUrl != null) {
playlist.setThumbnailUrl(thumbnailUrl);
playlist.setIsThumbnailSet(isPermanent);
}
return playlistTable.update(playlist);
}).subscribeOn(Schedulers.io());

View File

@ -7,6 +7,7 @@
<string name="install">Install</string>
<string name="cancel">Cancel</string>
<string name="ok">OK</string>
<string name="yes">Yes</string>
<string name="open_in_browser">Open in browser</string>
<string name="mark_as_watched">Mark as watched</string>
<string name="open_in_popup_mode">Open in popup mode</string>
@ -438,6 +439,7 @@
<string name="mute">Mute</string>
<string name="unmute">Unmute</string>
<string name="set_as_playlist_thumbnail">Set as playlist thumbnail</string>
<string name="unset_playlist_thumbnail">Unset thumbnail</string>
<string name="bookmark_playlist">Bookmark Playlist</string>
<string name="unbookmark_playlist">Remove Bookmark</string>
<string name="delete_playlist_prompt">Delete this playlist\?</string>