From 06fdd12f0c24e569a82b664ea47ab2b7ba1e65ee Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 28 Oct 2020 10:25:25 +0800 Subject: Deleting search history is now available. --- .../infinityforreddit/Activity/SearchActivity.java | 14 ++++++- .../Adapter/SearchActivityRecyclerViewAdapter.java | 11 ++++++ .../RecentSearchQuery/DeleteRecentSearchQuery.java | 44 ++++++++++++++++++++++ .../main/res/layout/item_recent_search_query.xml | 13 ++++++- 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/DeleteRecentSearchQuery.java diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchActivity.java index cd1989e2..69c637e9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchActivity.java @@ -39,6 +39,8 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RecentSearchQuery.DeleteRecentSearchQuery; +import ml.docilealligator.infinityforreddit.RecentSearchQuery.RecentSearchQuery; import ml.docilealligator.infinityforreddit.RecentSearchQuery.RecentSearchQueryViewModel; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -213,7 +215,17 @@ public class SearchActivity extends BaseActivity { private void bindView() { if (mAccountName != null) { - adapter = new SearchActivityRecyclerViewAdapter(this, mCustomThemeWrapper, this::search); + adapter = new SearchActivityRecyclerViewAdapter(this, mCustomThemeWrapper, new SearchActivityRecyclerViewAdapter.ItemOnClickListener() { + @Override + public void onClick(String query) { + search(query); + } + + @Override + public void onDelete(RecentSearchQuery recentSearchQuery) { + DeleteRecentSearchQuery.deleteRecentSearchQueryListener(mRedditDataRoomDatabase, recentSearchQuery, () -> {}); + } + }); recyclerView.setNestedScrollingEnabled(false); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SearchActivityRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SearchActivityRecyclerViewAdapter.java index 08a5fc21..75c22934 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SearchActivityRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SearchActivityRecyclerViewAdapter.java @@ -5,6 +5,7 @@ import android.graphics.drawable.Drawable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; @@ -23,16 +24,19 @@ public class SearchActivityRecyclerViewAdapter extends RecyclerView.Adapter recentSearchQueries; private int primaryTextColor; private Drawable historyIcon; + private Drawable deleteIcon; private ItemOnClickListener itemOnClickListener; public interface ItemOnClickListener { void onClick(String query); + void onDelete(RecentSearchQuery recentSearchQuery); } public SearchActivityRecyclerViewAdapter(Activity activity, CustomThemeWrapper customThemeWrapper, ItemOnClickListener itemOnClickListener) { this.primaryTextColor = customThemeWrapper.getPrimaryTextColor(); this.historyIcon = Utils.getTintedDrawable(activity, R.drawable.ic_history_24dp, customThemeWrapper.getPrimaryIconColor()); + this.deleteIcon = Utils.getTintedDrawable(activity, R.drawable.ic_delete_24dp, customThemeWrapper.getPrimaryIconColor()); this.itemOnClickListener = itemOnClickListener; } @@ -64,6 +68,8 @@ public class SearchActivityRecyclerViewAdapter extends RecyclerView.Adapter { if (recentSearchQueries != null && !recentSearchQueries.isEmpty()) { itemOnClickListener.onClick(recentSearchQueries.get(getAdapterPosition()).getSearchQuery()); } }); + + deleteImageView.setOnClickListener(view -> { + itemOnClickListener.onDelete(recentSearchQueries.get(getAdapterPosition())); + }); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/DeleteRecentSearchQuery.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/DeleteRecentSearchQuery.java new file mode 100644 index 00000000..f4ada66c --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/DeleteRecentSearchQuery.java @@ -0,0 +1,44 @@ +package ml.docilealligator.infinityforreddit.RecentSearchQuery; + +import android.os.AsyncTask; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class DeleteRecentSearchQuery { + public interface DeleteRecentSearchQueryListener { + void success(); + } + + public static void deleteRecentSearchQueryListener(RedditDataRoomDatabase redditDataRoomDatabase, + RecentSearchQuery recentSearchQuery, + DeleteRecentSearchQueryListener deleteRecentSearchQueryListener) { + new DeleteRecentSearchQueryAsyncTask(redditDataRoomDatabase, recentSearchQuery, deleteRecentSearchQueryListener).execute(); + } + + private static class DeleteRecentSearchQueryAsyncTask extends AsyncTask { + + private RecentSearchQueryDao recentSearchQueryDao; + private RecentSearchQuery recentSearchQuery; + private DeleteRecentSearchQueryListener deleteRecentSearchQueryListener; + + public DeleteRecentSearchQueryAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, + RecentSearchQuery recentSearchQuery, + DeleteRecentSearchQueryListener deleteRecentSearchQueryListener) { + this.recentSearchQueryDao = redditDataRoomDatabase.recentSearchQueryDao(); + this.recentSearchQuery = recentSearchQuery; + this.deleteRecentSearchQueryListener = deleteRecentSearchQueryListener; + } + + @Override + protected Void doInBackground(Void... voids) { + recentSearchQueryDao.deleteRecentSearchQueries(recentSearchQuery); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + deleteRecentSearchQueryListener.success(); + } + } +} diff --git a/app/src/main/res/layout/item_recent_search_query.xml b/app/src/main/res/layout/item_recent_search_query.xml index f014e2fe..fd851926 100644 --- a/app/src/main/res/layout/item_recent_search_query.xml +++ b/app/src/main/res/layout/item_recent_search_query.xml @@ -10,10 +10,21 @@ + + \ No newline at end of file -- cgit v1.2.3