Search in sources :

Example 1 with SearchHistoryEntry

use of org.schabi.newpipe.database.history.model.SearchHistoryEntry in project NewPipe by TeamNewPipe.

the class HistoryRecordManager method onSearched.

public Maybe<Long> onSearched(final int serviceId, final String search) {
    if (!isSearchHistoryEnabled())
        return Maybe.empty();
    final Date currentTime = new Date();
    final SearchHistoryEntry newEntry = new SearchHistoryEntry(currentTime, serviceId, search);
    return Maybe.fromCallable(() -> database.runInTransaction(() -> {
        SearchHistoryEntry latestEntry = searchHistoryTable.getLatestEntry();
        if (latestEntry != null && latestEntry.hasEqualValues(newEntry)) {
            latestEntry.setCreationDate(currentTime);
            return (long) searchHistoryTable.update(latestEntry);
        } else {
            return searchHistoryTable.insert(newEntry);
        }
    })).subscribeOn(Schedulers.io());
}
Also used : SearchHistoryEntry(org.schabi.newpipe.database.history.model.SearchHistoryEntry) Date(java.util.Date)

Example 2 with SearchHistoryEntry

use of org.schabi.newpipe.database.history.model.SearchHistoryEntry in project NewPipe by TeamNewPipe.

the class SearchHistoryFragment method onHistoryItemLongClick.

@Override
public void onHistoryItemLongClick(final SearchHistoryEntry item) {
    if (activity == null)
        return;
    new AlertDialog.Builder(activity).setTitle(item.getSearch()).setMessage(R.string.delete_item_search_history).setCancelable(true).setNeutralButton(R.string.cancel, null).setPositiveButton(R.string.delete_one, (dialog, i) -> {
        final Disposable onDelete = historyRecordManager.deleteSearches(Collections.singleton(item)).observeOn(AndroidSchedulers.mainThread()).subscribe(ignored -> {
        /*successful*/
        }, error -> Log.e(TAG, "Search history Delete One failed:", error));
        disposables.add(onDelete);
        makeSnackbar(R.string.item_deleted);
    }).setNegativeButton(R.string.delete_all, (dialog, i) -> {
        final Disposable onDeleteAll = historyRecordManager.deleteSearchHistory(item.getSearch()).observeOn(AndroidSchedulers.mainThread()).subscribe(ignored -> {
        /*successful*/
        }, error -> Log.e(TAG, "Search history Delete All failed:", error));
        disposables.add(onDeleteAll);
        makeSnackbar(R.string.item_deleted);
    }).show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Disposable(io.reactivex.disposables.Disposable) Context(android.content.Context) Bundle(android.os.Bundle) LayoutInflater(android.view.LayoutInflater) Collection(java.util.Collection) NewPipe(org.schabi.newpipe.extractor.NewPipe) NavigationHelper(org.schabi.newpipe.util.NavigationHelper) StringRes(android.support.annotation.StringRes) NonNull(android.support.annotation.NonNull) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) ViewGroup(android.view.ViewGroup) RecyclerView(android.support.v7.widget.RecyclerView) SearchHistoryEntry(org.schabi.newpipe.database.history.model.SearchHistoryEntry) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) AlertDialog(android.support.v7.app.AlertDialog) TextView(android.widget.TextView) Flowable(io.reactivex.Flowable) View(android.view.View) Nullable(android.support.annotation.Nullable) R(org.schabi.newpipe.R) Localization(org.schabi.newpipe.util.Localization) Collections(java.util.Collections) Log(android.util.Log)

Example 3 with SearchHistoryEntry

use of org.schabi.newpipe.database.history.model.SearchHistoryEntry in project NewPipe by TeamNewPipe.

the class SearchFragment method initSuggestionObserver.

