Skip to content

Snackbar for recent search deletions #1627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +16,11 @@
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;
Expand All @@ -24,11 +30,13 @@
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;
Expand Down Expand Up @@ -264,7 +272,21 @@ public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
.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<RecentSearchQuery> 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();
Expand Down Expand Up @@ -314,7 +336,20 @@ public void onClick(RecentSearchQuery recentSearchQuery) {

@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);
Expand Down Expand Up @@ -359,6 +394,15 @@ public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull R
}
}

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);
Expand Down Expand Up @@ -516,4 +560,4 @@ protected void onDestroy() {
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.res.ColorStateList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -125,4 +126,4 @@ public RecentSearchQueryViewHolder(@NonNull ItemRecentSearchQueryBinding binding
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public interface RecentSearchQueryDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(RecentSearchQuery recentSearchQuery);

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<RecentSearchQuery> recentSearchQueries);

@Query("SELECT * FROM recent_search_queries WHERE username = :username ORDER BY time DESC")
LiveData<List<RecentSearchQuery>> getAllRecentSearchQueriesLiveData(String username);

Expand All @@ -25,4 +28,4 @@ public interface RecentSearchQueryDao {

@Delete
void deleteRecentSearchQueries(RecentSearchQuery recentSearchQuery);
}
}
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@

<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="deleted_all_recent_search">Deleted all recent searches</string>
<string name="deleted_recent_search">Deleted recent search</string>
<string name="undo">Undo</string>

<string name="sort_best">Best</string>
<string name="sort_hot">Hot</string>
Expand Down Expand Up @@ -1406,5 +1409,4 @@
<string name="comment_filter_display_mode_remove_comments">Remove comments</string>
<string name="comment_filter_display_mode_fully_collapse_comments">Fully collapse comments</string>


</resources>
Loading