Deleting search history is now available.

This commit is contained in:
Alex Ning 2020-10-28 10:25:25 +08:00
parent fbddc73918
commit 06fdd12f0c
4 changed files with 80 additions and 2 deletions

View File

@ -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);

View File

@ -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<Recy
private List<RecentSearchQuery> 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<Recy
class RecentSearchQueryViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.recent_search_query_text_view_item_recent_search_query)
TextView recentSearchQueryTextView;
@BindView(R.id.delete_image_view_item_recent_search_query)
ImageView deleteImageView;
public RecentSearchQueryViewHolder(@NonNull View itemView) {
super(itemView);
@ -72,12 +78,17 @@ public class SearchActivityRecyclerViewAdapter extends RecyclerView.Adapter<Recy
recentSearchQueryTextView.setTextColor(primaryTextColor);
recentSearchQueryTextView.setCompoundDrawablesWithIntrinsicBounds(historyIcon, null, null, null);
deleteImageView.setImageDrawable(deleteIcon);
itemView.setOnClickListener(view -> {
if (recentSearchQueries != null && !recentSearchQueries.isEmpty()) {
itemOnClickListener.onClick(recentSearchQueries.get(getAdapterPosition()).getSearchQuery());
}
});
deleteImageView.setOnClickListener(view -> {
itemOnClickListener.onDelete(recentSearchQueries.get(getAdapterPosition()));
});
}
}
}

View File

@ -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<Void, Void, Void> {
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();
}
}
}

View File

@ -10,10 +10,21 @@
<TextView
android:id="@+id/recent_search_query_text_view_item_recent_search_query"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:fontFamily="?attr/font_family"
android:textSize="?attr/font_default"
android:drawablePadding="16dp" />
<ImageView
android:id="@+id/delete_image_view_item_recent_search_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
</LinearLayout>