Refactored filter to support category filtering
This commit is contained in:
parent
572095a7c3
commit
d74462a66c
@ -28,8 +28,6 @@ import android.view.LayoutInflater;
|
|||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Filter;
|
|
||||||
import android.widget.Filterable;
|
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.PopupMenu;
|
import android.widget.PopupMenu;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
@ -42,14 +40,13 @@ import org.threeten.bp.format.DateTimeFormatter;
|
|||||||
import org.threeten.bp.format.FormatStyle;
|
import org.threeten.bp.format.FormatStyle;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import it.danieleverducci.nextcloudmaps.R;
|
import it.danieleverducci.nextcloudmaps.R;
|
||||||
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
||||||
|
|
||||||
public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.GeofavoriteViewHolder> implements Filterable {
|
public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.GeofavoriteViewHolder> {
|
||||||
|
|
||||||
public static final String TAG = "GeofavoriteAdapter";
|
public static final String TAG = "GeofavoriteAdapter";
|
||||||
|
|
||||||
@ -58,12 +55,11 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
|
|||||||
public static final int SORT_BY_CATEGORY = 2;
|
public static final int SORT_BY_CATEGORY = 2;
|
||||||
public static final int SORT_BY_DISTANCE = 3;
|
public static final int SORT_BY_DISTANCE = 3;
|
||||||
|
|
||||||
private Context context;
|
private final Context context;
|
||||||
private ItemClickListener itemClickListener;
|
private final ItemClickListener itemClickListener;
|
||||||
private DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
|
private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
|
||||||
|
|
||||||
private List<Geofavorite> geofavoriteList = new ArrayList<>();
|
private List<Geofavorite> items = new ArrayList<>();
|
||||||
private List<Geofavorite> geofavoriteListFiltered = new ArrayList<>();
|
|
||||||
private int sortRule = SORT_BY_CREATED;
|
private int sortRule = SORT_BY_CREATED;
|
||||||
|
|
||||||
// Contains the position of the element containing the overflow menu clicked
|
// Contains the position of the element containing the overflow menu clicked
|
||||||
@ -75,15 +71,15 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setGeofavoriteList(@NonNull List<Geofavorite> geofavoriteList) {
|
public void setGeofavoriteList(@NonNull List<Geofavorite> geofavoriteList) {
|
||||||
this.geofavoriteList = geofavoriteList;
|
this.items.clear();
|
||||||
this.geofavoriteListFiltered = new ArrayList<>(geofavoriteList);
|
this.items.addAll(geofavoriteList);
|
||||||
|
|
||||||
performSort();
|
performSort();
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Geofavorite get(int position) {
|
public Geofavorite get(int position) {
|
||||||
return geofavoriteListFiltered.get(position);
|
return items.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSortRule() {
|
public int getSortRule() {
|
||||||
@ -106,7 +102,7 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull GeofavoriteViewHolder holder, int position) {
|
public void onBindViewHolder(@NonNull GeofavoriteViewHolder holder, int position) {
|
||||||
Geofavorite geofavorite = geofavoriteListFiltered.get(position);
|
Geofavorite geofavorite = items.get(position);
|
||||||
|
|
||||||
holder.tv_category.setText(geofavorite.categoryLetter());
|
holder.tv_category.setText(geofavorite.categoryLetter());
|
||||||
holder.setCategoryColor(
|
holder.setCategoryColor(
|
||||||
@ -118,48 +114,9 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return geofavoriteListFiltered.size();
|
return items.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Filter getFilter() {
|
|
||||||
return filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
Filter filter = new Filter() {
|
|
||||||
@Override
|
|
||||||
// Run on Background thread.
|
|
||||||
protected FilterResults performFiltering(CharSequence charSequence) {
|
|
||||||
FilterResults filterResults = new FilterResults();
|
|
||||||
List <Geofavorite> 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<? extends Geofavorite>) filterResults.values);
|
|
||||||
|
|
||||||
performSort();
|
|
||||||
notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class GeofavoriteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
class GeofavoriteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||||
TextView tv_category, tv_title, tv_content, tv_date;
|
TextView tv_category, tv_title, tv_content, tv_date;
|
||||||
ImageView bt_context_menu;
|
ImageView bt_context_menu;
|
||||||
@ -211,13 +168,13 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
|
|||||||
|
|
||||||
private void performSort() {
|
private void performSort() {
|
||||||
if (sortRule == SORT_BY_TITLE) {
|
if (sortRule == SORT_BY_TITLE) {
|
||||||
Collections.sort(geofavoriteListFiltered, Geofavorite.ByTitleAZ);
|
Collections.sort(items, Geofavorite.ByTitleAZ);
|
||||||
} else if (sortRule == SORT_BY_CREATED) {
|
} else if (sortRule == SORT_BY_CREATED) {
|
||||||
Collections.sort(geofavoriteListFiltered, Geofavorite.ByLastCreated);
|
Collections.sort(items, Geofavorite.ByLastCreated);
|
||||||
} else if (sortRule == SORT_BY_CATEGORY) {
|
} else if (sortRule == SORT_BY_CATEGORY) {
|
||||||
Collections.sort(geofavoriteListFiltered, Geofavorite.ByCategory);
|
Collections.sort(items, Geofavorite.ByCategory);
|
||||||
} else if (sortRule == SORT_BY_DISTANCE) {
|
} else if (sortRule == SORT_BY_DISTANCE) {
|
||||||
Collections.sort(geofavoriteListFiltered, Geofavorite.ByDistance);
|
Collections.sort(items, Geofavorite.ByDistance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|||||||
|
|
||||||
import com.google.android.material.card.MaterialCardView;
|
import com.google.android.material.card.MaterialCardView;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import it.danieleverducci.nextcloudmaps.R;
|
import it.danieleverducci.nextcloudmaps.R;
|
||||||
@ -39,12 +40,14 @@ import it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter;
|
|||||||
import it.danieleverducci.nextcloudmaps.activity.main.MainActivity;
|
import it.danieleverducci.nextcloudmaps.activity.main.MainActivity;
|
||||||
import it.danieleverducci.nextcloudmaps.activity.main.SortingOrderDialogFragment;
|
import it.danieleverducci.nextcloudmaps.activity.main.SortingOrderDialogFragment;
|
||||||
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
||||||
|
import it.danieleverducci.nextcloudmaps.utils.GeofavoritesFilter;
|
||||||
import it.danieleverducci.nextcloudmaps.utils.SettingsManager;
|
import it.danieleverducci.nextcloudmaps.utils.SettingsManager;
|
||||||
|
|
||||||
public class GeofavoriteListFragment extends GeofavoritesFragment implements SortingOrderDialogFragment.OnSortingOrderListener {
|
public class GeofavoriteListFragment extends GeofavoritesFragment implements SortingOrderDialogFragment.OnSortingOrderListener {
|
||||||
|
|
||||||
private SwipeRefreshLayout swipeRefresh;
|
private SwipeRefreshLayout swipeRefresh;
|
||||||
private GeofavoriteAdapter geofavoriteAdapter;
|
private GeofavoriteAdapter geofavoriteAdapter;
|
||||||
|
private List<Geofavorite> geofavorites = new ArrayList<>();
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
@ -99,6 +102,7 @@ public class GeofavoriteListFragment extends GeofavoritesFragment implements Sor
|
|||||||
mGeofavoritesFragmentViewModel.getGeofavorites().observe(getViewLifecycleOwner(), new Observer<List<Geofavorite>>() {
|
mGeofavoritesFragmentViewModel.getGeofavorites().observe(getViewLifecycleOwner(), new Observer<List<Geofavorite>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onChanged(List<Geofavorite> geofavorites) {
|
public void onChanged(List<Geofavorite> geofavorites) {
|
||||||
|
GeofavoriteListFragment.this.geofavorites = geofavorites;
|
||||||
geofavoriteAdapter.setGeofavoriteList(geofavorites);
|
geofavoriteAdapter.setGeofavoriteList(geofavorites);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -127,7 +131,9 @@ public class GeofavoriteListFragment extends GeofavoritesFragment implements Sor
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onSearch(String query) {
|
public void onSearch(String query) {
|
||||||
geofavoriteAdapter.getFilter().filter(query);
|
geofavoriteAdapter.setGeofavoriteList(
|
||||||
|
(new GeofavoritesFilter(geofavorites)).byText(query)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -22,6 +22,7 @@ package it.danieleverducci.nextcloudmaps.model;
|
|||||||
|
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.widget.Filter;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
@ -34,7 +35,9 @@ import org.threeten.bp.LocalDate;
|
|||||||
import org.threeten.bp.ZoneId;
|
import org.threeten.bp.ZoneId;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import it.danieleverducci.nextcloudmaps.utils.GeoUriParser;
|
import it.danieleverducci.nextcloudmaps.utils.GeoUriParser;
|
||||||
|
|
||||||
|
@ -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<Geofavorite> items;
|
||||||
|
public GeofavoritesFilter(List<Geofavorite> items) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Geofavorite> byText(String text) {
|
||||||
|
List<Geofavorite> 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<Geofavorite> byCategory(String category) {
|
||||||
|
List<Geofavorite> filteredGeofavorites = new ArrayList<>();
|
||||||
|
|
||||||
|
if (category.isEmpty()) {
|
||||||
|
return items;
|
||||||
|
} else {
|
||||||
|
for (Geofavorite geofavorite : items) {
|
||||||
|
if (geofavorite.getCategory().equals(category)) {
|
||||||
|
filteredGeofavorites.add(geofavorite);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredGeofavorites;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user