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.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<GeofavoriteAdapter.GeofavoriteViewHolder> implements Filterable {
|
||||
public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.GeofavoriteViewHolder> {
|
||||
|
||||
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_DISTANCE = 3;
|
||||
|
||||
private Context context;
|
||||
private ItemClickListener itemClickListener;
|
||||
private DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
|
||||
private final Context context;
|
||||
private final ItemClickListener itemClickListener;
|
||||
private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
|
||||
|
||||
private List<Geofavorite> geofavoriteList = new ArrayList<>();
|
||||
private List<Geofavorite> geofavoriteListFiltered = new ArrayList<>();
|
||||
private List<Geofavorite> 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<GeofavoriteAdapter.
|
||||
}
|
||||
|
||||
public void setGeofavoriteList(@NonNull List<Geofavorite> 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<GeofavoriteAdapter.
|
||||
|
||||
@Override
|
||||
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.setCategoryColor(
|
||||
@ -118,48 +114,9 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
|
||||
|
||||
@Override
|
||||
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 {
|
||||
TextView tv_category, tv_title, tv_content, tv_date;
|
||||
ImageView bt_context_menu;
|
||||
@ -211,13 +168,13 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
|
||||
|
||||
private void performSort() {
|
||||
if (sortRule == SORT_BY_TITLE) {
|
||||
Collections.sort(geofavoriteListFiltered, Geofavorite.ByTitleAZ);
|
||||
Collections.sort(items, Geofavorite.ByTitleAZ);
|
||||
} else if (sortRule == SORT_BY_CREATED) {
|
||||
Collections.sort(geofavoriteListFiltered, Geofavorite.ByLastCreated);
|
||||
Collections.sort(items, Geofavorite.ByLastCreated);
|
||||
} else if (sortRule == SORT_BY_CATEGORY) {
|
||||
Collections.sort(geofavoriteListFiltered, Geofavorite.ByCategory);
|
||||
Collections.sort(items, Geofavorite.ByCategory);
|
||||
} 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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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.SortingOrderDialogFragment;
|
||||
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
||||
import it.danieleverducci.nextcloudmaps.utils.GeofavoritesFilter;
|
||||
import it.danieleverducci.nextcloudmaps.utils.SettingsManager;
|
||||
|
||||
public class GeofavoriteListFragment extends GeofavoritesFragment implements SortingOrderDialogFragment.OnSortingOrderListener {
|
||||
|
||||
private SwipeRefreshLayout swipeRefresh;
|
||||
private GeofavoriteAdapter geofavoriteAdapter;
|
||||
private List<Geofavorite> geofavorites = new ArrayList<>();
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@ -99,6 +102,7 @@ public class GeofavoriteListFragment extends GeofavoritesFragment implements Sor
|
||||
mGeofavoritesFragmentViewModel.getGeofavorites().observe(getViewLifecycleOwner(), new Observer<List<Geofavorite>>() {
|
||||
@Override
|
||||
public void onChanged(List<Geofavorite> 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
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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