Skip to content

Commit

Permalink
Improve playlist dialogs, use only one layout
Browse files Browse the repository at this point in the history
Use same class (by extending it) both to append an item to a playlist and to choose a playlist tab
Fixes ui issues with select playlist tab dialog
  • Loading branch information
Stypox committed Jul 24, 2020
1 parent 88c86e8 commit 5d37aa5
Show file tree
Hide file tree
Showing 13 changed files with 623 additions and 629 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
import org.schabi.newpipe.fragments.BaseStateFragment;
import org.schabi.newpipe.fragments.EmptyFragment;
import org.schabi.newpipe.fragments.list.comments.CommentsFragment;
import org.schabi.newpipe.fragments.list.playlist.AppendPlaylistDialog;
import org.schabi.newpipe.fragments.list.videos.RelatedVideosFragment;
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog;
import org.schabi.newpipe.local.history.HistoryRecordManager;
import org.schabi.newpipe.player.MainVideoPlayer;
import org.schabi.newpipe.player.PopupVideoPlayer;
Expand Down Expand Up @@ -409,7 +409,7 @@ public void onClick(final View v) {
break;
case R.id.detail_controls_playlist_append:
if (getFragmentManager() != null && currentInfo != null) {
PlaylistAppendDialog.fromStreamInfo(currentInfo)
AppendPlaylistDialog.fromStreamInfo(currentInfo)
.show(getFragmentManager(), TAG);
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.schabi.newpipe.fragments.list.playlist;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.schabi.newpipe.R;
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity;
import org.schabi.newpipe.database.stream.model.StreamEntity;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.player.playqueue.PlayQueueItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import io.reactivex.android.schedulers.AndroidSchedulers;

public final class AppendPlaylistDialog extends PlaylistDialog
implements PlaylistDialog.OnSelectedListener {
public static final String TAG = AppendPlaylistDialog.class.getSimpleName();

public AppendPlaylistDialog(@Nullable final List<StreamEntity> streamEntities) {
super(streamEntities, false);
setOnSelectedListener(this);
}

@Override
public View onCreateView(@NonNull final LayoutInflater inflater,
final ViewGroup container,
final Bundle savedInstanceState) {

final View view = super.onCreateView(inflater, container, savedInstanceState);

final View newPlaylistButton = Objects.requireNonNull(view).findViewById(R.id.newPlaylist);
newPlaylistButton.setVisibility(View.VISIBLE);
newPlaylistButton.setOnClickListener(v -> openCreatePlaylistDialog());

view.findViewById(R.id.titleTextView).setVisibility(View.GONE);

return view;
}

public static AppendPlaylistDialog fromStreamInfo(final StreamInfo streamInfo) {
return new AppendPlaylistDialog(Collections.singletonList(new StreamEntity(streamInfo)));
}

public static AppendPlaylistDialog fromStreamInfoItems(
final List<StreamInfoItem> streamInfoItems) {

final List<StreamEntity> streamEntities = new ArrayList<>(streamInfoItems.size());
for (final StreamInfoItem streamInfoItem : streamInfoItems) {
streamEntities.add(new StreamEntity(streamInfoItem));
}
return new AppendPlaylistDialog(streamEntities);
}

public static AppendPlaylistDialog fromPlayQueueItems(
final List<PlayQueueItem> playQueueItems) {

final List<StreamEntity> streamEntities = new ArrayList<>(playQueueItems.size());
for (final PlayQueueItem playQueueItem : playQueueItems) {
streamEntities.add(new StreamEntity(playQueueItem));
}
return new AppendPlaylistDialog(streamEntities);
}


@Override
public void onLocalPlaylistSelected(final PlaylistMetadataEntry localPlaylist) {
if (getStoredStreamEntities() != null) {
final Toast successToast = Toast.makeText(getContext(),
R.string.playlist_add_stream_success, Toast.LENGTH_SHORT);

if (localPlaylist.thumbnailUrl
.equals("drawable://" + R.drawable.dummy_thumbnail_playlist)) {
dialogDisposables.add(getLocalPlaylistManager()
.changePlaylistThumbnail(localPlaylist.uid,
getStoredStreamEntities().get(0).getThumbnailUrl())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(ignored -> successToast.show()));
}

dialogDisposables.add(getLocalPlaylistManager()
.appendToPlaylist(localPlaylist.uid, getStoredStreamEntities())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(ignored -> successToast.show()));

requireDialog().dismiss();
}
}

@Override
public void onRemotePlaylistSelected(final PlaylistRemoteEntity remotePlaylist) {
// unreachable code
}

public void openCreatePlaylistDialog() {
if (getStoredStreamEntities() == null) {
return;
}

new CreatePlaylistDialog(getStoredStreamEntities())
.show(getParentFragmentManager(), TAG);
requireDialog().dismiss();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.schabi.newpipe.fragments.list.playlist;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import org.schabi.newpipe.NewPipeDatabase;
import org.schabi.newpipe.R;
import org.schabi.newpipe.database.stream.model.StreamEntity;
import org.schabi.newpipe.local.playlist.LocalPlaylistManager;
import org.schabi.newpipe.util.StateSaver;

import java.util.List;
import java.util.Queue;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public final class CreatePlaylistDialog extends DialogFragment implements StateSaver.WriteRead {

/*//////////////////////////////////////////////////////////////////////////
// Dialog
//////////////////////////////////////////////////////////////////////////*/

private Disposable createPlaylistDisposable;

private List<StreamEntity> streamEntities;
private StateSaver.SavedState savedState;

CreatePlaylistDialog(final List<StreamEntity> streamEntities) {
this.streamEntities = streamEntities;
}

protected List<StreamEntity> getStreams() {
return streamEntities;
}

/*//////////////////////////////////////////////////////////////////////////
// LifeCycle
//////////////////////////////////////////////////////////////////////////*/

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
savedState = StateSaver.tryToRestore(savedInstanceState, this);
}

@Override
public void onDestroy() {
super.onDestroy();
StateSaver.onDestroy(savedState);

if (createPlaylistDisposable != null) {
createPlaylistDisposable.dispose();
}
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) {
if (getStreams() == null) {
final Dialog dialog = super.onCreateDialog(savedInstanceState);
//remove title
final Window window = dialog.getWindow();
if (window != null) {
window.requestFeature(Window.FEATURE_NO_TITLE);
}
return dialog;
}

View dialogView = View.inflate(getContext(), R.layout.dialog_playlist_name, null);
EditText nameInput = dialogView.findViewById(R.id.playlist_name);

final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext())
.setTitle(R.string.create_playlist)
.setView(dialogView)
.setCancelable(true)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.create, (dialogInterface, i) -> {
final String name = nameInput.getText().toString();
final LocalPlaylistManager playlistManager =
new LocalPlaylistManager(NewPipeDatabase.getInstance(requireContext()));
final Toast successToast = Toast.makeText(getActivity(),
R.string.playlist_creation_success,
Toast.LENGTH_SHORT);

createPlaylistDisposable = playlistManager.createPlaylist(name, getStreams())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(longs -> successToast.show());
});

return dialogBuilder.create();
}

/*//////////////////////////////////////////////////////////////////////////
// State Saving
//////////////////////////////////////////////////////////////////////////*/

@Override
public String generateSuffix() {
final int size = streamEntities == null ? 0 : streamEntities.size();
return "." + size + ".list";
}

@Override
public void writeTo(final Queue<Object> objectsToSave) {
objectsToSave.add(streamEntities);
}

@Override
@SuppressWarnings("unchecked")
public void readFrom(@NonNull final Queue<Object> savedObjects) {
streamEntities = (List<StreamEntity>) savedObjects.poll();
}

@Override
public void onSaveInstanceState(@NonNull final Bundle outState) {
super.onSaveInstanceState(outState);
if (getActivity() != null) {
savedState = StateSaver.tryToSave(getActivity().isChangingConfigurations(),
savedState, outState, this);
}
}
}
Loading

0 comments on commit 5d37aa5

Please sign in to comment.