2018-01-17 06:12:03 +01:00
|
|
|
package org.schabi.newpipe.fragments.local;
|
2018-01-16 20:48:52 +01:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.NewPipeDatabase;
|
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
|
|
|
|
import org.schabi.newpipe.database.stream.model.StreamEntity;
|
|
|
|
import org.schabi.newpipe.extractor.InfoItem;
|
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
2018-01-22 04:32:49 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
2018-01-16 20:48:52 +01:00
|
|
|
import org.schabi.newpipe.info_list.InfoListAdapter;
|
2018-01-26 07:24:59 +01:00
|
|
|
import org.schabi.newpipe.info_list.OnInfoItemGesture;
|
2018-01-16 20:48:52 +01:00
|
|
|
import org.schabi.newpipe.info_list.stored.LocalPlaylistInfoItem;
|
2018-01-22 04:32:49 +01:00
|
|
|
import org.schabi.newpipe.playlist.PlayQueueItem;
|
2018-01-16 20:48:52 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2018-01-22 04:32:49 +01:00
|
|
|
import java.util.Collections;
|
2018-01-16 20:48:52 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
public final class PlaylistAppendDialog extends PlaylistDialog {
|
2018-01-16 20:48:52 +01:00
|
|
|
private static final String TAG = PlaylistAppendDialog.class.getCanonicalName();
|
|
|
|
|
|
|
|
private RecyclerView playlistRecyclerView;
|
|
|
|
private InfoListAdapter playlistAdapter;
|
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
public static PlaylistAppendDialog fromStreamInfo(final StreamInfo info) {
|
2018-01-16 20:48:52 +01:00
|
|
|
PlaylistAppendDialog dialog = new PlaylistAppendDialog();
|
2018-01-22 04:32:49 +01:00
|
|
|
dialog.setInfo(Collections.singletonList(new StreamEntity(info)));
|
2018-01-16 20:48:52 +01:00
|
|
|
return dialog;
|
|
|
|
}
|
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
public static PlaylistAppendDialog fromStreamInfoItems(final List<StreamInfoItem> items) {
|
|
|
|
PlaylistAppendDialog dialog = new PlaylistAppendDialog();
|
|
|
|
List<StreamEntity> entities = new ArrayList<>(items.size());
|
|
|
|
for (final StreamInfoItem item : items) {
|
|
|
|
entities.add(new StreamEntity(item));
|
|
|
|
}
|
|
|
|
dialog.setInfo(entities);
|
|
|
|
return dialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PlaylistAppendDialog fromPlayQueueItems(final List<PlayQueueItem> items) {
|
|
|
|
PlaylistAppendDialog dialog = new PlaylistAppendDialog();
|
|
|
|
List<StreamEntity> entities = new ArrayList<>(items.size());
|
|
|
|
for (final PlayQueueItem item : items) {
|
|
|
|
entities.add(new StreamEntity(item));
|
|
|
|
}
|
|
|
|
dialog.setInfo(entities);
|
|
|
|
return dialog;
|
2018-01-16 20:48:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Context context) {
|
|
|
|
super.onAttach(context);
|
|
|
|
playlistAdapter = new InfoListAdapter(getActivity());
|
|
|
|
playlistAdapter.useMiniItemVariants(true);
|
|
|
|
}
|
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Views
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2018-01-16 20:48:52 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
return inflater.inflate(R.layout.dialog_playlists, container);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
final View newPlaylistButton = view.findViewById(R.id.newPlaylist);
|
2018-01-16 20:48:52 +01:00
|
|
|
playlistRecyclerView = view.findViewById(R.id.playlist_list);
|
|
|
|
playlistRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
|
|
|
playlistRecyclerView.setAdapter(playlistAdapter);
|
|
|
|
|
|
|
|
final LocalPlaylistManager playlistManager =
|
|
|
|
new LocalPlaylistManager(NewPipeDatabase.getInstance(getContext()));
|
|
|
|
|
|
|
|
newPlaylistButton.setOnClickListener(ignored -> openCreatePlaylistDialog());
|
|
|
|
|
2018-01-26 07:24:59 +01:00
|
|
|
playlistAdapter.setOnPlaylistSelectedListener(new OnInfoItemGesture<PlaylistInfoItem>() {
|
2018-01-16 20:48:52 +01:00
|
|
|
@Override
|
|
|
|
public void selected(PlaylistInfoItem selectedItem) {
|
2018-01-22 04:32:49 +01:00
|
|
|
if (!(selectedItem instanceof LocalPlaylistInfoItem) || getStreams() == null)
|
|
|
|
return;
|
|
|
|
|
2018-01-16 20:48:52 +01:00
|
|
|
final long playlistId = ((LocalPlaylistInfoItem) selectedItem).getPlaylistId();
|
|
|
|
final Toast successToast =
|
|
|
|
Toast.makeText(getContext(), "Added", Toast.LENGTH_SHORT);
|
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
playlistManager.appendToPlaylist(playlistId, getStreams())
|
2018-01-16 20:48:52 +01:00
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(ignored -> successToast.show());
|
|
|
|
|
|
|
|
getDialog().dismiss();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
playlistManager.getPlaylists()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(metadataEntries -> {
|
|
|
|
if (metadataEntries.isEmpty()) {
|
|
|
|
openCreatePlaylistDialog();
|
2018-01-17 06:12:03 +01:00
|
|
|
return;
|
2018-01-16 20:48:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
List<InfoItem> playlistInfoItems = new ArrayList<>(metadataEntries.size());
|
|
|
|
for (final PlaylistMetadataEntry metadataEntry : metadataEntries) {
|
|
|
|
playlistInfoItems.add(metadataEntry.toStoredPlaylistInfoItem());
|
|
|
|
}
|
|
|
|
|
|
|
|
playlistAdapter.clearStreamItemList();
|
|
|
|
playlistAdapter.addInfoItemList(playlistInfoItems);
|
|
|
|
playlistRecyclerView.setVisibility(View.VISIBLE);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helper
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
public void openCreatePlaylistDialog() {
|
2018-01-22 04:32:49 +01:00
|
|
|
if (getStreams() == null || getFragmentManager() == null) return;
|
2018-01-16 20:48:52 +01:00
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
PlaylistCreationDialog.newInstance(getStreams()).show(getFragmentManager(), TAG);
|
2018-01-17 06:12:03 +01:00
|
|
|
getDialog().dismiss();
|
2018-01-16 20:48:52 +01:00
|
|
|
}
|
|
|
|
}
|