Add playlist tab to main page

Add bookmarked playlist as tab in the main page (by Settings > Content > Content of main page)
This commit is contained in:
Roy Yosef 2020-04-30 23:52:47 +03:00 committed by Stypox
parent faa6cb5c7d
commit dfc27b2480
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
7 changed files with 495 additions and 30 deletions

View File

@ -29,9 +29,8 @@ import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
import org.schabi.newpipe.report.UserAction;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.OnClickGesture;
import org.schabi.newpipe.util.PlaylistItemsUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import icepick.State;
@ -54,31 +53,6 @@ public final class BookmarkFragment extends BaseLocalListFragment<List<PlaylistL
// Fragment LifeCycle - Creation
///////////////////////////////////////////////////////////////////////////
private static List<PlaylistLocalItem> merge(
final List<PlaylistMetadataEntry> localPlaylists,
final List<PlaylistRemoteEntity> remotePlaylists) {
List<PlaylistLocalItem> items = new ArrayList<>(
localPlaylists.size() + remotePlaylists.size());
items.addAll(localPlaylists);
items.addAll(remotePlaylists);
Collections.sort(items, (left, right) -> {
String on1 = left.getOrderingName();
String on2 = right.getOrderingName();
if (on1 == null && on2 == null) {
return 0;
} else if (on1 != null && on2 == null) {
return -1;
} else if (on1 == null && on2 != null) {
return 1;
} else {
return on1.compareToIgnoreCase(on2);
}
});
return items;
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -164,7 +138,7 @@ public final class BookmarkFragment extends BaseLocalListFragment<List<PlaylistL
super.startLoading(forceLoad);
Flowable.combineLatest(localPlaylistManager.getPlaylists(),
remotePlaylistManager.getPlaylists(), BookmarkFragment::merge)
remotePlaylistManager.getPlaylists(), PlaylistItemsUtils::merge)
.onBackpressureLatest()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getPlaylistsSubscriber());

View File

