From d74462a66c7e975824e00dc5bd20925b1c5a4cb5 Mon Sep 17 00:00:00 2001 From: "Daniele Verducci (Slimpenguin)" Date: Sat, 24 Feb 2024 17:37:57 +0100 Subject: [PATCH] Refactored filter to support category filtering --- .../activity/main/GeofavoriteAdapter.java | 71 ++++--------------- .../fragments/GeofavoriteListFragment.java | 8 ++- .../nextcloudmaps/model/Geofavorite.java | 3 + .../utils/GeofavoritesFilter.java | 47 ++++++++++++ 4 files changed, 71 insertions(+), 58 deletions(-) create mode 100644 app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GeofavoritesFilter.java diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java index c6affa7..a2121f1 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java @@ -28,8 +28,6 @@ import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.Filter; -import android.widget.Filterable; import android.widget.ImageView; import android.widget.PopupMenu; import android.widget.TextView; @@ -42,14 +40,13 @@ import org.threeten.bp.format.DateTimeFormatter; import org.threeten.bp.format.FormatStyle; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.List; import it.danieleverducci.nextcloudmaps.R; import it.danieleverducci.nextcloudmaps.model.Geofavorite; -public class GeofavoriteAdapter extends RecyclerView.Adapter implements Filterable { +public class GeofavoriteAdapter extends RecyclerView.Adapter { public static final String TAG = "GeofavoriteAdapter"; @@ -58,12 +55,11 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter geofavoriteList = new ArrayList<>(); - private List geofavoriteListFiltered = new ArrayList<>(); + private List items = new ArrayList<>(); private int sortRule = SORT_BY_CREATED; // Contains the position of the element containing the overflow menu clicked @@ -75,15 +71,15 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter geofavoriteList) { - this.geofavoriteList = geofavoriteList; - this.geofavoriteListFiltered = new ArrayList<>(geofavoriteList); + this.items.clear(); + this.items.addAll(geofavoriteList); performSort(); notifyDataSetChanged(); } public Geofavorite get(int position) { - return geofavoriteListFiltered.get(position); + return items.get(position); } public int getSortRule() { @@ -106,7 +102,7 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter filteredGeofavorites = new ArrayList<>(); - - if (charSequence.toString().isEmpty()) { - filteredGeofavorites.addAll(geofavoriteList); - } else { - for (Geofavorite geofavorite : geofavoriteList) { - String query = charSequence.toString().toLowerCase(); - if (geofavorite.getName() != null && geofavorite.getName().toLowerCase().contains(query)) { - filteredGeofavorites.add(geofavorite); - } else if (geofavorite.getComment() != null && geofavorite.getComment().toLowerCase().contains(query)) { - filteredGeofavorites.add(geofavorite); - } - } - } - - filterResults.values = filteredGeofavorites; - return filterResults; - } - //Run on ui thread - @Override - protected void publishResults(CharSequence charSequence, FilterResults filterResults) { - geofavoriteListFiltered.clear(); - geofavoriteListFiltered.addAll((Collection) filterResults.values); - - performSort(); - notifyDataSetChanged(); - } - }; - class GeofavoriteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView tv_category, tv_title, tv_content, tv_date; ImageView bt_context_menu; @@ -211,13 +168,13 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter geofavorites = new ArrayList<>(); @Nullable @Override @@ -99,6 +102,7 @@ public class GeofavoriteListFragment extends GeofavoritesFragment implements Sor mGeofavoritesFragmentViewModel.getGeofavorites().observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(List geofavorites) { + GeofavoriteListFragment.this.geofavorites = geofavorites; geofavoriteAdapter.setGeofavoriteList(geofavorites); } }); @@ -127,7 +131,9 @@ public class GeofavoriteListFragment extends GeofavoritesFragment implements Sor } public void onSearch(String query) { - geofavoriteAdapter.getFilter().filter(query); + geofavoriteAdapter.setGeofavoriteList( + (new GeofavoritesFilter(geofavorites)).byText(query) + ); } @Override diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java index 6969e96..2d4dc88 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/model/Geofavorite.java @@ -22,6 +22,7 @@ package it.danieleverducci.nextcloudmaps.model; import android.graphics.Color; import android.net.Uri; +import android.widget.Filter; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -34,7 +35,9 @@ import org.threeten.bp.LocalDate; import org.threeten.bp.ZoneId; import java.io.Serializable; +import java.util.ArrayList; import java.util.Comparator; +import java.util.List; import it.danieleverducci.nextcloudmaps.utils.GeoUriParser; diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GeofavoritesFilter.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GeofavoritesFilter.java new file mode 100644 index 0000000..bf3f5b8 --- /dev/null +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/utils/GeofavoritesFilter.java @@ -0,0 +1,47 @@ +package it.danieleverducci.nextcloudmaps.utils; + +import java.util.ArrayList; +import java.util.List; + +import it.danieleverducci.nextcloudmaps.model.Geofavorite; + +public class GeofavoritesFilter { + List items; + public GeofavoritesFilter(List items) { + this.items = items; + } + + public List byText(String text) { + List filteredGeofavorites = new ArrayList<>(); + + if (text.isEmpty()) { + return items; + } else { + for (Geofavorite geofavorite : items) { + String query = text.toLowerCase(); + if (geofavorite.getName() != null && geofavorite.getName().toLowerCase().contains(query)) { + filteredGeofavorites.add(geofavorite); + } else if (geofavorite.getComment() != null && geofavorite.getComment().toLowerCase().contains(query)) { + filteredGeofavorites.add(geofavorite); + } + } + } + return filteredGeofavorites; + } + + public List byCategory(String category) { + List filteredGeofavorites = new ArrayList<>(); + + if (category.isEmpty()) { + return items; + } else { + for (Geofavorite geofavorite : items) { + if (geofavorite.getCategory().equals(category)) { + filteredGeofavorites.add(geofavorite); + } + } + } + return filteredGeofavorites; + + } +}