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