@ -0,0 +1,240 @@
package org.schabi.newpipe.settings;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.schabi.newpipe.NewPipeDatabase;
import org.schabi.newpipe.R;
import org.schabi.newpipe.database.AppDatabase;
import org.schabi.newpipe.database.LocalItem;
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.local.playlist.LocalPlaylistManager;
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.report.UserAction;
import org.schabi.newpipe.util.PlaylistItemsUtils;
import java.util.List;
import java.util.Vector;
import io.reactivex.Flowable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class SelectPlaylistFragment extends DialogFragment {
/**
* This contains the base display options for images.
*/
private static final DisplayImageOptions DISPLAY_IMAGE_OPTIONS
= new DisplayImageOptions.Builder().cacheInMemory(true).build();
private final ImageLoader imageLoader = ImageLoader.getInstance();
private OnSelectedLisener onSelectedLisener = null;
private OnCancelListener onCancelListener = null;
private ProgressBar progressBar;
private TextView emptyView;
private RecyclerView recyclerView;
private List<PlaylistLocalItem> playlists = new Vector<>();
public void setOnSelectedLisener(final OnSelectedLisener listener) {
onSelectedLisener = listener;
}
public void setOnCancelListener(final OnCancelListener listener) {
onCancelListener = listener;
}
/*//////////////////////////////////////////////////////////////////////////
// Init
//////////////////////////////////////////////////////////////////////////*/
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.select_playlist_fragment, container, false);
recyclerView = v.findViewById(R.id.items_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
SelectPlaylistAdapter playlistAdapter = new SelectPlaylistAdapter();
recyclerView.setAdapter(playlistAdapter);
progressBar = v.findViewById(R.id.progressBar);
emptyView = v.findViewById(R.id.empty_state_view);
progressBar.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.GONE);
final AppDatabase database = NewPipeDatabase.getInstance(this.getContext());
LocalPlaylistManager localPlaylistManager = new LocalPlaylistManager(database);
RemotePlaylistManager remotePlaylistManager = new RemotePlaylistManager(database);
Flowable.combineLatest(localPlaylistManager.getPlaylists(),
remotePlaylistManager.getPlaylists(), PlaylistItemsUtils::merge)
.toObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getPlaylistsObserver());
return v;
}
/*//////////////////////////////////////////////////////////////////////////
// Handle actions
//////////////////////////////////////////////////////////////////////////*/
@Override
public void onCancel(final DialogInterface dialogInterface) {
super.onCancel(dialogInterface);
if (onCancelListener != null) {
onCancelListener.onCancel();
}
}
private void clickedItem(final int position) {
if (onSelectedLisener != null) {
LocalItem selectedItem = playlists.get(position);
if (selectedItem instanceof PlaylistMetadataEntry) {
final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem);
onSelectedLisener
.onLocalPlaylistSelected(entry.uid, entry.name);
} else if (selectedItem instanceof PlaylistRemoteEntity) {
final PlaylistRemoteEntity entry = ((PlaylistRemoteEntity) selectedItem);
onSelectedLisener.onRemotePlaylistSelected(
entry.getServiceId(), entry.getUrl(), entry.getName());
}
}
dismiss();
}
/*//////////////////////////////////////////////////////////////////////////
// Item handling
//////////////////////////////////////////////////////////////////////////*/
private void displayPlaylists(final List<PlaylistLocalItem> newPlaylists) {
this.playlists = newPlaylists;
progressBar.setVisibility(View.GONE);
if (newPlaylists.isEmpty()) {
emptyView.setVisibility(View.VISIBLE);
return;
}
recyclerView.setVisibility(View.VISIBLE);
}
private Observer<List<PlaylistLocalItem>> getPlaylistsObserver() {
return new Observer<List<PlaylistLocalItem>>() {
@Override
public void onSubscribe(final Disposable d) { }
@Override
public void onNext(final List<PlaylistLocalItem> newPlaylists) {
displayPlaylists(newPlaylists);
}
@Override
public void onError(final Throwable exception) {
SelectPlaylistFragment.this.onError(exception);
}
@Override
public void onComplete() { }
};
}
/*//////////////////////////////////////////////////////////////////////////
// Error
//////////////////////////////////////////////////////////////////////////*/
protected void onError(final Throwable e) {
final Activity activity = getActivity();
ErrorActivity.reportError(activity, e, activity.getClass(), null, ErrorActivity.ErrorInfo
.make(UserAction.UI_ERROR, "none", "", R.string.app_ui_crash));
}
/*//////////////////////////////////////////////////////////////////////////
// Interfaces
//////////////////////////////////////////////////////////////////////////*/
public interface OnSelectedLisener {
void onLocalPlaylistSelected(long id, String name);
void onRemotePlaylistSelected(int serviceId, String url, String name);
}
public interface OnCancelListener {
void onCancel();
}
private class SelectPlaylistAdapter
extends RecyclerView.Adapter<SelectPlaylistAdapter.SelectPlaylistItemHolder> {
@Override
public SelectPlaylistItemHolder onCreateViewHolder(final ViewGroup parent,
final int viewType) {
View item = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_playlist_mini_item, parent, false);
return new SelectPlaylistItemHolder(item);
}
@Override
public void onBindViewHolder(final SelectPlaylistItemHolder holder, final int position) {
PlaylistLocalItem selectedItem = playlists.get(position);
if (selectedItem instanceof PlaylistMetadataEntry) {
final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem);
holder.titleView.setText(entry.name);
holder.view.setOnClickListener(view -> clickedItem(position));
imageLoader.displayImage(entry.thumbnailUrl, holder.thumbnailView,
DISPLAY_IMAGE_OPTIONS);
} else if (selectedItem instanceof PlaylistRemoteEntity) {
final PlaylistRemoteEntity entry = ((PlaylistRemoteEntity) selectedItem);
holder.titleView.setText(entry.getName());
holder.view.setOnClickListener(view -> clickedItem(position));
imageLoader.displayImage(entry.getThumbnailUrl(), holder.thumbnailView,
DISPLAY_IMAGE_OPTIONS);
}
}
@Override
public int getItemCount() {
return playlists.size();
}
public class SelectPlaylistItemHolder extends RecyclerView.ViewHolder {
public final View view;
final ImageView thumbnailView;
final TextView titleView;
SelectPlaylistItemHolder(final View v) {
super(v);
this.view = v;
thumbnailView = v.findViewById(R.id.itemThumbnailView);
titleView = v.findViewById(R.id.itemTitleView);
}
}
}
}

