2017-09-03 06:04:18 +00:00
|
|
|
package org.schabi.newpipe.fragments.list.playlist;
|
|
|
|
|
2022-01-16 16:08:13 +00:00
|
|
|
import static org.schabi.newpipe.ktx.ViewUtils.animate;
|
|
|
|
import static org.schabi.newpipe.ktx.ViewUtils.animateHideRecyclerViewAllowingScrolling;
|
|
|
|
|
2019-08-14 12:17:05 +00:00
|
|
|
import android.content.Context;
|
2017-09-03 06:04:18 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
2018-01-04 04:28:01 +00:00
|
|
|
import android.view.MenuItem;
|
2017-09-03 06:04:18 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2020-03-17 17:41:39 +00:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources;
|
2022-02-23 20:11:25 +00:00
|
|
|
|
|
|
|
import com.google.android.material.shape.CornerFamily;
|
|
|
|
import com.google.android.material.shape.ShapeAppearanceModel;
|
2020-03-31 17:20:15 +00:00
|
|
|
|
2018-02-06 05:32:23 +00:00
|
|
|
import org.reactivestreams.Subscriber;
|
|
|
|
import org.reactivestreams.Subscription;
|
|
|
|
import org.schabi.newpipe.NewPipeDatabase;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.R;
|
2018-02-06 05:32:23 +00:00
|
|
|
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity;
|
2022-05-10 06:43:23 +00:00
|
|
|
import org.schabi.newpipe.database.stream.model.StreamEntity;
|
2020-11-03 05:19:19 +00:00
|
|
|
import org.schabi.newpipe.databinding.PlaylistControlBinding;
|
|
|
|
import org.schabi.newpipe.databinding.PlaylistHeaderBinding;
|
2020-12-11 13:55:47 +00:00
|
|
|
import org.schabi.newpipe.error.ErrorInfo;
|
2021-12-01 08:43:24 +00:00
|
|
|
import org.schabi.newpipe.error.ErrorUtil;
|
2020-12-11 13:55:47 +00:00
|
|
|
import org.schabi.newpipe.error.UserAction;
|
2018-03-18 15:37:49 +00:00
|
|
|
import org.schabi.newpipe.extractor.InfoItem;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.extractor.ListExtractor;
|
2020-03-17 17:41:39 +00:00
|
|
|
import org.schabi.newpipe.extractor.ServiceList;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
|
2020-03-17 17:41:39 +00:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
|
2017-11-11 22:47:34 +00:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.fragments.list.BaseListInfoFragment;
|
2022-02-18 22:46:23 +00:00
|
|
|
import org.schabi.newpipe.info_list.dialog.InfoItemDialog;
|
2022-05-16 06:55:15 +00:00
|
|
|
import org.schabi.newpipe.info_list.dialog.StreamDialogDefaultEntry;
|
2022-05-10 06:43:23 +00:00
|
|
|
import org.schabi.newpipe.local.dialog.PlaylistDialog;
|
2018-06-19 01:22:52 +00:00
|
|
|
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
|
2018-04-21 21:10:01 +00:00
|
|
|
import org.schabi.newpipe.player.playqueue.PlayQueue;
|
|
|
|
import org.schabi.newpipe.player.playqueue.PlaylistPlayQueue;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.util.ExtractorHelper;
|
2020-03-20 19:57:56 +00:00
|
|
|
import org.schabi.newpipe.util.Localization;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
2023-05-02 09:08:32 +00:00
|
|
|
import org.schabi.newpipe.util.image.PicassoHelper;
|
2022-01-16 16:08:13 +00:00
|
|
|
import org.schabi.newpipe.util.external_communication.ShareUtils;
|
2023-09-09 23:11:00 +00:00
|
|
|
import org.schabi.newpipe.util.PlayButtonHelper;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2018-03-18 15:37:49 +00:00
|
|
|
import java.util.ArrayList;
|
2018-02-06 05:32:23 +00:00
|
|
|
import java.util.List;
|
2018-02-09 03:53:04 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2022-01-16 16:08:13 +00:00
|
|
|
import java.util.function.Supplier;
|
2022-05-10 06:43:23 +00:00
|
|
|
import java.util.stream.Collectors;
|
2018-02-06 05:32:23 +00:00
|
|
|
|
2020-10-31 20:55:45 +00:00
|
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
|
|
|
import io.reactivex.rxjava3.core.Flowable;
|
|
|
|
import io.reactivex.rxjava3.core.Single;
|
|
|
|
import io.reactivex.rxjava3.disposables.CompositeDisposable;
|
|
|
|
import io.reactivex.rxjava3.disposables.Disposable;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2023-09-09 23:11:00 +00:00
|
|
|
public class PlaylistFragment extends BaseListInfoFragment<StreamInfoItem, PlaylistInfo>
|
|
|
|
implements PlaylistControlViewHolder {
|
2021-03-27 13:37:44 +00:00
|
|
|
|
|
|
|
private static final String PICASSO_PLAYLIST_TAG = "PICASSO_PLAYLIST_TAG";
|
|
|
|
|
2018-02-06 05:32:23 +00:00
|
|
|
private CompositeDisposable disposables;
|
|
|
|
private Subscription bookmarkReactor;
|
2018-02-09 03:53:04 +00:00
|
|
|
private AtomicBoolean isBookmarkButtonReady;
|
2018-02-06 05:32:23 +00:00
|
|
|
|
|
|
|
private RemotePlaylistManager remotePlaylistManager;
|
|
|
|
private PlaylistRemoteEntity playlistEntity;
|
2021-03-27 13:37:44 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Views
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2020-11-03 05:19:19 +00:00
|
|
|
private PlaylistHeaderBinding headerBinding;
|
|
|
|
private PlaylistControlBinding playlistControlBinding;
|
2017-09-04 17:23:56 +00:00
|
|
|
|
2018-02-06 05:32:23 +00:00
|
|
|
private MenuItem playlistBookmarkButton;
|
2018-01-29 07:01:06 +00:00
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
public static PlaylistFragment getInstance(final int serviceId, final String url,
|
|
|
|
final String name) {
|
2020-08-16 08:24:58 +00:00
|
|
|
final PlaylistFragment instance = new PlaylistFragment();
|
2018-07-10 14:26:42 +00:00
|
|
|
instance.setInitialData(serviceId, url, name);
|
2017-09-03 06:04:18 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2020-12-11 13:55:47 +00:00
|
|
|
public PlaylistFragment() {
|
|
|
|
super(UserAction.REQUESTED_PLAYLIST);
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onCreate(final Bundle savedInstanceState) {
|
2018-02-06 05:32:23 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
disposables = new CompositeDisposable();
|
2018-02-09 03:53:04 +00:00
|
|
|
isBookmarkButtonReady = new AtomicBoolean(false);
|
2020-03-31 17:20:15 +00:00
|
|
|
remotePlaylistManager = new RemotePlaylistManager(NewPipeDatabase
|
|
|
|
.getInstance(requireContext()));
|
2018-02-06 05:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public View onCreateView(@NonNull final LayoutInflater inflater,
|
|
|
|
@Nullable final ViewGroup container,
|
|
|
|
@Nullable final Bundle savedInstanceState) {
|
2017-09-03 06:04:18 +00:00
|
|
|
return inflater.inflate(R.layout.fragment_playlist, container, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2020-11-03 05:19:19 +00:00
|
|
|
@Override
|
2022-01-16 16:08:13 +00:00
|
|
|
protected Supplier<View> getListHeaderSupplier() {
|
2020-11-03 05:19:19 +00:00
|
|
|
headerBinding = PlaylistHeaderBinding
|
|
|
|
.inflate(activity.getLayoutInflater(), itemsList, false);
|
|
|
|
playlistControlBinding = headerBinding.playlistControl;
|
2018-02-08 23:58:48 +00:00
|
|
|
|
2022-01-16 16:08:13 +00:00
|
|
|
return headerBinding::getRoot;
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
protected void initViews(final View rootView, final Bundle savedInstanceState) {
|
2017-09-03 06:04:18 +00:00
|
|
|
super.initViews(rootView, savedInstanceState);
|
|
|
|
|
2022-11-02 18:56:10 +00:00
|
|
|
// Is mini variant still relevant?
|
|
|
|
// Only the remote playlist screen uses it now
|
2020-03-31 17:20:15 +00:00
|
|
|
infoListAdapter.setUseMiniVariant(true);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
private PlayQueue getPlayQueueStartingAt(final StreamInfoItem infoItem) {
|
2019-08-14 12:17:05 +00:00
|
|
|
return getPlayQueue(Math.max(infoListAdapter.getItemsList().indexOf(infoItem), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-27 15:14:26 +00:00
|
|
|
protected void showInfoItemDialog(final StreamInfoItem item) {
|
2019-08-14 12:17:05 +00:00
|
|
|
final Context context = getContext();
|
2022-02-18 22:44:14 +00:00
|
|
|
try {
|
|
|
|
final InfoItemDialog.Builder dialogBuilder =
|
|
|
|
new InfoItemDialog.Builder(getActivity(), context, this, item);
|
|
|
|
|
|
|
|
dialogBuilder
|
|
|
|
.setAction(
|
|
|
|
StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND,
|
|
|
|
(f, infoItem) -> NavigationHelper.playOnBackgroundPlayer(
|
|
|
|
context, getPlayQueueStartingAt(infoItem), true))
|
|
|
|
.create()
|
|
|
|
.show();
|
|
|
|
} catch (final IllegalArgumentException e) {
|
|
|
|
InfoItemDialog.Builder.reportErrorDuringInitialization(e, item);
|
2020-03-31 17:20:15 +00:00
|
|
|
}
|
2019-08-14 12:17:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:28:01 +00:00
|
|
|
@Override
|
2021-06-19 22:29:18 +00:00
|
|
|
public void onCreateOptionsMenu(@NonNull final Menu menu,
|
|
|
|
@NonNull final MenuInflater inflater) {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "onCreateOptionsMenu() called with: "
|
|
|
|
+ "menu = [" + menu + "], inflater = [" + inflater + "]");
|
|
|
|
}
|
2018-01-04 04:28:01 +00:00
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
inflater.inflate(R.menu.menu_playlist, menu);
|
2018-01-29 07:01:06 +00:00
|
|
|
|
2018-02-06 05:32:23 +00:00
|
|
|
playlistBookmarkButton = menu.findItem(R.id.menu_item_bookmark);
|
2018-02-09 03:53:04 +00:00
|
|
|
updateBookmarkButtons();
|
2018-02-06 05:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
2020-11-03 05:19:19 +00:00
|
|
|
headerBinding = null;
|
|
|
|
playlistControlBinding = null;
|
|
|
|
|
2018-02-06 05:32:23 +00:00
|
|
|
super.onDestroyView();
|
2020-03-31 17:20:15 +00:00
|
|
|
if (isBookmarkButtonReady != null) {
|
|
|
|
isBookmarkButtonReady.set(false);
|
|
|
|
}
|
2018-02-09 03:53:04 +00:00
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
if (disposables != null) {
|
|
|
|
disposables.clear();
|
|
|
|
}
|
|
|
|
if (bookmarkReactor != null) {
|
|
|
|
bookmarkReactor.cancel();
|
|
|
|
}
|
2018-02-06 05:32:23 +00:00
|
|
|
|
|
|
|
bookmarkReactor = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
if (disposables != null) {
|
|
|
|
disposables.dispose();
|
|
|
|
}
|
2018-02-06 05:32:23 +00:00
|
|
|
|
|
|
|
disposables = null;
|
|
|
|
remotePlaylistManager = null;
|
|
|
|
playlistEntity = null;
|
2018-02-09 03:53:04 +00:00
|
|
|
isBookmarkButtonReady = null;
|
2018-01-04 04:28:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Load and handle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
2022-02-19 15:53:37 +00:00
|
|
|
protected Single<ListExtractor.InfoItemsPage<StreamInfoItem>> loadMoreItemsLogic() {
|
2020-04-15 13:31:53 +00:00
|
|
|
return ExtractorHelper.getMorePlaylistItems(serviceId, url, currentNextPage);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
protected Single<PlaylistInfo> loadResult(final boolean forceLoad) {
|
2018-07-10 14:26:42 +00:00
|
|
|
return ExtractorHelper.getPlaylistInfo(serviceId, url, forceLoad);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:28:01 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
2018-01-04 04:28:01 +00:00
|
|
|
switch (item.getItemId()) {
|
2020-02-22 09:36:10 +00:00
|
|
|
case R.id.action_settings:
|
|
|
|
NavigationHelper.openSettings(requireContext());
|
|
|
|
break;
|
2018-01-04 04:28:01 +00:00
|
|
|
case R.id.menu_item_openInBrowser:
|
2020-02-22 09:36:10 +00:00
|
|
|
ShareUtils.openUrlInBrowser(requireContext(), url);
|
2018-01-04 04:28:01 +00:00
|
|
|
break;
|
2018-01-29 07:01:06 +00:00
|
|
|
case R.id.menu_item_share:
|
2022-12-31 22:30:12 +00:00
|
|
|
ShareUtils.shareText(requireContext(), name, url,
|
2023-05-01 21:11:48 +00:00
|
|
|
currentInfo == null ? List.of() : currentInfo.getThumbnails());
|
2018-01-04 04:28:01 +00:00
|
|
|
break;
|
2018-02-06 05:32:23 +00:00
|
|
|
case R.id.menu_item_bookmark:
|
2018-02-09 03:53:04 +00:00
|
|
|
onBookmarkClicked();
|
2018-01-29 07:01:06 +00:00
|
|
|
break;
|
2022-05-10 06:43:23 +00:00
|
|
|
case R.id.menu_item_append_playlist:
|
2022-12-09 08:39:40 +00:00
|
|
|
if (currentInfo != null) {
|
|
|
|
disposables.add(PlaylistDialog.createCorrespondingDialog(
|
|
|
|
getContext(),
|
|
|
|
getPlayQueue()
|
|
|
|
.getStreams()
|
|
|
|
.stream()
|
|
|
|
.map(StreamEntity::new)
|
|
|
|
.collect(Collectors.toList()),
|
|
|
|
dialog -> dialog.show(getFM(), TAG)
|
|
|
|
));
|
|
|
|
}
|
2022-05-10 06:43:23 +00:00
|
|
|
break;
|
2018-01-04 04:28:01 +00:00
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Contract
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showLoading() {
|
|
|
|
super.showLoading();
|
2021-01-16 03:32:01 +00:00
|
|
|
animate(headerBinding.getRoot(), false, 200);
|
2021-01-15 10:26:09 +00:00
|
|
|
animateHideRecyclerViewAllowingScrolling(itemsList);
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2021-03-27 13:37:44 +00:00
|
|
|
PicassoHelper.cancelTag(PICASSO_PLAYLIST_TAG);
|
2021-01-16 03:32:01 +00:00
|
|
|
animate(headerBinding.uploaderLayout, false, 200);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleResult(@NonNull final PlaylistInfo result) {
|
|
|
|
super.handleResult(result);
|
|
|
|
|
2021-01-16 03:32:01 +00:00
|
|
|
animate(headerBinding.getRoot(), true, 100);
|
|
|
|
animate(headerBinding.uploaderLayout, true, 300);
|
2020-11-03 05:19:19 +00:00
|
|
|
headerBinding.uploaderLayout.setOnClickListener(null);
|
2020-03-31 17:20:15 +00:00
|
|
|
// If we have an uploader put them into the UI
|
|
|
|
if (!TextUtils.isEmpty(result.getUploaderName())) {
|
2020-11-03 05:19:19 +00:00
|
|
|
headerBinding.uploaderName.setText(result.getUploaderName());
|
2017-12-08 14:05:08 +00:00
|
|
|
if (!TextUtils.isEmpty(result.getUploaderUrl())) {
|
2020-11-03 05:19:19 +00:00
|
|
|
headerBinding.uploaderLayout.setOnClickListener(v -> {
|
2018-07-08 15:46:21 +00:00
|
|
|
try {
|
2020-08-03 12:47:02 +00:00
|
|
|
NavigationHelper.openChannelFragment(getFM(), result.getServiceId(),
|
|
|
|
result.getUploaderUrl(), result.getUploaderName());
|
2020-08-16 08:24:58 +00:00
|
|
|
} catch (final Exception e) {
|
2021-12-01 08:43:24 +00:00
|
|
|
ErrorUtil.showUiErrorSnackbar(this, "Opening channel fragment", e);
|
2018-07-08 15:46:21 +00:00
|
|
|
}
|
|
|
|
});
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
2020-04-02 11:51:10 +00:00
|
|
|
} else { // Otherwise say we have no uploader
|
2020-11-03 05:19:19 +00:00
|
|
|
headerBinding.uploaderName.setText(R.string.playlist_no_uploader);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 05:19:19 +00:00
|
|
|
playlistControlBinding.getRoot().setVisibility(View.VISIBLE);
|
2017-11-12 08:16:51 +00:00
|
|
|
|
2020-03-17 17:41:39 +00:00
|
|
|
if (result.getServiceId() == ServiceList.YouTube.getServiceId()
|
|
|
|
&& (YoutubeParsingHelper.isYoutubeMixId(result.getId())
|
|
|
|
|| YoutubeParsingHelper.isYoutubeMusicMixId(result.getId()))) {
|
|
|
|
// this is an auto-generated playlist (e.g. Youtube mix), so a radio is shown
|
2022-02-23 20:11:25 +00:00
|
|
|
final ShapeAppearanceModel model = ShapeAppearanceModel.builder()
|
|
|
|
.setAllCorners(CornerFamily.ROUNDED, 0f)
|
|
|
|
.build(); // this turns the image back into a square
|
|
|
|
headerBinding.uploaderAvatarView.setShapeAppearanceModel(model);
|
2022-05-16 06:55:15 +00:00
|
|
|
headerBinding.uploaderAvatarView.setStrokeColor(AppCompatResources
|
|
|
|
.getColorStateList(requireContext(), R.color.transparent_background_color));
|
2020-11-03 05:19:19 +00:00
|
|
|
headerBinding.uploaderAvatarView.setImageDrawable(
|
|
|
|
AppCompatResources.getDrawable(requireContext(),
|
2021-03-27 14:45:49 +00:00
|
|
|
R.drawable.ic_radio)
|
2020-11-03 05:19:19 +00:00
|
|
|
);
|
2020-03-17 17:41:39 +00:00
|
|
|
} else {
|
2023-05-01 21:11:48 +00:00
|
|
|
PicassoHelper.loadAvatar(result.getUploaderAvatars()).tag(PICASSO_PLAYLIST_TAG)
|
2021-03-27 13:37:44 +00:00
|
|
|
.into(headerBinding.uploaderAvatarView);
|
2020-03-17 17:41:39 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 05:19:19 +00:00
|
|
|
headerBinding.playlistStreamCount.setText(Localization
|
2020-03-20 19:57:56 +00:00
|
|
|
.localizeStreamCount(getContext(), result.getStreamCount()));
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2017-12-08 14:05:08 +00:00
|
|
|
if (!result.getErrors().isEmpty()) {
|
2020-12-11 13:55:47 +00:00
|
|
|
showSnackBarError(new ErrorInfo(result.getErrors(), UserAction.REQUESTED_PLAYLIST,
|
|
|
|
result.getUrl(), result));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
2017-09-04 17:23:56 +00:00
|
|
|
|
2018-02-11 01:20:56 +00:00
|
|
|
remotePlaylistManager.getPlaylist(result)
|
2018-06-19 01:22:52 +00:00
|
|
|
.flatMap(lists -> getUpdateProcessor(lists, result), (lists, id) -> lists)
|
2018-02-11 01:20:56 +00:00
|
|
|
.onBackpressureLatest()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(getPlaylistBookmarkSubscriber());
|
|
|
|
|
2023-09-09 23:11:00 +00:00
|
|
|
PlayButtonHelper.initPlaylistControlClickListener(activity, playlistControlBinding, this);
|
2017-09-04 17:23:56 +00:00
|
|
|
}
|
|
|
|
|
2023-09-09 23:11:00 +00:00
|
|
|
public PlayQueue getPlayQueue() {
|
2017-11-11 22:47:34 +00:00
|
|
|
return getPlayQueue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private PlayQueue getPlayQueue(final int index) {
|
2018-03-18 15:37:49 +00:00
|
|
|
final List<StreamInfoItem> infoItems = new ArrayList<>();
|
2020-08-16 08:24:58 +00:00
|
|
|
for (final InfoItem i : infoListAdapter.getItemsList()) {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (i instanceof StreamInfoItem) {
|
2018-03-18 15:37:49 +00:00
|
|
|
infoItems.add((StreamInfoItem) i);
|
|
|
|
}
|
|
|
|
}
|
2017-11-02 06:38:18 +00:00
|
|
|
return new PlaylistPlayQueue(
|
2017-12-08 14:05:08 +00:00
|
|
|
currentInfo.getServiceId(),
|
|
|
|
currentInfo.getUrl(),
|
2020-04-15 13:31:53 +00:00
|
|
|
currentInfo.getNextPage(),
|
2018-03-18 15:37:49 +00:00
|
|
|
infoItems,
|
2017-11-11 22:47:34 +00:00
|
|
|
index
|
2017-09-06 00:48:48 +00:00
|
|
|
);
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
private Flowable<Integer> getUpdateProcessor(
|
|
|
|
@NonNull final List<PlaylistRemoteEntity> playlists,
|
|
|
|
@NonNull final PlaylistInfo result) {
|
2018-06-19 01:22:52 +00:00
|
|
|
final Flowable<Integer> noItemToUpdate = Flowable.just(/*noItemToUpdate=*/-1);
|
2020-03-31 17:20:15 +00:00
|
|
|
if (playlists.isEmpty()) {
|
|
|
|
return noItemToUpdate;
|
|
|
|
}
|
2018-06-19 01:22:52 +00:00
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
final PlaylistRemoteEntity playlistRemoteEntity = playlists.get(0);
|
|
|
|
if (playlistRemoteEntity.isIdenticalTo(result)) {
|
|
|
|
return noItemToUpdate;
|
|
|
|
}
|
2018-06-19 01:22:52 +00:00
|
|
|
|
|
|
|
return remotePlaylistManager.onUpdate(playlists.get(0).getUid(), result).toFlowable();
|
|
|
|
}
|
|
|
|
|
2018-02-06 05:32:23 +00:00
|
|
|
private Subscriber<List<PlaylistRemoteEntity>> getPlaylistBookmarkSubscriber() {
|
2022-01-15 22:55:19 +00:00
|
|
|
return new Subscriber<>() {
|
2018-02-06 05:32:23 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onSubscribe(final Subscription s) {
|
|
|
|
if (bookmarkReactor != null) {
|
|
|
|
bookmarkReactor.cancel();
|
|
|
|
}
|
2018-02-06 05:32:23 +00:00
|
|
|
bookmarkReactor = s;
|
|
|
|
bookmarkReactor.request(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onNext(final List<PlaylistRemoteEntity> playlist) {
|
2018-02-06 05:32:23 +00:00
|
|
|
playlistEntity = playlist.isEmpty() ? null : playlist.get(0);
|
2018-02-09 03:53:04 +00:00
|
|
|
|
|
|
|
updateBookmarkButtons();
|
|
|
|
isBookmarkButtonReady.set(true);
|
2018-02-06 05:32:23 +00:00
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
if (bookmarkReactor != null) {
|
|
|
|
bookmarkReactor.request(1);
|
|
|
|
}
|
2018-02-06 05:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-12-11 13:55:47 +00:00
|
|
|
public void onError(final Throwable throwable) {
|
|
|
|
showError(new ErrorInfo(throwable, UserAction.REQUESTED_BOOKMARK,
|
|
|
|
"Get playlist bookmarks"));
|
2018-02-06 05:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onComplete() { }
|
2018-02-06 05:32:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void setTitle(final String title) {
|
2017-09-03 06:04:18 +00:00
|
|
|
super.setTitle(title);
|
2021-03-28 20:25:51 +00:00
|
|
|
if (headerBinding != null) {
|
|
|
|
headerBinding.playlistTitleView.setText(title);
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
2018-02-06 05:32:23 +00:00
|
|
|
|
2018-02-09 03:53:04 +00:00
|
|
|
private void onBookmarkClicked() {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (isBookmarkButtonReady == null || !isBookmarkButtonReady.get()
|
|
|
|
|| remotePlaylistManager == null) {
|
2018-02-09 03:53:04 +00:00
|
|
|
return;
|
2020-03-31 17:20:15 +00:00
|
|
|
}
|
2018-02-09 03:53:04 +00:00
|
|
|
|
|
|
|
final Disposable action;
|
|
|
|
|
|
|
|
if (currentInfo != null && playlistEntity == null) {
|
|
|
|
action = remotePlaylistManager.onBookmark(currentInfo)
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
2020-12-11 13:55:47 +00:00
|
|
|
.subscribe(ignored -> { /* Do nothing */ }, throwable ->
|
|
|
|
showError(new ErrorInfo(throwable, UserAction.REQUESTED_BOOKMARK,
|
|
|
|
"Adding playlist bookmark")));
|
2018-02-09 03:53:04 +00:00
|
|
|
} else if (playlistEntity != null) {
|
|
|
|
action = remotePlaylistManager.deletePlaylist(playlistEntity.getUid())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.doFinally(() -> playlistEntity = null)
|
2020-12-11 13:55:47 +00:00
|
|
|
.subscribe(ignored -> { /* Do nothing */ }, throwable ->
|
|
|
|
showError(new ErrorInfo(throwable, UserAction.REQUESTED_BOOKMARK,
|
|
|
|
"Deleting playlist bookmark")));
|
2018-02-09 03:53:04 +00:00
|
|
|
} else {
|
2020-10-31 20:55:45 +00:00
|
|
|
action = Disposable.empty();
|
2018-02-09 03:53:04 +00:00
|
|
|
}
|
2018-02-06 05:32:23 +00:00
|
|
|
|
2018-02-09 03:53:04 +00:00
|
|
|
disposables.add(action);
|
2018-02-06 05:32:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 03:53:04 +00:00
|
|
|
private void updateBookmarkButtons() {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (playlistBookmarkButton == null || activity == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-06 05:32:23 +00:00
|
|
|
|
2021-03-27 14:45:49 +00:00
|
|
|
final int drawable = playlistEntity == null
|
|
|
|
? R.drawable.ic_playlist_add : R.drawable.ic_playlist_add_check;
|
2018-02-08 23:58:48 +00:00
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
final int titleRes = playlistEntity == null
|
|
|
|
? R.string.bookmark_playlist : R.string.unbookmark_playlist;
|
2018-02-08 23:58:48 +00:00
|
|
|
|
2021-03-27 14:45:49 +00:00
|
|
|
playlistBookmarkButton.setIcon(drawable);
|
2018-02-09 03:53:04 +00:00
|
|
|
playlistBookmarkButton.setTitle(titleRes);
|
2018-02-08 23:58:48 +00:00
|
|
|
}
|
2019-12-06 15:32:45 +00:00
|
|
|
}
|