diff options
author | Abhishek <52370265+abhishekabhi789@users.noreply.github.com> | 2023-09-14 14:29:40 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-14 14:29:40 +0000 |
commit | 6475dd3f05e7f02de7353315ea7db2ef3a875214 (patch) | |
tree | fb57c527b3314ac682a3b790eb7cd77c5c898d03 /app | |
parent | 4cdf12b664ee1b446cbfa44d9ecb3b3f1d3b01b4 (diff) | |
download | infinity-for-reddit-6475dd3f05e7f02de7353315ea7db2ef3a875214.tar infinity-for-reddit-6475dd3f05e7f02de7353315ea7db2ef3a875214.tar.gz infinity-for-reddit-6475dd3f05e7f02de7353315ea7db2ef3a875214.tar.bz2 infinity-for-reddit-6475dd3f05e7f02de7353315ea7db2ef3a875214.tar.lz infinity-for-reddit-6475dd3f05e7f02de7353315ea7db2ef3a875214.tar.xz infinity-for-reddit-6475dd3f05e7f02de7353315ea7db2ef3a875214.tar.zst infinity-for-reddit-6475dd3f05e7f02de7353315ea7db2ef3a875214.zip |
Added a button to clear all recent searches. (#1487)
* Added a button to clear all recent searches.
- Deletes all the searches for the current user from the database.
- Delete icon placed at the end of subreddit name layout in the activity search layout(I couldn't find a better place for it).
_Icon visible only when there are recent searches.
* Confirmation dialog shows when clearing all search history.
Diffstat (limited to 'app')
6 files changed, 97 insertions, 2 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java index 4bf0bcfa..3e786f2c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java @@ -47,6 +47,7 @@ import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.recentsearchquery.DeleteAllRecentSearchQueries; import ml.docilealligator.infinityforreddit.recentsearchquery.DeleteRecentSearchQuery; import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQuery; import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQueryViewModel; @@ -95,6 +96,8 @@ public class SearchActivity extends BaseActivity { ImageView clearSearchTextImageView; @BindView(R.id.link_handler_image_view_search_activity) ImageView linkHandlerImageView; + @BindView(R.id.delete_all_recent_searches_image_view_search_activity) + ImageView deleteAllSearchesImageView; @BindView(R.id.subreddit_name_relative_layout_search_activity) RelativeLayout subredditNameRelativeLayout; @BindView(R.id.search_in_text_view_search_activity) @@ -156,6 +159,7 @@ public class SearchActivity extends BaseActivity { setSupportActionBar(toolbar); clearSearchTextImageView.setVisibility(View.GONE); + deleteAllSearchesImageView.setVisibility(View.GONE); searchOnlySubreddits = getIntent().getBooleanExtra(EXTRA_SEARCH_ONLY_SUBREDDITS, false); searchOnlyUsers = getIntent().getBooleanExtra(EXTRA_SEARCH_ONLY_USERS, false); @@ -269,6 +273,10 @@ public class SearchActivity extends BaseActivity { } }); + deleteAllSearchesImageView.setOnClickListener(view -> { + DeleteAllRecentSearchQueries.deleteAllRecentSearchQueriesListener(this,mRedditDataRoomDatabase,mAccountName, () -> {}); + }); + if (savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); @@ -326,8 +334,10 @@ public class SearchActivity extends BaseActivity { mRecentSearchQueryViewModel.getAllRecentSearchQueries().observe(this, recentSearchQueries -> { if (recentSearchQueries != null && !recentSearchQueries.isEmpty()) { divider.setVisibility(View.VISIBLE); + deleteAllSearchesImageView.setVisibility(View.VISIBLE); } else { divider.setVisibility(View.GONE); + deleteAllSearchesImageView.setVisibility(View.GONE); } adapter.setRecentSearchQueries(recentSearchQueries); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/DeleteAllRecentSearchQueries.java b/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/DeleteAllRecentSearchQueries.java new file mode 100644 index 00000000..1aeac1d3 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/DeleteAllRecentSearchQueries.java @@ -0,0 +1,64 @@ +package ml.docilealligator.infinityforreddit.recentsearchquery; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.AsyncTask; + +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class DeleteAllRecentSearchQueries { + public static void deleteAllRecentSearchQueriesListener(Context context, RedditDataRoomDatabase redditDataRoomDatabase, String username, + DeleteAllRecentSearchQueriesListener deleteAllRecentSearchQueriesListener) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(R.string.confirm); + builder.setMessage(R.string.confirm_delete_all_recent_searches); + builder.setPositiveButton(context.getString(R.string.yes), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + new DeleteRecentSearchQueryAsyncTask(context, redditDataRoomDatabase, username, deleteAllRecentSearchQueriesListener).execute(); + } + }); + builder.setNegativeButton(context.getString(R.string.no), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + + public interface DeleteAllRecentSearchQueriesListener { + void success(); + } + + private static class DeleteRecentSearchQueryAsyncTask extends AsyncTask<Void, Void, Void> { + + private RecentSearchQueryDao recentSearchQueryDao; + private String username; + private DeleteAllRecentSearchQueriesListener deleteAllRecentSearchQueriesListener; + private Context context; + + public DeleteRecentSearchQueryAsyncTask(Context context, RedditDataRoomDatabase redditDataRoomDatabase, + String username, + DeleteAllRecentSearchQueriesListener deleteRecentSearchQueryListener) { + this.recentSearchQueryDao = redditDataRoomDatabase.recentSearchQueryDao(); + this.username = username; + this.deleteAllRecentSearchQueriesListener = deleteRecentSearchQueryListener; + this.context = context; + } + + @Override + protected Void doInBackground(Void... voids) { + recentSearchQueryDao.deleteAllRecentSearchQueries(username); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + deleteAllRecentSearchQueriesListener.success(); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQueryDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQueryDao.java index ae3ec0ee..407b5614 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQueryDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQueryDao.java @@ -20,6 +20,9 @@ public interface RecentSearchQueryDao { @Query("SELECT * FROM recent_search_queries WHERE username = :username ORDER BY time DESC") List<RecentSearchQuery> getAllRecentSearchQueries(String username); + @Query("DELETE FROM recent_search_queries WHERE username = :username") + void deleteAllRecentSearchQueries(String username); + @Delete void deleteRecentSearchQueries(RecentSearchQuery recentSearchQuery); } diff --git a/app/src/main/res/drawable/ic_delete_all_24.xml b/app/src/main/res/drawable/ic_delete_all_24.xml new file mode 100644 index 00000000..8b3673b4 --- /dev/null +++ b/app/src/main/res/drawable/ic_delete_all_24.xml @@ -0,0 +1,5 @@ +<vector android:height="24dp" android:tint="#000000" + android:viewportHeight="24" android:viewportWidth="24" + android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> + <path android:fillColor="@android:color/white" android:pathData="M15,16h4v2h-4zM15,8h7v2h-7zM15,12h6v2h-6zM3,18c0,1.1 0.9,2 2,2h6c1.1,0 2,-0.9 2,-2L13,8L3,8v10zM5,10h6v8L5,18v-8zM10,4L6,4L5,5L2,5v2h12L14,5h-3z"/> +</vector> diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index f6269887..1a4580c6 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -81,13 +81,13 @@ android:layout_height="wrap_content" android:background="?attr/selectableItemBackground" android:clickable="true" - android:focusable="true" - android:padding="16dp"> + android:focusable="true"> <TextView android:id="@+id/search_in_text_view_search_activity" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_margin="16dp" android:text="@string/search_in" android:textColor="?attr/colorAccent" android:textSize="?attr/font_default" @@ -105,6 +105,16 @@ android:textColor="?attr/primaryTextColor" android:textSize="?attr/font_default" android:fontFamily="?attr/font_family" /> + <ImageView + android:id="@+id/delete_all_recent_searches_image_view_search_activity" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignParentEnd="true" + android:padding="16dp" + android:clickable="true" + android:focusable="true" + android:src="@drawable/ic_delete_all_24" + app:tint="@color/primaryTextColor" /> </RelativeLayout> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b5bdac1..1cf7ff8a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -261,6 +261,9 @@ <string name="search_in">Search in</string> <string name="all_subreddits">All subreddits</string> + <string name="confirm">Confirm</string> + <string name="confirm_delete_all_recent_searches">Are you sure you want to delete all recent search queries?</string> + <string name="sort_best">Best</string> <string name="sort_hot">Hot</string> <string name="sort_new">New</string> |