View File

@ -34,6 +34,7 @@ import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.report.UserAction;
import org.schabi.newpipe.settings.SelectChannelFragment;
import org.schabi.newpipe.settings.SelectKioskFragment;
import org.schabi.newpipe.settings.SelectPlaylistFragment;
import org.schabi.newpipe.settings.tabs.AddTabDialog.ChooseTabListItem;
import org.schabi.newpipe.util.ThemeHelper;
@ -211,6 +212,23 @@ public class ChooseTabsFragment extends Fragment {
addTab(new Tab.ChannelTab(serviceId, url, name)));
selectChannelFragment.show(requireFragmentManager(), "select_channel");
return;
case PLAYLIST:
SelectPlaylistFragment selectPlaylistFragment = new SelectPlaylistFragment();
selectPlaylistFragment.setOnSelectedLisener(
new SelectPlaylistFragment.OnSelectedLisener() {
@Override
public void onLocalPlaylistSelected(final long id, final String name) {
addTab(new Tab.PlaylistTab(id, name));
}
@Override
public void onRemotePlaylistSelected(
final int serviceId, final String url, final String name) {
addTab(new Tab.PlaylistTab(serviceId, url, name));
}
});
selectPlaylistFragment.show(requireFragmentManager(), "select_playlist");
return;
default:
addTab(type.getTab());
break;
@ -248,6 +266,11 @@ public class ChooseTabsFragment extends Fragment {
R.attr.ic_kiosk_hot)));
}
break;
case PLAYLIST:
returnList.add(new ChooseTabListItem(tab.getTabId(),
getString(R.string.playlist_page_summary),
tab.getTabIconRes(context)));
break;
default:
if (!tabList.contains(tab)) {
returnList.add(new ChooseTabListItem(context, tab));
@ -393,6 +416,13 @@ public class ChooseTabsFragment extends Fragment {
tabName = NewPipe.getNameOfService(((Tab.ChannelTab) tab)
.getChannelServiceId()) + "/" + tab.getTabName(requireContext());
break;
case PLAYLIST:
int serviceId = ((Tab.PlaylistTab) tab).getPlaylistServiceId();
String serviceName = serviceId == -1
? getString(R.string.local)
: NewPipe.getNameOfService(serviceId);
tabName = serviceName + "/" + tab.getTabName(requireContext());
break;
default:
tabName = tab.getTabName(requireContext());
break;

View File

@ -11,6 +11,7 @@ import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonSink;
import org.schabi.newpipe.R;
import org.schabi.newpipe.database.LocalItem.LocalItemType;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
@ -18,9 +19,11 @@ import org.schabi.newpipe.fragments.BlankFragment;
import org.schabi.newpipe.fragments.list.channel.ChannelFragment;
import org.schabi.newpipe.fragments.list.kiosk.DefaultKioskFragment;
import org.schabi.newpipe.fragments.list.kiosk.KioskFragment;
import org.schabi.newpipe.fragments.list.playlist.PlaylistFragment;
import org.schabi.newpipe.local.bookmark.BookmarkFragment;
import org.schabi.newpipe.local.feed.FeedFragment;
import org.schabi.newpipe.local.history.StatisticsPlaylistFragment;
import org.schabi.newpipe.local.playlist.LocalPlaylistFragment;
import org.schabi.newpipe.local.subscription.SubscriptionFragment;
import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.report.UserAction;
@ -33,7 +36,8 @@ import java.util.Objects;
public abstract class Tab {
private static final String JSON_TAB_ID_KEY = "tab_id";
Tab() { }
Tab() {
}
Tab(@NonNull final JsonObject jsonObject) {
readDataFromJson(jsonObject);
@ -83,6 +87,8 @@ public abstract class Tab {
return new KioskTab(jsonObject);
case CHANNEL:
return new ChannelTab(jsonObject);
case PLAYLIST:
return new PlaylistTab(jsonObject);
}
}
@ -147,7 +153,8 @@ public abstract class Tab {
BOOKMARKS(new BookmarksTab()),
HISTORY(new HistoryTab()),
KIOSK(new KioskTab()),
CHANNEL(new ChannelTab());
CHANNEL(new ChannelTab()),
PLAYLIST(new PlaylistTab());
private Tab tab;
@ -482,4 +489,134 @@ public abstract class Tab {
return kioskId;
}
}
public static class PlaylistTab extends Tab {
public static final int ID = 8;
private static final String JSON_PLAYLIST_SERVICE_ID_KEY = "playlist_service_id";
private static final String JSON_PLAYLIST_URL_KEY = "playlist_url";
private static final String JSON_PLAYLIST_NAME_KEY = "playlist_name";
private static final String JSON_PLAYLIST_ID_KEY = "playlist_id";
private static final String JSON_PLAYLIST_TYPE_KEY = "playlist_type";
private int playlistServiceId;
private String playlistUrl;
private String playlistName;
private long playlistId;
private LocalItemType playlistType;
private PlaylistTab() {
this.playlistName = "<no-name>";
this.playlistId = -1;
this.playlistType = LocalItemType.PLAYLIST_LOCAL_ITEM;
this.playlistServiceId = -1;
this.playlistUrl = "<no-url>";
}
public PlaylistTab(final long playlistId, final String playlistName) {
this.playlistName = playlistName;
this.playlistId = playlistId;
this.playlistType = LocalItemType.PLAYLIST_LOCAL_ITEM;
this.playlistServiceId = -1;
this.playlistUrl = "<no-url>";
}
public PlaylistTab(final int playlistServiceId, final String playlistUrl,
final String playlistName) {
this.playlistServiceId = playlistServiceId;
this.playlistUrl = playlistUrl;
this.playlistName = playlistName;
this.playlistType = LocalItemType.PLAYLIST_REMOTE_ITEM;
this.playlistId = -1;
}
public PlaylistTab(final JsonObject jsonObject) {
super(jsonObject);
}
@Override
public int getTabId() {
return ID;
}
@Override
public String getTabName(final Context context) {
return playlistName;
}
@DrawableRes
@Override
public int getTabIconRes(final Context context) {
return ThemeHelper.resolveResourceIdFromAttr(context, R.attr.ic_list);
}
@Override
public Fragment getFragment(final Context context) {
if (playlistType == LocalItemType.PLAYLIST_LOCAL_ITEM) {
return LocalPlaylistFragment.getInstance(playlistId,
playlistName == null ? "" : playlistName);
} else { // playlistType == LocalItemType.PLAYLIST_REMOTE_ITEM
return PlaylistFragment.getInstance(playlistServiceId, playlistUrl,
playlistName == null ? "" : playlistName);
}
}
@Override
protected void writeDataToJson(final JsonSink writerSink) {
writerSink.value(JSON_PLAYLIST_SERVICE_ID_KEY, playlistServiceId)
.value(JSON_PLAYLIST_URL_KEY, playlistUrl)
.value(JSON_PLAYLIST_NAME_KEY, playlistName)
.value(JSON_PLAYLIST_ID_KEY, playlistId)
.value(JSON_PLAYLIST_TYPE_KEY, playlistType.toString());
}
@Override
protected void readDataFromJson(final JsonObject jsonObject) {
playlistServiceId = jsonObject.getInt(JSON_PLAYLIST_SERVICE_ID_KEY, -1);
playlistUrl = jsonObject.getString(JSON_PLAYLIST_URL_KEY, "<no-url>");
playlistName = jsonObject.getString(JSON_PLAYLIST_NAME_KEY, "<no-name>");
playlistId = jsonObject.getInt(JSON_PLAYLIST_ID_KEY, -1);
playlistType = LocalItemType.valueOf(
jsonObject.getString(JSON_PLAYLIST_TYPE_KEY,
LocalItemType.PLAYLIST_LOCAL_ITEM.toString())
);
}
@Override
public boolean equals(final Object obj) {
boolean baseEqual = super.equals(obj)
&& Objects.equals(playlistType, ((PlaylistTab) obj).playlistType)
&& Objects.equals(playlistName, ((PlaylistTab) obj).playlistName);
if (!baseEqual) {
return false;
}
boolean localPlaylistEquals = playlistId == ((PlaylistTab) obj).playlistId;
boolean remotePlaylistEquals =
playlistServiceId == ((PlaylistTab) obj).playlistServiceId
&& Objects.equals(playlistUrl, ((PlaylistTab) obj).playlistUrl);
return localPlaylistEquals || remotePlaylistEquals;
}
public int getPlaylistServiceId() {
return playlistServiceId;
}
public String getPlaylistUrl() {
return playlistUrl;
}
public String getPlaylistName() {
return playlistName;
}
public long getPlaylistId() {
return playlistId;
}
public LocalItemType getPlaylistType() {
return playlistType;
}
}
}

View File

@ -0,0 +1,38 @@
package org.schabi.newpipe.util;
import org.schabi.newpipe.database.playlist.PlaylistLocalItem;
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public final class PlaylistItemsUtils {
private PlaylistItemsUtils() { }
public static List<PlaylistLocalItem> merge(
final List<PlaylistMetadataEntry> localPlaylists,
final List<PlaylistRemoteEntity> remotePlaylists) {
List<PlaylistLocalItem> items = new ArrayList<>(
localPlaylists.size() + remotePlaylists.size());
items.addAll(localPlaylists);
items.addAll(remotePlaylists);
Collections.sort(items, (left, right) -> {
String on1 = left.getOrderingName();
String on2 = right.getOrderingName();
if (on1 == null && on2 == null) {
return 0;
} else if (on1 != null && on2 == null) {
return -1;
} else if (on1 == null && on2 != null) {
return 1;
} else {
return on1.compareToIgnoreCase(on2);
}
});
return items;
}
}

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="13dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
android:text="@string/select_a_playlist"
android:textAppearance="?android:attr/textAppearanceLarge" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/items_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/list_playlist_mini_item"></androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="@+id/empty_state_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/no_playlist_bookmarked_yet"
android:textAppearance="?android:attr/textAppearanceListItem" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
</LinearLayout>

View File

@ -399,6 +399,8 @@
<string name="channel_page_summary">Channel Page</string>
<string name="select_a_channel">Select a channel</string>
<string name="no_channel_subscribed_yet">No channel subscriptions yet</string>
<string name="select_a_playlist">Select a playlist</string>
<string name="no_playlist_bookmarked_yet">No playlists bookmarks yet</string>
<string name="select_a_kiosk">Select a kiosk</string>
<string name="export_complete_toast">Exported</string>
<string name="import_complete_toast">Imported</string>
@ -651,4 +653,5 @@
<string name="detail_sub_channel_thumbnail_view_description">Channel\'s avatar thumbnail</string>
<string name="channel_created_by">Created by %s</string>
<string name="video_detail_by">By %s</string>
<string name="playlist_page_summary">Playlist Page</string>
</resources>