2018-01-28 07:14:38 +01:00
|
|
|
package org.schabi.newpipe.fragments.local.bookmark;
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2018-01-28 07:14:38 +01:00
|
|
|
import android.app.AlertDialog;
|
2018-01-17 06:12:03 +01:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Parcelable;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2018-01-31 01:01:11 +01:00
|
|
|
import android.util.Log;
|
2018-01-17 06:12:03 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import org.reactivestreams.Subscriber;
|
|
|
|
import org.reactivestreams.Subscription;
|
|
|
|
import org.schabi.newpipe.NewPipeDatabase;
|
|
|
|
import org.schabi.newpipe.R;
|
2018-01-28 07:14:38 +01:00
|
|
|
import org.schabi.newpipe.database.LocalItem;
|
2018-01-17 06:12:03 +01:00
|
|
|
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
|
2018-01-31 01:01:11 +01:00
|
|
|
import org.schabi.newpipe.fragments.local.BaseLocalListFragment;
|
2018-01-28 07:14:38 +01:00
|
|
|
import org.schabi.newpipe.fragments.local.LocalPlaylistManager;
|
2018-01-17 06:12:03 +01:00
|
|
|
import org.schabi.newpipe.report.UserAction;
|
|
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
2018-01-31 01:21:50 +01:00
|
|
|
import org.schabi.newpipe.util.OnClickGesture;
|
2018-01-17 06:12:03 +01:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import icepick.State;
|
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
2018-01-31 01:01:11 +01:00
|
|
|
import io.reactivex.disposables.Disposable;
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
public final class BookmarkFragment
|
|
|
|
extends BaseLocalListFragment<List<PlaylistMetadataEntry>, Void> {
|
2018-01-17 06:12:03 +01:00
|
|
|
|
|
|
|
private View watchHistoryButton;
|
|
|
|
private View mostWatchedButton;
|
|
|
|
|
|
|
|
@State
|
|
|
|
protected Parcelable itemsListState;
|
|
|
|
|
|
|
|
private Subscription databaseSubscription;
|
|
|
|
private CompositeDisposable disposables = new CompositeDisposable();
|
|
|
|
private LocalPlaylistManager localPlaylistManager;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-01-31 01:01:11 +01:00
|
|
|
// Fragment LifeCycle - Creation
|
2018-01-17 06:12:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@Override
|
2018-01-31 01:01:11 +01:00
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
localPlaylistManager = new LocalPlaylistManager(NewPipeDatabase.getInstance(getContext()));
|
|
|
|
disposables = new CompositeDisposable();
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
|
|
|
@Nullable ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
2018-01-31 01:01:11 +01:00
|
|
|
if (activity != null && activity.getSupportActionBar() != null) {
|
2018-01-17 06:12:03 +01:00
|
|
|
activity.getSupportActionBar().setDisplayShowTitleEnabled(true);
|
2018-01-31 01:01:11 +01:00
|
|
|
activity.setTitle(R.string.tab_subscriptions);
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return inflater.inflate(R.layout.fragment_bookmarks, container, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2018-01-31 01:01:11 +01:00
|
|
|
public void setUserVisibleHint(boolean isVisibleToUser) {
|
|
|
|
super.setUserVisibleHint(isVisibleToUser);
|
|
|
|
if (isVisibleToUser) setTitle(getString(R.string.tab_bookmarks));
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-01-31 01:01:11 +01:00
|
|
|
// Fragment LifeCycle - Views
|
2018-01-17 06:12:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initViews(View rootView, Bundle savedInstanceState) {
|
|
|
|
super.initViews(rootView, savedInstanceState);
|
2018-01-31 01:01:11 +01:00
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
@Override
|
|
|
|
protected View getListHeader() {
|
2018-01-17 06:12:03 +01:00
|
|
|
final View headerRootLayout = activity.getLayoutInflater()
|
|
|
|
.inflate(R.layout.bookmark_header, itemsList, false);
|
|
|
|
watchHistoryButton = headerRootLayout.findViewById(R.id.watchHistory);
|
|
|
|
mostWatchedButton = headerRootLayout.findViewById(R.id.mostWatched);
|
2018-01-31 01:01:11 +01:00
|
|
|
return headerRootLayout;
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initListeners() {
|
|
|
|
super.initListeners();
|
|
|
|
|
2018-01-31 01:21:50 +01:00
|
|
|
itemListAdapter.setSelectedListener(new OnClickGesture<LocalItem>() {
|
2018-01-17 06:12:03 +01:00
|
|
|
@Override
|
2018-01-28 07:14:38 +01:00
|
|
|
public void selected(LocalItem selectedItem) {
|
2018-01-17 06:12:03 +01:00
|
|
|
// Requires the parent fragment to find holder for fragment replacement
|
2018-01-28 07:14:38 +01:00
|
|
|
if (selectedItem instanceof PlaylistMetadataEntry && getParentFragment() != null) {
|
|
|
|
final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem);
|
2018-01-17 06:12:03 +01:00
|
|
|
NavigationHelper.openLocalPlaylistFragment(
|
2018-01-28 07:14:38 +01:00
|
|
|
getParentFragment().getFragmentManager(), entry.uid, entry.name);
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-01-28 07:14:38 +01:00
|
|
|
public void held(LocalItem selectedItem) {
|
|
|
|
if (selectedItem instanceof PlaylistMetadataEntry) {
|
|
|
|
showDeleteDialog((PlaylistMetadataEntry) selectedItem);
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
watchHistoryButton.setOnClickListener(view -> {
|
|
|
|
if (getParentFragment() != null) {
|
|
|
|
NavigationHelper.openWatchHistoryFragment(getParentFragment().getFragmentManager());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mostWatchedButton.setOnClickListener(view -> {
|
|
|
|
if (getParentFragment() != null) {
|
|
|
|
NavigationHelper.openMostPlayedFragment(getParentFragment().getFragmentManager());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-01-31 01:01:11 +01:00
|
|
|
// Fragment LifeCycle - Loading
|
2018-01-17 06:12:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startLoading(boolean forceLoad) {
|
|
|
|
super.startLoading(forceLoad);
|
|
|
|
localPlaylistManager.getPlaylists()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(getSubscriptionSubscriber());
|
|
|
|
}
|
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Fragment LifeCycle - Destruction
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
itemsListState = itemsList.getLayoutManager().onSaveInstanceState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
super.onDestroyView();
|
|
|
|
if (disposables != null) disposables.clear();
|
|
|
|
if (databaseSubscription != null) databaseSubscription.cancel();
|
|
|
|
|
|
|
|
databaseSubscription = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
if (disposables != null) disposables.dispose();
|
|
|
|
|
|
|
|
disposables = null;
|
|
|
|
localPlaylistManager = null;
|
|
|
|
itemsListState = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Subscriptions Loader
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-01-17 06:12:03 +01:00
|
|
|
private Subscriber<List<PlaylistMetadataEntry>> getSubscriptionSubscriber() {
|
|
|
|
return new Subscriber<List<PlaylistMetadataEntry>>() {
|
|
|
|
@Override
|
|
|
|
public void onSubscribe(Subscription s) {
|
|
|
|
showLoading();
|
|
|
|
if (databaseSubscription != null) databaseSubscription.cancel();
|
|
|
|
databaseSubscription = s;
|
|
|
|
databaseSubscription.request(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNext(List<PlaylistMetadataEntry> subscriptions) {
|
|
|
|
handleResult(subscriptions);
|
|
|
|
if (databaseSubscription != null) databaseSubscription.request(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(Throwable exception) {
|
|
|
|
BookmarkFragment.this.onError(exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onComplete() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleResult(@NonNull List<PlaylistMetadataEntry> result) {
|
|
|
|
super.handleResult(result);
|
|
|
|
|
2018-01-28 07:14:38 +01:00
|
|
|
itemListAdapter.clearStreamItemList();
|
2018-01-17 06:12:03 +01:00
|
|
|
|
|
|
|
if (result.isEmpty()) {
|
|
|
|
showEmptyState();
|
2018-01-31 01:01:11 +01:00
|
|
|
return;
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
itemListAdapter.addItems(result);
|
|
|
|
if (itemsListState != null) {
|
|
|
|
itemsList.getLayoutManager().onRestoreInstanceState(itemsListState);
|
|
|
|
itemsListState = null;
|
|
|
|
}
|
|
|
|
hideLoading();
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
2018-01-31 01:01:11 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Fragment Error Handling
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-01-17 06:12:03 +01:00
|
|
|
|
|
|
|
@Override
|
2018-01-31 01:01:11 +01:00
|
|
|
protected boolean onError(Throwable exception) {
|
|
|
|
if (super.onError(exception)) return true;
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
onUnrecoverableError(exception, UserAction.SOMETHING_ELSE,
|
|
|
|
"none", "Bookmark", R.string.general_error);
|
|
|
|
return true;
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-01-31 01:01:11 +01:00
|
|
|
protected void resetFragment() {
|
|
|
|
super.resetFragment();
|
|
|
|
if (disposables != null) disposables.clear();
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-01-31 01:01:11 +01:00
|
|
|
// Utils
|
2018-01-17 06:12:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
private void showDeleteDialog(final PlaylistMetadataEntry item) {
|
|
|
|
new AlertDialog.Builder(activity)
|
|
|
|
.setTitle(item.name)
|
|
|
|
.setMessage(R.string.delete_playlist_prompt)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setPositiveButton(R.string.delete, (dialog, i) ->
|
|
|
|
disposables.add(deletePlaylist(item.uid))
|
|
|
|
)
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.show();
|
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
private Disposable deletePlaylist(final long playlistId) {
|
|
|
|
return localPlaylistManager.deletePlaylist(playlistId)
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(ignored -> {/*Do nothing on success*/},
|
|
|
|
throwable -> Log.e(TAG, "Playlist deletion failed, id=["
|
|
|
|
+ playlistId + "]")
|
|
|
|
);
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|