private void initSuggestionObserver() {
    if (DEBUG)
        Log.d(TAG, "initSuggestionObserver() called");
    if (suggestionDisposable != null)
        suggestionDisposable.dispose();
    final Observable<String> observable = suggestionPublisher.debounce(SUGGESTIONS_DEBOUNCE, TimeUnit.MILLISECONDS).startWith(searchQuery != null ? searchQuery : "").filter(query -> isSuggestionsEnabled);
    suggestionDisposable = observable.switchMap(query -> {
        final Flowable<List<SearchHistoryEntry>> flowable = historyRecordManager.getRelatedSearches(query, 3, 25);
        final Observable<List<SuggestionItem>> local = flowable.toObservable().map(searchHistoryEntries -> {
            List<SuggestionItem> result = new ArrayList<>();
            for (SearchHistoryEntry entry : searchHistoryEntries) result.add(new SuggestionItem(true, entry.getSearch()));
            return result;
        });
        if (query.length() < THRESHOLD_NETWORK_SUGGESTION) {
            // Only pass through if the query length is equal or greater than THRESHOLD_NETWORK_SUGGESTION
            return local.materialize();
        }
        final Observable<List<SuggestionItem>> network = ExtractorHelper.suggestionsFor(serviceId, query, contentCountry).toObservable().map(strings -> {
            List<SuggestionItem> result = new ArrayList<>();
            for (String entry : strings) {
                result.add(new SuggestionItem(false, entry));
            }
            return result;
        });
        return Observable.zip(local, network, (localResult, networkResult) -> {
            List<SuggestionItem> result = new ArrayList<>();
            if (localResult.size() > 0)
                result.addAll(localResult);
            // Remove duplicates
            final Iterator<SuggestionItem> iterator = networkResult.iterator();
            while (iterator.hasNext() && localResult.size() > 0) {
                final SuggestionItem next = iterator.next();
                for (SuggestionItem item : localResult) {
                    if (item.query.equals(next.query)) {
                        iterator.remove();
                        break;
                    }
                }
            }
            if (networkResult.size() > 0)
                result.addAll(networkResult);
            return result;
        }).materialize();
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(listNotification -> {
        if (listNotification.isOnNext()) {
            handleSuggestions(listNotification.getValue());
        } else if (listNotification.isOnError()) {
            Throwable error = listNotification.getError();
            if (!ExtractorHelper.hasAssignableCauseThrowable(error, IOException.class, SocketException.class, InterruptedException.class, InterruptedIOException.class)) {
                onSuggestionError(error);
            }
        }
    });
}
Also used : Bundle(android.os.Bundle) ReCaptchaActivity(org.schabi.newpipe.ReCaptchaActivity) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) SearchHistoryEntry(org.schabi.newpipe.database.history.model.SearchHistoryEntry) ExtractorHelper(org.schabi.newpipe.util.ExtractorHelper) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) SearchResult(org.schabi.newpipe.extractor.search.SearchResult) PreferenceManager(android.preference.PreferenceManager) AnimationUtils.animateView(org.schabi.newpipe.util.AnimationUtils.animateView) R(org.schabi.newpipe.R) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) Log(android.util.Log) BaseListFragment(org.schabi.newpipe.fragments.list.BaseListFragment) UserAction(org.schabi.newpipe.report.UserAction) ViewGroup(android.view.ViewGroup) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) TextView(android.widget.TextView) Constants(org.schabi.newpipe.util.Constants) PublishSubject(io.reactivex.subjects.PublishSubject) BackPressable(org.schabi.newpipe.fragments.BackPressable) Queue(java.util.Queue) Nullable(android.support.annotation.Nullable) EditorInfo(android.view.inputmethod.EditorInfo) DecelerateInterpolator(android.view.animation.DecelerateInterpolator) TextWatcher(android.text.TextWatcher) Context(android.content.Context) KeyEvent(android.view.KeyEvent) ListExtractor(org.schabi.newpipe.extractor.ListExtractor) StreamingService(org.schabi.newpipe.extractor.StreamingService) NewPipe(org.schabi.newpipe.extractor.NewPipe) NavigationHelper(org.schabi.newpipe.util.NavigationHelper) Intent(android.content.Intent) AnimationUtils(org.schabi.newpipe.util.AnimationUtils) Callable(java.util.concurrent.Callable) NonNull(android.support.annotation.NonNull) InterruptedIOException(java.io.InterruptedIOException) Editable(android.text.Editable) MenuItem(android.view.MenuItem) InputMethodManager(android.view.inputmethod.InputMethodManager) ArrayList(java.util.ArrayList) SearchEngine(org.schabi.newpipe.extractor.search.SearchEngine) SocketException(java.net.SocketException) InfoItem(org.schabi.newpipe.extractor.InfoItem) Flowable(io.reactivex.Flowable) HistoryRecordManager(org.schabi.newpipe.history.HistoryRecordManager) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) LayoutManagerSmoothScroller(org.schabi.newpipe.util.LayoutManagerSmoothScroller) Observable(io.reactivex.Observable) State(icepick.State) ActionBar(android.support.v7.app.ActionBar) Iterator(java.util.Iterator) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Consumer(io.reactivex.functions.Consumer) TimeUnit(java.util.concurrent.TimeUnit) RecyclerView(android.support.v7.widget.RecyclerView) AlertDialog(android.support.v7.app.AlertDialog) SharedPreferences(android.content.SharedPreferences) TooltipCompat(android.support.v7.widget.TooltipCompat) Activity(android.app.Activity) EditText(android.widget.EditText) SocketException(java.net.SocketException) InterruptedIOException(java.io.InterruptedIOException) ArrayList(java.util.ArrayList) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SearchHistoryEntry(org.schabi.newpipe.database.history.model.SearchHistoryEntry) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Context (android.content.Context)2 Bundle (android.os.Bundle)2 NonNull (android.support.annotation.NonNull)2 Nullable (android.support.annotation.Nullable)2 AlertDialog (android.support.v7.app.AlertDialog)2 RecyclerView (android.support.v7.widget.RecyclerView)2 Log (android.util.Log)2 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 TextView (android.widget.TextView)2 Flowable (io.reactivex.Flowable)2 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)2 Disposable (io.reactivex.disposables.Disposable)2 SearchHistoryEntry (org.schabi.newpipe.database.history.model.SearchHistoryEntry)2 Activity (android.app.Activity)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 PreferenceManager (android.preference.PreferenceManager)1 StringRes (android.support.annotation.StringRes)1