From 4c10ef65f5ad83e20fb37b3c86bbad836e79a7a1 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Sat, 28 Apr 2018 16:11:37 +0200 Subject: [PATCH] add delete while history add delete whole history --- .../newpipe/fragments/BaseStateFragment.java | 3 +- .../fragments/list/search/SearchFragment.java | 2 +- .../local/history/HistoryRecordManager.java | 25 +++--- .../history/StatisticsPlaylistFragment.java | 2 +- .../org/schabi/newpipe/report/UserAction.java | 3 +- .../settings/HistorySettingsFragment.java | 76 +++++++++++++++++++ app/src/main/res/values/settings_keys.xml | 2 + app/src/main/res/values/strings.xml | 8 ++ app/src/main/res/xml/history_settings.xml | 10 +++ 9 files changed, 113 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/BaseStateFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/BaseStateFragment.java index cb9ce8947..5707716bf 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/BaseStateFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/BaseStateFragment.java @@ -239,7 +239,8 @@ public abstract class BaseStateFragment extends BaseFragment implements ViewC if (rootView == null && getView() != null) rootView = getView(); if (rootView == null) return; - ErrorActivity.reportError(getContext(), exception, MainActivity.class, rootView, ErrorActivity.ErrorInfo.make(userAction, serviceName, request, errorId)); + ErrorActivity.reportError(getContext(), exception, MainActivity.class, rootView, + ErrorActivity.ErrorInfo.make(userAction, serviceName, request, errorId)); } /*////////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java index 13e349680..d07ff6448 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java @@ -544,7 +544,7 @@ public class SearchFragment howManyDeleted -> suggestionPublisher .onNext(searchEditText.getText().toString()), throwable -> showSnackBarError(throwable, - UserAction.SOMETHING_ELSE, "none", + UserAction.DELETE_FROM_HISTORY, "none", "Deleting item failed", R.string.general_error) ); disposables.add(onDelete); diff --git a/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java b/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java index fae71b8ea..d3d824103 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java @@ -45,6 +45,7 @@ import java.util.List; import io.reactivex.Flowable; import io.reactivex.Maybe; +import io.reactivex.Scheduler; import io.reactivex.Single; import io.reactivex.schedulers.Schedulers; @@ -98,6 +99,11 @@ public class HistoryRecordManager { .subscribeOn(Schedulers.io()); } + public Single deleteWholeStreamHistory() { + return Single.fromCallable(() -> streamHistoryTable.deleteAll()) + .subscribeOn(Schedulers.io()); + } + public Flowable> getStreamHistory() { return streamHistoryTable.getHistory().subscribeOn(Schedulers.io()); } @@ -132,20 +138,6 @@ public class HistoryRecordManager { // Search History /////////////////////////////////////////////////////// - public Single> insertSearches(final Collection entries) { - return Single.fromCallable(() -> searchHistoryTable.insertAll(entries)) - .subscribeOn(Schedulers.io()); - } - - public Single deleteSearches(final Collection entries) { - return Single.fromCallable(() -> searchHistoryTable.delete(entries)) - .subscribeOn(Schedulers.io()); - } - - public Flowable> getSearchHistory() { - return searchHistoryTable.getAll(); - } - public Maybe onSearched(final int serviceId, final String search) { if (!isSearchHistoryEnabled()) return Maybe.empty(); @@ -168,6 +160,11 @@ public class HistoryRecordManager { .subscribeOn(Schedulers.io()); } + public Single deleteWholeSearchHistory() { + return Single.fromCallable(() -> searchHistoryTable.deleteAll()) + .subscribeOn(Schedulers.io()); + } + public Flowable> getRelatedSearches(final String query, final int similarQueryLimit, final int uniqueQueryLimit) { 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 33dd5eeca..52e09bfe4 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 @@ -338,7 +338,7 @@ public class StatisticsPlaylistFragment howManyDelted -> Snackbar.make(getView(), R.string.one_item_deleted, Snackbar.LENGTH_SHORT).show(), throwable -> showSnackBarError(throwable, - UserAction.SOMETHING_ELSE, "none", + UserAction.DELETE_FROM_HISTORY, "none", "Deleting item failed", R.string.general_error)); disposables.add(onDelte); diff --git a/app/src/main/java/org/schabi/newpipe/report/UserAction.java b/app/src/main/java/org/schabi/newpipe/report/UserAction.java index c06c65a81..93a3ce16c 100644 --- a/app/src/main/java/org/schabi/newpipe/report/UserAction.java +++ b/app/src/main/java/org/schabi/newpipe/report/UserAction.java @@ -14,7 +14,8 @@ public enum UserAction { REQUESTED_STREAM("requested stream"), REQUESTED_CHANNEL("requested channel"), REQUESTED_PLAYLIST("requested playlist"), - REQUESTED_KIOSK("requested kiosk"); + REQUESTED_KIOSK("requested kiosk"), + DELETE_FROM_HISTORY("delete from history"); private final String message; diff --git a/app/src/main/java/org/schabi/newpipe/settings/HistorySettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/HistorySettingsFragment.java index 53e8d6fc4..5806405e6 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/HistorySettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/HistorySettingsFragment.java @@ -1,20 +1,44 @@ package org.schabi.newpipe.settings; +import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.design.widget.Snackbar; +import android.support.v7.app.AlertDialog; import android.support.v7.preference.Preference; +import android.util.Log; import android.widget.Toast; +import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.R; +import org.schabi.newpipe.local.history.HistoryRecordManager; +import org.schabi.newpipe.report.ErrorActivity; +import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.util.InfoCache; +import java.util.ArrayList; +import java.util.Collection; + +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.disposables.Disposable; +import io.reactivex.disposables.Disposables; + public class HistorySettingsFragment extends BasePreferenceFragment { private String cacheWipeKey; + private String viewsHistroyClearKey; + private String searchHistoryClearKey; + private HistoryRecordManager recordManager; + private CompositeDisposable disposables; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); cacheWipeKey = getString(R.string.metadata_cache_wipe_key); + viewsHistroyClearKey = getString(R.string.clear_views_history_key); + searchHistoryClearKey = getString(R.string.clear_search_history_key); + recordManager = new HistoryRecordManager(getActivity()); + disposables = new CompositeDisposable(); } @Override @@ -30,6 +54,58 @@ public class HistorySettingsFragment extends BasePreferenceFragment { Toast.LENGTH_SHORT).show(); } + if (preference.getKey().equals(viewsHistroyClearKey)) { + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.delete_view_history_alert) + .setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.dismiss())) + .setPositiveButton(R.string.delete, ((dialog, which) -> { + final Disposable onDelte = recordManager.deleteWholeStreamHistory() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + howManyDelted -> Toast.makeText(getActivity(), + R.string.view_history_deleted, + Toast.LENGTH_SHORT).show(), + throwable -> ErrorActivity.reportError(getContext(), + throwable, + SettingsActivity.class, null, + ErrorActivity.ErrorInfo.make( + UserAction.DELETE_FROM_HISTORY, + "none", + "Delete view history", + R.string.general_error))); + + disposables.add(onDelte); + })) + .create() + .show(); + } + + if (preference.getKey().equals(searchHistoryClearKey)) { + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.delete_search_history_alert) + .setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.dismiss())) + .setPositiveButton(R.string.delete, ((dialog, which) -> { + final Disposable onDelte = recordManager.deleteWholeSearchHistory() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + howManyDelted -> Toast.makeText(getActivity(), + R.string.search_history_deleted, + Toast.LENGTH_SHORT).show(), + throwable -> ErrorActivity.reportError(getContext(), + throwable, + SettingsActivity.class, null, + ErrorActivity.ErrorInfo.make( + UserAction.DELETE_FROM_HISTORY, + "none", + "Delete search history", + R.string.general_error))); + + disposables.add(onDelte); + })) + .create() + .show(); + } + return super.onPreferenceTreeClick(preference); } } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index dcbea2eb8..20a6ab68f 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -147,6 +147,8 @@ download_thumbnail_key cache_wipe_key + clear_play_history + clear_search_history file_rename diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8878513c1..3edbd4e9b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -155,6 +155,14 @@ Export database Will override your current history and subscriptions Export history, subscriptions and playlists. + Clear watch history + Deletes the history of played streams. + Delete whole watch history. + Watch history deleted. + Clear search history + Deletes history of search keywords. + Delete whole search history. + Search history deleted. Error Network error diff --git a/app/src/main/res/xml/history_settings.xml b/app/src/main/res/xml/history_settings.xml index 2e19517b9..2be1e5341 100644 --- a/app/src/main/res/xml/history_settings.xml +++ b/app/src/main/res/xml/history_settings.xml @@ -21,4 +21,14 @@ android:summary="@string/metadata_cache_wipe_summary" android:title="@string/metadata_cache_wipe_title"/> + + + +