diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index fa2cf8690..38935113f 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -152,7 +152,7 @@ public class MainActivity extends AppCompatActivity { settings.setOnClickListener(view -> NavigationHelper.openSettings(this)); downloads.setOnClickListener(view ->NavigationHelper.openDownloads(this)); history.setOnClickListener(view -> - NavigationHelper.openLastPlayedFragment(getSupportFragmentManager())); + NavigationHelper.openStatisticFragment(getSupportFragmentManager())); } private void setupDrawerHeader() { diff --git a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java index abaf691a4..f3f390c4d 100644 --- a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java @@ -39,8 +39,6 @@ import io.reactivex.disposables.CompositeDisposable; public final class BookmarkFragment extends BaseLocalListFragment, Void> { - private View mostPlayedButton; - @State protected Parcelable itemsListState; @@ -94,14 +92,6 @@ public final class BookmarkFragment super.initViews(rootView, savedInstanceState); } - @Override - protected View getListHeader() { - final View headerRootLayout = activity.getLayoutInflater() - .inflate(R.layout.bookmark_header, itemsList, false); - mostPlayedButton = headerRootLayout.findViewById(R.id.mostPlayed); - return headerRootLayout; - } - @Override protected void initListeners() { super.initListeners(); @@ -135,12 +125,6 @@ public final class BookmarkFragment } } }); - - mostPlayedButton.setOnClickListener(view -> { - if (getParentFragment() != null) { - NavigationHelper.openMostPlayedFragment(getParentFragment().getFragmentManager()); - } - }); } /////////////////////////////////////////////////////////////////////////// @@ -173,7 +157,6 @@ public final class BookmarkFragment @Override public void onDestroyView() { super.onDestroyView(); - if (mostPlayedButton != null) mostPlayedButton.setOnClickListener(null); if (disposables != null) disposables.clear(); if (databaseSubscription != null) databaseSubscription.cancel(); diff --git a/app/src/main/java/org/schabi/newpipe/local/history/LastPlayedFragment.java b/app/src/main/java/org/schabi/newpipe/local/history/LastPlayedFragment.java deleted file mode 100644 index 4dc6fd4cd..000000000 --- a/app/src/main/java/org/schabi/newpipe/local/history/LastPlayedFragment.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.schabi.newpipe.local.history; - -import org.schabi.newpipe.R; -import org.schabi.newpipe.database.stream.StreamStatisticsEntry; -import org.schabi.newpipe.local.history.StatisticsPlaylistFragment; - -import java.util.Collections; -import java.util.List; - -public final class LastPlayedFragment extends StatisticsPlaylistFragment { - @Override - protected String getName() { - return getString(R.string.title_last_played); - } - - @Override - protected List processResult(List results) { - Collections.sort(results, (left, right) -> - right.latestAccessDate.compareTo(left.latestAccessDate)); - return results; - } -} diff --git a/app/src/main/java/org/schabi/newpipe/local/history/MostPlayedFragment.java b/app/src/main/java/org/schabi/newpipe/local/history/MostPlayedFragment.java deleted file mode 100644 index cf473089a..000000000 --- a/app/src/main/java/org/schabi/newpipe/local/history/MostPlayedFragment.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.schabi.newpipe.local.history; - -import org.schabi.newpipe.R; -import org.schabi.newpipe.database.stream.StreamStatisticsEntry; -import org.schabi.newpipe.local.history.StatisticsPlaylistFragment; - -import java.util.Collections; -import java.util.List; - -public final class MostPlayedFragment extends StatisticsPlaylistFragment { - @Override - protected String getName() { - return getString(R.string.title_most_played); - } - - @Override - protected List processResult(List results) { - Collections.sort(results, (left, right) -> - ((Long) right.watchCount).compareTo(left.watchCount)); - return results; - } - -} diff --git a/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java index 7136157a8..33dd5eeca 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java @@ -7,11 +7,12 @@ import android.os.Bundle; import android.os.Parcelable; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.annotation.StringRes; import android.support.design.widget.Snackbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; @@ -36,12 +37,16 @@ import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.Disposable; -public abstract class StatisticsPlaylistFragment +public class StatisticsPlaylistFragment extends BaseLocalListFragment, Void> { private View headerPlayAllButton; private View headerPopupButton; private View headerBackgroundButton; + private View playlistCtrl; + private View sortButton; + private ImageView sortButtonIcon; + private TextView sortButtonText; @State protected Parcelable itemsListState; @@ -51,14 +56,26 @@ public abstract class StatisticsPlaylistFragment private HistoryRecordManager recordManager; private CompositeDisposable disposables = new CompositeDisposable(); + private enum StatisticSortMode { + LAST_PLAYED, + MOST_PLAYED, + } - /////////////////////////////////////////////////////////////////////////// - // Abstracts - /////////////////////////////////////////////////////////////////////////// + StatisticSortMode sortMode = StatisticSortMode.LAST_PLAYED; - protected abstract String getName(); - - protected abstract List processResult(final List results); + protected List processResult(final List results) { + switch (sortMode) { + case LAST_PLAYED: + Collections.sort(results, (left, right) -> + right.latestAccessDate.compareTo(left.latestAccessDate)); + return results; + case MOST_PLAYED: + Collections.sort(results, (left, right) -> + ((Long) right.watchCount).compareTo(left.watchCount)); + return results; + default: return null; + } + } /////////////////////////////////////////////////////////////////////////// // Fragment LifeCycle - Creation @@ -84,16 +101,20 @@ public abstract class StatisticsPlaylistFragment @Override protected void initViews(View rootView, Bundle savedInstanceState) { super.initViews(rootView, savedInstanceState); - setTitle(getName()); + setTitle(getString(R.string.title_last_played)); } @Override protected View getListHeader() { - final View headerRootLayout = activity.getLayoutInflater().inflate(R.layout.playlist_control, + final View headerRootLayout = activity.getLayoutInflater().inflate(R.layout.statistic_playlist_control, itemsList, false); + playlistCtrl = headerRootLayout.findViewById(R.id.playlist_control); 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); + sortButton = headerRootLayout.findViewById(R.id.sortButton); + sortButtonIcon = headerRootLayout.findViewById(R.id.sortButtonIcon); + sortButtonText = headerRootLayout.findViewById(R.id.sortButtonText); return headerRootLayout; } @@ -199,6 +220,8 @@ public abstract class StatisticsPlaylistFragment super.handleResult(result); if (itemListAdapter == null) return; + playlistCtrl.setVisibility(View.VISIBLE); + itemListAdapter.clearStreamItemList(); if (result.isEmpty()) { @@ -218,6 +241,7 @@ public abstract class StatisticsPlaylistFragment NavigationHelper.playOnPopupPlayer(activity, getPlayQueue())); headerBackgroundButton.setOnClickListener(view -> NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue())); + sortButton.setOnClickListener(view -> toogleSortMode()); hideLoading(); } @@ -244,6 +268,21 @@ public abstract class StatisticsPlaylistFragment // Utils //////////////////////////////////////////////////////////////////////////*/ + private void toogleSortMode() { + if(sortMode == StatisticSortMode.LAST_PLAYED) { + sortMode = StatisticSortMode.MOST_PLAYED; + setTitle(getString(R.string.title_most_played)); + sortButtonIcon.setImageResource(getIconByAttr(R.attr.history)); + sortButtonText.setText(R.string.title_last_played); + } else { + sortMode = StatisticSortMode.LAST_PLAYED; + setTitle(getString(R.string.title_last_played)); + sortButtonIcon.setImageResource(getIconByAttr(R.attr.filter)); + sortButtonText.setText(R.string.title_most_played); + } + startLoading(true); + } + private void showStreamDialog(final StreamStatisticsEntry item) { final Context context = getContext(); final Activity activity = getActivity(); @@ -301,7 +340,7 @@ public abstract class StatisticsPlaylistFragment throwable -> showSnackBarError(throwable, UserAction.SOMETHING_ELSE, "none", "Deleting item failed", R.string.general_error)); - + disposables.add(onDelte); } } @@ -324,5 +363,10 @@ public abstract class StatisticsPlaylistFragment } return new SinglePlayQueue(streamInfoItems, index); } + + private int getIconByAttr(final int attr) { + return getContext().obtainStyledAttributes(new int[] {attr}) + .getResourceId(0, -1); + } } diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 59754c11d..ebbeb06f8 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -37,9 +37,8 @@ import org.schabi.newpipe.local.feed.FeedFragment; import org.schabi.newpipe.fragments.list.kiosk.KioskFragment; import org.schabi.newpipe.fragments.list.playlist.PlaylistFragment; import org.schabi.newpipe.fragments.list.search.SearchFragment; -import org.schabi.newpipe.local.history.LastPlayedFragment; +import org.schabi.newpipe.local.history.StatisticsPlaylistFragment; import org.schabi.newpipe.local.playlist.LocalPlaylistFragment; -import org.schabi.newpipe.local.history.MostPlayedFragment; import org.schabi.newpipe.local.subscription.SubscriptionsImportFragment; import org.schabi.newpipe.player.BackgroundPlayer; import org.schabi.newpipe.player.BackgroundPlayerActivity; @@ -351,16 +350,9 @@ public class NavigationHelper { .commit(); } - public static void openLastPlayedFragment(FragmentManager fragmentManager) { + public static void openStatisticFragment(FragmentManager fragmentManager) { defaultTransaction(fragmentManager) - .replace(R.id.fragment_holder, new LastPlayedFragment()) - .addToBackStack(null) - .commit(); - } - - public static void openMostPlayedFragment(FragmentManager fragmentManager) { - defaultTransaction(fragmentManager) - .replace(R.id.fragment_holder, new MostPlayedFragment()) + .replace(R.id.fragment_holder, new StatisticsPlaylistFragment()) .addToBackStack(null) .commit(); } diff --git a/app/src/main/res/layout/bookmark_header.xml b/app/src/main/res/layout/statistic_playlist_control.xml similarity index 65% rename from app/src/main/res/layout/bookmark_header.xml rename to app/src/main/res/layout/statistic_playlist_control.xml index ac76eb0c7..8dc4e8c08 100644 --- a/app/src/main/res/layout/bookmark_header.xml +++ b/app/src/main/res/layout/statistic_playlist_control.xml @@ -1,21 +1,20 @@ - + android:background="?attr/selectableItemBackground" + android:orientation="vertical"> - + - \ No newline at end of file + \ No newline at end of file