From 482e8b3c732b543fd1dc932ddea36a1002bed08f Mon Sep 17 00:00:00 2001 From: Abhishek <52370265+abhishekabhi789@users.noreply.github.com> Date: Sat, 17 Aug 2024 08:50:16 +0530 Subject: Snackbar for recent search deletions (#1627) * snackbar will be shown for recent search deletions * the view from SearchActivityRecyclerViewAdapter causes crash in android 6 sometimes, so using appBarLayout instead. --------- Co-authored-by: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> --- .../activities/SearchActivity.java | 50 ++++++++++++++++++++-- .../SearchActivityRecyclerViewAdapter.java | 3 +- .../recentsearchquery/RecentSearchQueryDao.java | 5 ++- app/src/main/res/values/strings.xml | 4 +- 4 files changed, 56 insertions(+), 6 deletions(-) (limited to 'app') 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 37ef5cf3..a09d3b1a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.activities; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; @@ -15,6 +16,11 @@ import android.view.KeyEvent; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.RelativeLayout; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -24,11 +30,13 @@ import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.Executor; import javax.inject.Inject; @@ -264,7 +272,21 @@ public class SearchActivity extends BaseActivity { .setTitle(R.string.confirm) .setMessage(R.string.confirm_delete_all_recent_searches) .setPositiveButton(R.string.yes, (dialogInterface, i) -> { - executor.execute(() -> mRedditDataRoomDatabase.recentSearchQueryDao().deleteAllRecentSearchQueries(accountName)); + hideKeyboard(true); + executor.execute(() -> { + List deletedQueries = mRedditDataRoomDatabase.recentSearchQueryDao().getAllRecentSearchQueries(accountName); + mRedditDataRoomDatabase.recentSearchQueryDao().deleteAllRecentSearchQueries(accountName); + view.post(() -> Snackbar.make(view, R.string.deleted_all_recent_search, Snackbar.LENGTH_LONG) + .setAction(R.string.undo, v -> executor.execute(() -> mRedditDataRoomDatabase.recentSearchQueryDao().insertAll(deletedQueries))) + .addCallback(new Snackbar.Callback() { + @Override + public void onDismissed(Snackbar transientBottomBar, int event) { + super.onDismissed(transientBottomBar, event); + hideKeyboard(false); + } + }) + .show()); + }); }) .setNegativeButton(R.string.no, null) .show(); @@ -314,7 +336,20 @@ public class SearchActivity extends BaseActivity { @Override public void onDelete(RecentSearchQuery recentSearchQuery) { - executor.execute(() -> mRedditDataRoomDatabase.recentSearchQueryDao().deleteRecentSearchQueries(recentSearchQuery)); + hideKeyboard(true); + executor.execute(() -> { + mRedditDataRoomDatabase.recentSearchQueryDao().deleteRecentSearchQueries(recentSearchQuery); + Snackbar.make(appBarLayout, R.string.deleted_recent_search, Snackbar.LENGTH_SHORT) + .setAction(R.string.undo, v -> executor.execute(() -> mRedditDataRoomDatabase.recentSearchQueryDao().insert(recentSearchQuery))) + .addCallback(new Snackbar.Callback() { + @Override + public void onDismissed(Snackbar transientBottomBar, int event) { + super.onDismissed(transientBottomBar, event); + hideKeyboard(false); + } + }) + .show(); + }); } }); binding.recentSearchQueryRecyclerViewSearchActivity.setVisibility(View.VISIBLE); @@ -359,6 +394,15 @@ public class SearchActivity extends BaseActivity { } } + private void hideKeyboard(Boolean hide) { + View view = this.getCurrentFocus(); + if (view != null) { + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + if (hide) imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + else imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); + } + } + private void search(String query) { if (query.equalsIgnoreCase("suicide") && mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_SUICIDE_PREVENTION_ACTIVITY, true)) { Intent intent = new Intent(this, SuicidePreventionActivity.class); @@ -516,4 +560,4 @@ public class SearchActivity extends BaseActivity { public void onAccountSwitchEvent(SwitchAccountEvent event) { finish(); } -} +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SearchActivityRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SearchActivityRecyclerViewAdapter.java index 0761ed08..3cfdf2fb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SearchActivityRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SearchActivityRecyclerViewAdapter.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit.adapters; import android.content.res.ColorStateList; import android.view.LayoutInflater; +import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; @@ -125,4 +126,4 @@ public class SearchActivityRecyclerViewAdapter extends RecyclerView.Adapter recentSearchQueries); + @Query("SELECT * FROM recent_search_queries WHERE username = :username ORDER BY time DESC") LiveData> getAllRecentSearchQueriesLiveData(String username); @@ -25,4 +28,4 @@ public interface RecentSearchQueryDao { @Delete void deleteRecentSearchQueries(RecentSearchQuery recentSearchQuery); -} +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1b301887..e1f8faea 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -265,6 +265,9 @@ Confirm Are you sure you want to delete all recent search queries? + Deleted all recent searches + Deleted recent search + Undo Best Hot @@ -1406,5 +1409,4 @@ Remove comments Fully collapse comments - -- cgit v1.2.3