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 e20d6bad3..a3b01f251 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 @@ -12,6 +12,7 @@ import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.TooltipCompat; +import android.support.v7.widget.helper.ItemTouchHelper; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; @@ -45,10 +46,9 @@ import org.schabi.newpipe.fragments.list.BaseListFragment; import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.UserAction; -import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.AnimationUtils; +import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.ExtractorHelper; -import org.schabi.newpipe.util.LayoutManagerSmoothScroller; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.ServiceHelper; @@ -73,8 +73,8 @@ import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; import io.reactivex.subjects.PublishSubject; +import static android.support.v7.widget.helper.ItemTouchHelper.Callback.makeMovementFlags; import static java.util.Arrays.asList; - import static org.schabi.newpipe.util.AnimationUtils.animateView; public class SearchFragment @@ -298,7 +298,23 @@ public class SearchFragment suggestionsPanel = rootView.findViewById(R.id.suggestions_panel); suggestionsRecyclerView = rootView.findViewById(R.id.suggestions_list); suggestionsRecyclerView.setAdapter(suggestionListAdapter); - suggestionsRecyclerView.setLayoutManager(new LayoutManagerSmoothScroller(activity)); + new ItemTouchHelper(new ItemTouchHelper.Callback() { + @Override + public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { + return getSuggestionMovementFlags(recyclerView, viewHolder); + } + + @Override + public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, + @NonNull RecyclerView.ViewHolder viewHolder1) { + return false; + } + + @Override + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) { + onSuggestionItemSwiped(viewHolder, i); + } + }).attachToRecyclerView(suggestionsRecyclerView); searchToolbarContainer = activity.findViewById(R.id.toolbar_search_container); searchEditText = searchToolbarContainer.findViewById(R.id.toolbar_search_edit_text); @@ -901,4 +917,28 @@ public class SearchFragment return true; } + + /*////////////////////////////////////////////////////////////////////////// + // Suggestion item touch helper + //////////////////////////////////////////////////////////////////////////*/ + + public int getSuggestionMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { + final int position = viewHolder.getAdapterPosition(); + final SuggestionItem item = suggestionListAdapter.getItem(position); + return item.fromHistory ? makeMovementFlags(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) : 0; + } + + public void onSuggestionItemSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) { + final int position = viewHolder.getAdapterPosition(); + final String query = suggestionListAdapter.getItem(position).query; + final Disposable onDelete = historyRecordManager.deleteSearchHistory(query) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + howManyDeleted -> suggestionPublisher + .onNext(searchEditText.getText().toString()), + throwable -> showSnackBarError(throwable, + 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/fragments/list/search/SuggestionListAdapter.java b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SuggestionListAdapter.java index 7b5f72c53..3f4e9af0b 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SuggestionListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SuggestionListAdapter.java @@ -75,7 +75,7 @@ public class SuggestionListAdapter extends RecyclerView.Adapter dialog.dismiss())) + .setPositiveButton(R.string.delete, ((dialog, which) -> { + final Disposable onDelete = recordManager.deleteWholeStreamHistory() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + howManyDeleted -> Toast.makeText(getContext(), + 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))); + + final Disposable onClearOrphans = recordManager.removeOrphanedRecords() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + howManyDeleted -> {}, + 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(onClearOrphans); + disposables.add(onDelete); + })) + .create() + .show(); + break; + default: + return super.onOptionsItemSelected(item); + } + return true; + } + /////////////////////////////////////////////////////////////////////////// // Fragment LifeCycle - Loading /////////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/res/layout/fragment_search.xml b/app/src/main/res/layout/fragment_search.xml index d49d23175..c6f733e3f 100644 --- a/app/src/main/res/layout/fragment_search.xml +++ b/app/src/main/res/layout/fragment_search.xml @@ -67,6 +67,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" + app:layoutManager="LinearLayoutManager" tools:listitem="@layout/item_search_suggestion"/> diff --git a/app/src/main/res/menu/menu_history.xml b/app/src/main/res/menu/menu_history.xml index 526f500f6..9c030b5fe 100644 --- a/app/src/main/res/menu/menu_history.xml +++ b/app/src/main/res/menu/menu_history.xml @@ -3,8 +3,8 @@ xmlns:tools="http://schemas.android.com/tools" tools:context="org.schabi.newpipe.history.HistoryActivity"> -