Working search in both map and list view

This commit is contained in:
Daniele Verducci (Slimpenguin) 2024-02-24 18:03:11 +01:00
parent d74462a66c
commit 28258d8c15
3 changed files with 28 additions and 41 deletions

View File

@ -1,37 +1,24 @@
package it.danieleverducci.nextcloudmaps.fragments; package it.danieleverducci.nextcloudmaps.fragments;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_CATEGORY; import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_CATEGORY;
import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_CREATED; import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_CREATED;
import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_DISTANCE; import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_DISTANCE;
import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_TITLE; import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_TITLE;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatImageView; import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.card.MaterialCardView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -47,7 +34,6 @@ public class GeofavoriteListFragment extends GeofavoritesFragment implements Sor
private SwipeRefreshLayout swipeRefresh; private SwipeRefreshLayout swipeRefresh;
private GeofavoriteAdapter geofavoriteAdapter; private GeofavoriteAdapter geofavoriteAdapter;
private List<Geofavorite> geofavorites = new ArrayList<>();
@Nullable @Nullable
@Override @Override
@ -99,13 +85,6 @@ 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);
}
});
// Setup view listeners // Setup view listeners
swipeRefresh = v.findViewById(R.id.swipe_refresh); swipeRefresh = v.findViewById(R.id.swipe_refresh);
@ -130,10 +109,10 @@ public class GeofavoriteListFragment extends GeofavoritesFragment implements Sor
updateSortingIcon(sortRule); updateSortingIcon(sortRule);
} }
public void onSearch(String query) { @Override
geofavoriteAdapter.setGeofavoriteList( public void onDatasetChange(List<Geofavorite> items) {
(new GeofavoritesFilter(geofavorites)).byText(query) // Called when the items are loaded or a filtering happens
); geofavoriteAdapter.setGeofavoriteList(items);
} }
@Override @Override

View File

@ -106,15 +106,6 @@ public class GeofavoriteMapFragment extends GeofavoritesFragment implements Main
} }
} }
}); });
mGeofavoritesFragmentViewModel.getGeofavorites().observe(getViewLifecycleOwner(), new Observer<List<Geofavorite>>() {
@Override
public void onChanged(List<Geofavorite> geofavorites) {
clearAllMarkers();
for(Geofavorite gf : geofavorites)
addMarker(gf);
map.invalidate();
}
});
return v; return v;
} }
@ -126,6 +117,14 @@ public class GeofavoriteMapFragment extends GeofavoritesFragment implements Main
((MainActivity)requireActivity()).addOnGpsPermissionGrantedListener(this); ((MainActivity)requireActivity()).addOnGpsPermissionGrantedListener(this);
} }
@Override
public void onDatasetChange(List<Geofavorite> items) {
clearAllMarkers();
for(Geofavorite gf : items)
addMarker(gf);
map.invalidate();
}
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
@ -139,11 +138,6 @@ public class GeofavoriteMapFragment extends GeofavoritesFragment implements Main
); );
} }
@Override
public void onSearch(String query) {
// TODO: filter
}
@Override @Override
public void onGpsPermissionGranted() { public void onGpsPermissionGranted() {
showUserPosition(); showUserPosition();

View File

@ -31,12 +31,15 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import it.danieleverducci.nextcloudmaps.R; import it.danieleverducci.nextcloudmaps.R;
import it.danieleverducci.nextcloudmaps.activity.detail.GeofavoriteDetailActivity; import it.danieleverducci.nextcloudmaps.activity.detail.GeofavoriteDetailActivity;
import it.danieleverducci.nextcloudmaps.activity.main.GeofavoritesFragmentViewModel; import it.danieleverducci.nextcloudmaps.activity.main.GeofavoritesFragmentViewModel;
import it.danieleverducci.nextcloudmaps.activity.main.MainActivity; import it.danieleverducci.nextcloudmaps.activity.main.MainActivity;
import it.danieleverducci.nextcloudmaps.model.Geofavorite; import it.danieleverducci.nextcloudmaps.model.Geofavorite;
import it.danieleverducci.nextcloudmaps.utils.GeofavoritesFilter;
import it.danieleverducci.nextcloudmaps.utils.IntentGenerator; import it.danieleverducci.nextcloudmaps.utils.IntentGenerator;
/** /**
@ -50,6 +53,7 @@ public abstract class GeofavoritesFragment extends Fragment {
private View toolbar; private View toolbar;
private View homeToolbar; private View homeToolbar;
private SearchView searchView; private SearchView searchView;
private List<Geofavorite> geofavorites = new ArrayList<>();
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
@ -93,11 +97,21 @@ public abstract class GeofavoritesFragment extends Fragment {
@Override @Override
public boolean onQueryTextChange(String query) { public boolean onQueryTextChange(String query) {
onSearch(query); onDatasetChange(
(new GeofavoritesFilter(geofavorites)).byText(query)
);
return false; return false;
} }
}); });
mGeofavoritesFragmentViewModel.getGeofavorites().observe(getViewLifecycleOwner(), new Observer<List<Geofavorite>>() {
@Override
public void onChanged(List<Geofavorite> geofavorites) {
GeofavoritesFragment.this.geofavorites = geofavorites;
onDatasetChange(geofavorites);
}
});
searchView.setOnCloseListener(() -> { searchView.setOnCloseListener(() -> {
if (toolbar.getVisibility() == VISIBLE && TextUtils.isEmpty(searchView.getQuery())) { if (toolbar.getVisibility() == VISIBLE && TextUtils.isEmpty(searchView.getQuery())) {
updateToolbars(true); updateToolbars(true);
@ -131,7 +145,7 @@ public abstract class GeofavoritesFragment extends Fragment {
} }
abstract public void onSearch(String query); abstract public void onDatasetChange(List<Geofavorite> items);
protected void openGeofavorite(Geofavorite item) { protected void openGeofavorite(Geofavorite item) {