From 25f7b05fd022dddad361933bac3298119eeba2fb Mon Sep 17 00:00:00 2001 From: "Daniele Verducci (Slimpenguin)" Date: Tue, 27 Feb 2024 08:59:37 +0100 Subject: [PATCH] WIP implementing filtering by category --- .../main/GeofavoritesFragmentViewModel.java | 5 +++ .../fragments/GeofavoritesFragment.java | 33 +++++++++++++++++++ app/src/main/res/drawable/ic_filter.xml | 5 +++ app/src/main/res/drawable/ic_filter_off.xml | 6 ++++ app/src/main/res/layout/app_toolbar.xml | 9 +++++ app/src/main/res/values-it/strings.xml | 2 ++ app/src/main/res/values/strings.xml | 2 ++ 7 files changed, 62 insertions(+) create mode 100644 app/src/main/res/drawable/ic_filter.xml create mode 100644 app/src/main/res/drawable/ic_filter_off.xml diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoritesFragmentViewModel.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoritesFragmentViewModel.java index 9088a5e..09fb917 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoritesFragmentViewModel.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoritesFragmentViewModel.java @@ -5,6 +5,7 @@ import android.content.Context; import androidx.lifecycle.LiveData; import androidx.lifecycle.ViewModel; +import java.util.HashSet; import java.util.List; import it.danieleverducci.nextcloudmaps.model.Geofavorite; @@ -26,6 +27,10 @@ public class GeofavoritesFragmentViewModel extends ViewModel { mRepo.updateGeofavorites(); } + public LiveData> getCategories(){ + return mRepo.getCategories(); + } + public void deleteGeofavorite(Geofavorite geofav) { mRepo.deleteGeofavorite(geofav); } diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoritesFragment.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoritesFragment.java index 6d83aed..e8c72f8 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoritesFragment.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoritesFragment.java @@ -10,6 +10,7 @@ import android.os.Handler; import android.text.TextUtils; import android.util.Log; import android.view.View; +import android.widget.ImageButton; import android.widget.Toast; import androidx.annotation.NonNull; @@ -32,6 +33,8 @@ import com.squareup.picasso.Picasso; import java.io.File; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; import it.danieleverducci.nextcloudmaps.R; @@ -53,7 +56,9 @@ public abstract class GeofavoritesFragment extends Fragment { private View toolbar; private View homeToolbar; private SearchView searchView; + private ImageButton filterButton; private List geofavorites = new ArrayList<>(); + private HashSet categories = new HashSet<>(); @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -87,6 +92,8 @@ public abstract class GeofavoritesFragment extends Fragment { // Setup toolbar/searchbar toolbar = view.findViewById(R.id.toolbar); homeToolbar = view.findViewById(R.id.home_toolbar); + filterButton = view.findViewById(R.id.search_filter); + filterButton.setOnClickListener(v -> showCategoryFilterDialog()); searchView = view.findViewById(R.id.search_view); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @@ -111,6 +118,12 @@ public abstract class GeofavoritesFragment extends Fragment { onDatasetChange(geofavorites); } }); + mGeofavoritesFragmentViewModel.getCategories().observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(HashSet categories) { + GeofavoritesFragment.this.categories = categories; + } + }); searchView.setOnCloseListener(() -> { if (toolbar.getVisibility() == VISIBLE && TextUtils.isEmpty(searchView.getQuery())) { @@ -205,6 +218,26 @@ public abstract class GeofavoritesFragment extends Fragment { ad.show(); } + private void showCategoryFilterDialog() { + if (categories.isEmpty()) { + Toast.makeText(requireContext(), R.string.filtering_unavailable, Toast.LENGTH_SHORT).show(); + return; + } + + AlertDialog.Builder builder = new AlertDialog.Builder(requireContext()); + builder.setTitle(R.string.filtering_dialog_title); + String[] categoryNames = categories.toArray(new String[categories.size()]); + builder.setItems(categoryNames, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String category = categoryNames[which]; + Log.d(TAG, "Selected category " + category); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + private void updateToolbars(boolean disableSearch) { homeToolbar.setVisibility(disableSearch ? VISIBLE : GONE); toolbar.setVisibility(disableSearch ? GONE : VISIBLE); diff --git a/app/src/main/res/drawable/ic_filter.xml b/app/src/main/res/drawable/ic_filter.xml new file mode 100644 index 0000000..3925260 --- /dev/null +++ b/app/src/main/res/drawable/ic_filter.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_filter_off.xml b/app/src/main/res/drawable/ic_filter_off.xml new file mode 100644 index 0000000..eda907f --- /dev/null +++ b/app/src/main/res/drawable/ic_filter_off.xml @@ -0,0 +1,6 @@ + + + + diff --git a/app/src/main/res/layout/app_toolbar.xml b/app/src/main/res/layout/app_toolbar.xml index 0685597..c030538 100644 --- a/app/src/main/res/layout/app_toolbar.xml +++ b/app/src/main/res/layout/app_toolbar.xml @@ -71,6 +71,15 @@ android:textSize="16sp" android:text="@string/search_in_all"/> + + + Vuoi cambiare account? Geosegnalibro eliminato Impossibile ottenere la lista dei geosegnalibri + Il filtro per categoria non รจ disponibile al momento + Filtra per categoria Ordina per diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 85685cd..52191dd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -42,6 +42,8 @@ Do you want to switch account? Geofavorite deleted Unable to obtain geofavorites list + Category filtering is unavailable at the moment + Filter by category Sort by