2018-04-21 23:10:01 +02:00
|
|
|
package org.schabi.newpipe.local.history;
|
2018-01-17 06:12:03 +01:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Parcelable;
|
|
|
|
import android.view.LayoutInflater;
|
2018-12-29 19:01:45 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2018-01-17 06:12:03 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2018-04-28 15:37:27 +02:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2018-04-29 13:15:52 +02:00
|
|
|
import android.widget.Toast;
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
|
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
|
|
|
|
2018-01-17 06:12:03 +01:00
|
|
|
import org.reactivestreams.Subscriber;
|
|
|
|
import org.reactivestreams.Subscription;
|
|
|
|
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.stream.StreamStatisticsEntry;
|
2020-10-18 20:19:50 +02:00
|
|
|
import org.schabi.newpipe.database.stream.model.StreamEntity;
|
2018-01-17 06:12:03 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
2019-07-22 11:58:01 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamType;
|
2018-01-17 06:12:03 +01:00
|
|
|
import org.schabi.newpipe.info_list.InfoItemDialog;
|
2018-12-29 19:01:45 +01:00
|
|
|
import org.schabi.newpipe.local.BaseLocalListFragment;
|
2020-10-06 14:38:48 +02:00
|
|
|
import org.schabi.newpipe.player.helper.PlayerHolder;
|
2018-04-21 23:10:01 +02:00
|
|
|
import org.schabi.newpipe.player.playqueue.PlayQueue;
|
|
|
|
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
|
2018-12-29 19:01:45 +01:00
|
|
|
import org.schabi.newpipe.report.ErrorActivity;
|
2020-10-04 03:50:28 +02:00
|
|
|
import org.schabi.newpipe.report.ErrorInfo;
|
2018-01-17 06:12:03 +01:00
|
|
|
import org.schabi.newpipe.report.UserAction;
|
2018-12-29 19:01:45 +01:00
|
|
|
import org.schabi.newpipe.settings.SettingsActivity;
|
2018-01-17 06:12:03 +01:00
|
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
2018-01-31 01:21:50 +01:00
|
|
|
import org.schabi.newpipe.util.OnClickGesture;
|
2019-07-25 00:44:12 +02:00
|
|
|
import org.schabi.newpipe.util.StreamDialogEntry;
|
2018-04-29 13:15:52 +02:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
2018-01-17 06:12:03 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2020-10-06 14:38:48 +02:00
|
|
|
import java.util.Arrays;
|
2018-02-03 18:36:40 +01:00
|
|
|
import java.util.Collections;
|
2020-06-25 13:37:13 +02:00
|
|
|
import java.util.Comparator;
|
2018-01-17 06:12:03 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import icepick.State;
|
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
2018-04-24 22:02:23 +02:00
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
import io.reactivex.disposables.Disposable;
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2018-04-28 15:37:27 +02:00
|
|
|
public class StatisticsPlaylistFragment
|
2018-01-28 07:14:38 +01:00
|
|
|
extends BaseLocalListFragment<List<StreamStatisticsEntry>, Void> {
|
2020-03-31 19:20:15 +02:00
|
|
|
private final CompositeDisposable disposables = new CompositeDisposable();
|
|
|
|
@State
|
|
|
|
Parcelable itemsListState;
|
|
|
|
private StatisticSortMode sortMode = StatisticSortMode.LAST_PLAYED;
|
2018-01-17 06:12:03 +01:00
|
|
|
private View headerPlayAllButton;
|
|
|
|
private View headerPopupButton;
|
|
|
|
private View headerBackgroundButton;
|
2018-04-28 15:37:27 +02:00
|
|
|
private View playlistCtrl;
|
|
|
|
private View sortButton;
|
|
|
|
private ImageView sortButtonIcon;
|
|
|
|
private TextView sortButtonText;
|
2018-01-17 06:12:03 +01:00
|
|
|
/* Used for independent events */
|
|
|
|
private Subscription databaseSubscription;
|
2018-01-27 06:34:17 +01:00
|
|
|
private HistoryRecordManager recordManager;
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
private List<StreamStatisticsEntry> processResult(final List<StreamStatisticsEntry> results) {
|
2020-06-25 13:37:13 +02:00
|
|
|
final Comparator<StreamStatisticsEntry> comparator;
|
2018-04-28 15:37:27 +02:00
|
|
|
switch (sortMode) {
|
|
|
|
case LAST_PLAYED:
|
2020-06-25 13:37:13 +02:00
|
|
|
comparator = Comparator.comparing(StreamStatisticsEntry::getLatestAccessDate);
|
|
|
|
break;
|
2018-04-28 15:37:27 +02:00
|
|
|
case MOST_PLAYED:
|
2020-06-25 13:37:13 +02:00
|
|
|
comparator = Comparator.comparingLong(StreamStatisticsEntry::getWatchCount);
|
|
|
|
break;
|
2020-03-31 19:20:15 +02:00
|
|
|
default:
|
|
|
|
return null;
|
2018-04-28 15:37:27 +02:00
|
|
|
}
|
2020-06-25 13:37:13 +02:00
|
|
|
Collections.sort(results, comparator.reversed());
|
|
|
|
return results;
|
2018-04-28 15:37:27 +02:00
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-01-31 01:01:11 +01:00
|
|
|
// Fragment LifeCycle - Creation
|
2018-01-17 06:12:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void onCreate(final Bundle savedInstanceState) {
|
2018-01-31 01:01:11 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
recordManager = new HistoryRecordManager(getContext());
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public View onCreateView(@NonNull final LayoutInflater inflater,
|
|
|
|
@Nullable final ViewGroup container,
|
|
|
|
@Nullable final Bundle savedInstanceState) {
|
2018-01-17 06:12:03 +01:00
|
|
|
return inflater.inflate(R.layout.fragment_playlist, container, false);
|
|
|
|
}
|
|
|
|
|
2018-06-10 21:57:35 +02:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void setUserVisibleHint(final boolean isVisibleToUser) {
|
2018-06-10 21:57:35 +02:00
|
|
|
super.setUserVisibleHint(isVisibleToUser);
|
|
|
|
if (activity != null && isVisibleToUser) {
|
|
|
|
setTitle(activity.getString(R.string.title_activity_history));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:01:45 +01:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
|
2018-12-29 19:01:45 +01:00
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
inflater.inflate(R.menu.menu_history, menu);
|
|
|
|
}
|
|
|
|
|
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
|
2020-03-31 19:20:15 +02:00
|
|
|
protected void initViews(final View rootView, final Bundle savedInstanceState) {
|
2018-01-17 06:12:03 +01:00
|
|
|
super.initViews(rootView, savedInstanceState);
|
2020-03-31 19:20:15 +02:00
|
|
|
if (!useAsFrontPage) {
|
2018-06-11 11:01:18 +02:00
|
|
|
setTitle(getString(R.string.title_last_played));
|
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected View getListHeader() {
|
2020-03-31 19:20:15 +02:00
|
|
|
final View headerRootLayout = activity.getLayoutInflater()
|
|
|
|
.inflate(R.layout.statistic_playlist_control, itemsList, false);
|
2018-04-28 15:37:27 +02:00
|
|
|
playlistCtrl = headerRootLayout.findViewById(R.id.playlist_control);
|
2018-01-17 06:12:03 +01:00
|
|
|
headerPlayAllButton = headerRootLayout.findViewById(R.id.playlist_ctrl_play_all_button);
|
|
|
|
headerPopupButton = headerRootLayout.findViewById(R.id.playlist_ctrl_play_popup_button);
|
|
|
|
headerBackgroundButton = headerRootLayout.findViewById(R.id.playlist_ctrl_play_bg_button);
|
2018-04-28 15:37:27 +02:00
|
|
|
sortButton = headerRootLayout.findViewById(R.id.sortButton);
|
|
|
|
sortButtonIcon = headerRootLayout.findViewById(R.id.sortButtonIcon);
|
|
|
|
sortButtonText = headerRootLayout.findViewById(R.id.sortButtonText);
|
2018-01-17 06:12:03 +01:00
|
|
|
return headerRootLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
2020-03-31 19:20:15 +02:00
|
|
|
public void selected(final LocalItem selectedItem) {
|
2018-01-28 07:14:38 +01:00
|
|
|
if (selectedItem instanceof StreamStatisticsEntry) {
|
2020-10-18 20:19:50 +02:00
|
|
|
final StreamEntity item =
|
|
|
|
((StreamStatisticsEntry) selectedItem).getStreamEntity();
|
|
|
|
NavigationHelper.openVideoDetailFragment(requireContext(), getFM(),
|
|
|
|
item.getServiceId(), item.getUrl(), item.getTitle(), null, false);
|
2018-01-28 07:14:38 +01:00
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void held(final LocalItem selectedItem) {
|
2018-01-28 07:14:38 +01:00
|
|
|
if (selectedItem instanceof StreamStatisticsEntry) {
|
|
|
|
showStreamDialog((StreamStatisticsEntry) selectedItem);
|
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:01:45 +01:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
2018-12-29 19:01:45 +01:00
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_history_clear:
|
|
|
|
new AlertDialog.Builder(activity)
|
|
|
|
.setTitle(R.string.delete_view_history_alert)
|
|
|
|
.setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.dismiss()))
|
|
|
|
.setPositiveButton(R.string.delete, ((dialog, which) -> {
|
|
|
|
final Disposable onDelete = recordManager.deleteWholeStreamHistory()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(
|
|
|
|
howManyDeleted -> Toast.makeText(getContext(),
|
2019-08-07 14:34:49 +02:00
|
|
|
R.string.watch_history_deleted,
|
2018-12-29 19:01:45 +01:00
|
|
|
Toast.LENGTH_SHORT).show(),
|
|
|
|
throwable -> ErrorActivity.reportError(getContext(),
|
|
|
|
throwable,
|
|
|
|
SettingsActivity.class, null,
|
2020-10-04 03:50:28 +02:00
|
|
|
ErrorInfo.make(
|
2018-12-29 19:01:45 +01:00
|
|
|
UserAction.DELETE_FROM_HISTORY,
|
|
|
|
"none",
|
|
|
|
"Delete view history",
|
|
|
|
R.string.general_error)));
|
|
|
|
|
|
|
|
final Disposable onClearOrphans = recordManager.removeOrphanedRecords()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(
|
2020-03-31 19:20:15 +02:00
|
|
|
howManyDeleted -> {
|
|
|
|
},
|
2018-12-29 19:01:45 +01:00
|
|
|
throwable -> ErrorActivity.reportError(getContext(),
|
|
|
|
throwable,
|
|
|
|
SettingsActivity.class, null,
|
2020-10-04 03:50:28 +02:00
|
|
|
ErrorInfo.make(
|
2018-12-29 19:01:45 +01:00
|
|
|
UserAction.DELETE_FROM_HISTORY,
|
|
|
|
"none",
|
|
|
|
"Delete search history",
|
|
|
|
R.string.general_error)));
|
|
|
|
disposables.add(onClearOrphans);
|
|
|
|
disposables.add(onDelete);
|
|
|
|
}))
|
|
|
|
.create()
|
|
|
|
.show();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Fragment LifeCycle - Loading
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void startLoading(final boolean forceLoad) {
|
2018-01-31 01:01:11 +01:00
|
|
|
super.startLoading(forceLoad);
|
|
|
|
recordManager.getStreamStatistics()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(getHistoryObserver());
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-01-31 01:01:11 +01:00
|
|
|
// Fragment LifeCycle - Destruction
|
2018-01-17 06:12:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@Override
|
2018-01-31 01:01:11 +01:00
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
itemsListState = itemsList.getLayoutManager().onSaveInstanceState();
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-01-31 01:01:11 +01:00
|
|
|
public void onDestroyView() {
|
|
|
|
super.onDestroyView();
|
2018-02-07 23:20:16 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
if (itemListAdapter != null) {
|
|
|
|
itemListAdapter.unsetSelectedListener();
|
|
|
|
}
|
|
|
|
if (headerBackgroundButton != null) {
|
|
|
|
headerBackgroundButton.setOnClickListener(null);
|
|
|
|
}
|
|
|
|
if (headerPlayAllButton != null) {
|
|
|
|
headerPlayAllButton.setOnClickListener(null);
|
|
|
|
}
|
|
|
|
if (headerPopupButton != null) {
|
|
|
|
headerPopupButton.setOnClickListener(null);
|
|
|
|
}
|
2018-02-07 23:20:16 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
if (databaseSubscription != null) {
|
|
|
|
databaseSubscription.cancel();
|
|
|
|
}
|
2018-01-31 01:01:11 +01:00
|
|
|
databaseSubscription = null;
|
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
recordManager = null;
|
|
|
|
itemsListState = null;
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Statistics Loader
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-01-17 06:12:03 +01:00
|
|
|
private Subscriber<List<StreamStatisticsEntry>> getHistoryObserver() {
|
|
|
|
return new Subscriber<List<StreamStatisticsEntry>>() {
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void onSubscribe(final Subscription s) {
|
2018-01-17 06:12:03 +01:00
|
|
|
showLoading();
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
if (databaseSubscription != null) {
|
|
|
|
databaseSubscription.cancel();
|
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
databaseSubscription = s;
|
|
|
|
databaseSubscription.request(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void onNext(final List<StreamStatisticsEntry> streams) {
|
2018-01-17 06:12:03 +01:00
|
|
|
handleResult(streams);
|
2020-03-31 19:20:15 +02:00
|
|
|
if (databaseSubscription != null) {
|
|
|
|
databaseSubscription.request(1);
|
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void onError(final Throwable exception) {
|
2018-01-17 23:32:09 +01:00
|
|
|
StatisticsPlaylistFragment.this.onError(exception);
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onComplete() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void handleResult(@NonNull final List<StreamStatisticsEntry> result) {
|
2018-01-17 06:12:03 +01:00
|
|
|
super.handleResult(result);
|
2020-03-31 19:20:15 +02:00
|
|
|
if (itemListAdapter == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-03 18:36:40 +01:00
|
|
|
|
2018-04-28 15:37:27 +02:00
|
|
|
playlistCtrl.setVisibility(View.VISIBLE);
|
|
|
|
|
2018-01-28 07:14:38 +01:00
|
|
|
itemListAdapter.clearStreamItemList();
|
2018-01-17 06:12:03 +01:00
|
|
|
|
|
|
|
if (result.isEmpty()) {
|
|
|
|
showEmptyState();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:06:48 +01:00
|
|
|
itemListAdapter.addItems(processResult(result));
|
2018-01-17 06:12:03 +01:00
|
|
|
if (itemsListState != null) {
|
|
|
|
itemsList.getLayoutManager().onRestoreInstanceState(itemsListState);
|
|
|
|
itemsListState = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
headerPlayAllButton.setOnClickListener(view ->
|
2020-10-18 20:19:50 +02:00
|
|
|
NavigationHelper.playOnMainPlayer(activity, getPlayQueue()));
|
2018-01-17 06:12:03 +01:00
|
|
|
headerPopupButton.setOnClickListener(view ->
|
2019-04-13 09:31:32 +02:00
|
|
|
NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(), false));
|
2018-01-17 06:12:03 +01:00
|
|
|
headerBackgroundButton.setOnClickListener(view ->
|
2019-04-13 09:31:32 +02:00
|
|
|
NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue(), false));
|
2018-04-29 13:15:52 +02:00
|
|
|
sortButton.setOnClickListener(view -> toggleSortMode());
|
2018-01-31 01:01:11 +01:00
|
|
|
|
2018-01-17 06:12:03 +01:00
|
|
|
hideLoading();
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Fragment Error Handling
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
@Override
|
|
|
|
protected void resetFragment() {
|
|
|
|
super.resetFragment();
|
2020-03-31 19:20:15 +02:00
|
|
|
if (databaseSubscription != null) {
|
|
|
|
databaseSubscription.cancel();
|
|
|
|
}
|
2018-01-31 01:01:11 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 06:12:03 +01:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
protected boolean onError(final Throwable exception) {
|
|
|
|
if (super.onError(exception)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
|
|
|
|
onUnrecoverableError(exception, UserAction.SOMETHING_ELSE,
|
2018-01-29 08:01:06 +01:00
|
|
|
"none", "History Statistics", R.string.general_error);
|
2018-01-17 06:12:03 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2018-04-29 13:15:52 +02:00
|
|
|
private void toggleSortMode() {
|
2020-03-31 19:20:15 +02:00
|
|
|
if (sortMode == StatisticSortMode.LAST_PLAYED) {
|
2018-04-28 15:37:27 +02:00
|
|
|
sortMode = StatisticSortMode.MOST_PLAYED;
|
|
|
|
setTitle(getString(R.string.title_most_played));
|
2020-03-25 16:23:47 +01:00
|
|
|
sortButtonIcon.setImageResource(
|
|
|
|
ThemeHelper.resolveResourceIdFromAttr(requireContext(), R.attr.ic_history));
|
2018-04-28 15:37:27 +02:00
|
|
|
sortButtonText.setText(R.string.title_last_played);
|
|
|
|
} else {
|
|
|
|
sortMode = StatisticSortMode.LAST_PLAYED;
|
|
|
|
setTitle(getString(R.string.title_last_played));
|
2020-03-25 16:23:47 +01:00
|
|
|
sortButtonIcon.setImageResource(
|
|
|
|
ThemeHelper.resolveResourceIdFromAttr(requireContext(), R.attr.ic_filter_list));
|
2018-04-28 15:37:27 +02:00
|
|
|
sortButtonText.setText(R.string.title_most_played);
|
|
|
|
}
|
|
|
|
startLoading(true);
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
private PlayQueue getPlayQueueStartingAt(final StreamStatisticsEntry infoItem) {
|
2019-08-14 14:17:05 +02:00
|
|
|
return getPlayQueue(Math.max(itemListAdapter.getItemsList().indexOf(infoItem), 0));
|
|
|
|
}
|
|
|
|
|
2018-01-31 01:01:11 +01:00
|
|
|
private void showStreamDialog(final StreamStatisticsEntry item) {
|
|
|
|
final Context context = getContext();
|
|
|
|
final Activity activity = getActivity();
|
2020-03-31 19:20:15 +02:00
|
|
|
if (context == null || context.getResources() == null || activity == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-31 01:01:11 +01:00
|
|
|
final StreamInfoItem infoItem = item.toStreamInfoItem();
|
2019-07-25 00:44:12 +02:00
|
|
|
|
2020-10-06 14:38:48 +02:00
|
|
|
final ArrayList<StreamDialogEntry> entries = new ArrayList<>();
|
|
|
|
|
|
|
|
if (PlayerHolder.getType() != null) {
|
2020-10-06 17:22:12 +02:00
|
|
|
entries.add(StreamDialogEntry.enqueue);
|
2020-10-06 14:38:48 +02:00
|
|
|
}
|
2019-07-25 00:44:12 +02:00
|
|
|
if (infoItem.getStreamType() == StreamType.AUDIO_STREAM) {
|
2020-10-06 14:38:48 +02:00
|
|
|
entries.addAll(Arrays.asList(
|
2019-07-25 00:44:12 +02:00
|
|
|
StreamDialogEntry.start_here_on_background,
|
|
|
|
StreamDialogEntry.delete,
|
|
|
|
StreamDialogEntry.append_playlist,
|
2020-10-06 14:38:48 +02:00
|
|
|
StreamDialogEntry.share
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
entries.addAll(Arrays.asList(
|
2019-07-25 00:44:12 +02:00
|
|
|
StreamDialogEntry.start_here_on_background,
|
|
|
|
StreamDialogEntry.start_here_on_popup,
|
|
|
|
StreamDialogEntry.delete,
|
|
|
|
StreamDialogEntry.append_playlist,
|
2020-10-06 14:38:48 +02:00
|
|
|
StreamDialogEntry.share
|
|
|
|
));
|
2019-07-22 11:58:01 +02:00
|
|
|
}
|
2020-10-06 14:38:48 +02:00
|
|
|
StreamDialogEntry.setEnabledEntries(entries);
|
2018-01-31 01:01:11 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
StreamDialogEntry.start_here_on_background.setCustomAction((fragment, infoItemDuplicate) ->
|
|
|
|
NavigationHelper
|
|
|
|
.playOnBackgroundPlayer(context, getPlayQueueStartingAt(item), true));
|
2019-07-25 00:53:13 +02:00
|
|
|
StreamDialogEntry.delete.setCustomAction((fragment, infoItemDuplicate) ->
|
2020-03-31 19:20:15 +02:00
|
|
|
deleteEntry(Math.max(itemListAdapter.getItemsList().indexOf(item), 0)));
|
2018-01-31 01:01:11 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
new InfoItemDialog(activity, infoItem, StreamDialogEntry.getCommands(context),
|
|
|
|
(dialog, which) -> StreamDialogEntry.clickOn(which, this, infoItem)).show();
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:02:36 +02:00
|
|
|
private void deleteEntry(final int index) {
|
2020-10-05 17:45:05 +02:00
|
|
|
final LocalItem infoItem = itemListAdapter.getItemsList().get(index);
|
2020-03-31 19:20:15 +02:00
|
|
|
if (infoItem instanceof StreamStatisticsEntry) {
|
2018-04-23 22:02:36 +02:00
|
|
|
final StreamStatisticsEntry entry = (StreamStatisticsEntry) infoItem;
|
2020-10-05 17:45:05 +02:00
|
|
|
final Disposable onDelete = recordManager
|
|
|
|
.deleteStreamHistoryAndState(entry.getStreamId())
|
2018-04-24 22:02:23 +02:00
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(
|
2020-10-05 17:45:05 +02:00
|
|
|
() -> {
|
2020-03-31 19:20:15 +02:00
|
|
|
if (getView() != null) {
|
2018-04-29 13:15:52 +02:00
|
|
|
Snackbar.make(getView(), R.string.one_item_deleted,
|
|
|
|
Snackbar.LENGTH_SHORT).show();
|
|
|
|
} else {
|
|
|
|
Toast.makeText(getContext(),
|
|
|
|
R.string.one_item_deleted,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
},
|
2018-04-24 22:02:23 +02:00
|
|
|
throwable -> showSnackBarError(throwable,
|
2018-04-28 16:11:37 +02:00
|
|
|
UserAction.DELETE_FROM_HISTORY, "none",
|
2018-04-24 22:02:23 +02:00
|
|
|
"Deleting item failed", R.string.general_error));
|
2018-04-28 15:37:27 +02:00
|
|
|
|
2018-04-29 13:15:52 +02:00
|
|
|
disposables.add(onDelete);
|
2018-04-23 22:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 06:12:03 +01:00
|
|
|
private PlayQueue getPlayQueue() {
|
|
|
|
return getPlayQueue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private PlayQueue getPlayQueue(final int index) {
|
2018-02-03 18:36:40 +01:00
|
|
|
if (itemListAdapter == null) {
|
|
|
|
return new SinglePlayQueue(Collections.emptyList(), 0);
|
|
|
|
}
|
|
|
|
|
2018-01-28 07:14:38 +01:00
|
|
|
final List<LocalItem> infoItems = itemListAdapter.getItemsList();
|
2020-08-16 10:24:58 +02:00
|
|
|
final List<StreamInfoItem> streamInfoItems = new ArrayList<>(infoItems.size());
|
2018-01-28 07:14:38 +01:00
|
|
|
for (final LocalItem item : infoItems) {
|
|
|
|
if (item instanceof StreamStatisticsEntry) {
|
|
|
|
streamInfoItems.add(((StreamStatisticsEntry) item).toStreamInfoItem());
|
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
return new SinglePlayQueue(streamInfoItems, index);
|
|
|
|
}
|
2020-03-31 19:20:15 +02:00
|
|
|
|
|
|
|
private enum StatisticSortMode {
|
|
|
|
LAST_PLAYED,
|
|
|
|
MOST_PLAYED,
|
|
|
|
}
|
2018-01-17 06:12:03 +01:00
|
|
|
}
|
|
|
|
|