-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve playlist dialogs, use only one layout
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
Showing
13 changed files
with
623 additions
and
629 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
app/src/main/java/org/schabi/newpipe/fragments/list/playlist/AppendPlaylistDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
app/src/main/java/org/schabi/newpipe/fragments/list/playlist/CreatePlaylistDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.