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());
}
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();
}
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);
}
}
});
}
Aggregations