Commit d0185f1e authored by Mudar Noufal's avatar Mudar Noufal

Update map layers on filters change

fix #31
parent f27828af
......@@ -25,9 +25,13 @@ package ca.mudar.mtlaucasou.data;
import com.google.android.gms.maps.model.LatLngBounds;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import ca.mudar.mtlaucasou.Const;
import ca.mudar.mtlaucasou.Const.LayerTypes;
import ca.mudar.mtlaucasou.model.LayerType;
import ca.mudar.mtlaucasou.model.MapType;
import ca.mudar.mtlaucasou.model.RealmPlacemark;
......@@ -41,6 +45,15 @@ import static ca.mudar.mtlaucasou.util.LogUtils.makeLogTag;
public class RealmQueries {
private static final String TAG = makeLogTag("RealmQueries");
private static final Set<String> HEAT_WAVE_LAYERS = new HashSet<>(Arrays.asList(
LayerTypes.AIR_CONDITIONING,
LayerTypes.POOLS,
LayerTypes.WADING_POOLS,
LayerTypes.PLAY_FOUNTAINS));
private static final Set<String> HEALTH_LAYERS = new HashSet<>(Arrays.asList(
LayerTypes.HOSPITALS,
LayerTypes.AIR_CONDITIONING));
/**
* Delete data from the Realm db
......@@ -52,12 +65,12 @@ public class RealmQueries {
realm.beginTransaction();
final RealmQuery query = realm.where(RealmPlacemark.class);
if (Const.LayerTypes._HEAT_WAVE_MIXED.equals(layerType)) {
if (LayerTypes._HEAT_WAVE_MIXED.equals(layerType)) {
// The `water_supplies` endpoint provides 3 layerTypes we need to delete
query.in(RealmPlacemark.FIELD_LAYER_TYPE, new String[]{
Const.LayerTypes.POOLS,
Const.LayerTypes.WADING_POOLS,
Const.LayerTypes.PLAY_FOUNTAINS
LayerTypes.POOLS,
LayerTypes.WADING_POOLS,
LayerTypes.PLAY_FOUNTAINS
});
} else {
query.equalTo(RealmPlacemark.FIELD_LAYER_TYPE, layerType);
......@@ -96,6 +109,38 @@ public class RealmQueries {
}
}
/**
* Get all Placemarks for requested mapType and selected layers
*
* @param realm
* @param mapType
* @param layers
* @return
*/
public static RealmQuery<RealmPlacemark> queryPlacemarksByMapType(Realm realm,
@MapType String mapType,
@LayerType Set<String> layers) {
final RealmQuery<RealmPlacemark> query = queryPlacemarksByMapType(realm, mapType);
if (layers != null) {
Set<String> filterLayers = null;
if (Const.MapTypes.HEAT_WAVE.equals(mapType)) {
filterLayers = new HashSet<>(HEAT_WAVE_LAYERS);
filterLayers.retainAll(layers);
} else if (Const.MapTypes.HEALTH.equals(mapType)) {
filterLayers = new HashSet<>(HEALTH_LAYERS);
filterLayers.retainAll(layers);
}
if (filterLayers != null && filterLayers.size() > 0) {
return query.in(RealmPlacemark.FIELD_LAYER_TYPE,
filterLayers.toArray(new String[filterLayers.size()]));
}
}
return query;
}
/**
* Get all Placemarks for requested mapType
*
......@@ -103,7 +148,7 @@ public class RealmQueries {
* @param mapType
* @return
*/
public static RealmQuery<RealmPlacemark> queryPlacemarksByMapType(Realm realm, @MapType String mapType) {
private static RealmQuery<RealmPlacemark> queryPlacemarksByMapType(Realm realm, @MapType String mapType) {
return realm
.where(RealmPlacemark.class)
.equalTo(RealmPlacemark.FIELD_MAP_TYPE, mapType);
......
......@@ -78,7 +78,8 @@ import static ca.mudar.mtlaucasou.util.LogUtils.makeLogTag;
public class MainActivity extends BaseActivity implements
OnMapReadyCallback,
SearchResultsManager.MapUpdatesListener,
LocationUpdatesManager.LocationUpdatesCallbacks {
LocationUpdatesManager.LocationUpdatesCallbacks,
MapLayersManager.LayersFilterCallbacks {
private static final String TAG = makeLogTag("MainActivity");
private static final long BOTTOM_BAR_ANIM_DURATION = 200L; // 200ms
......@@ -239,7 +240,7 @@ public class MainActivity extends BaseActivity implements
}
});
mLayersManager = new MapLayersManager(this, (FloatingActionMenu) findViewById(R.id.fab_menu));
mLayersManager = new MapLayersManager(this, (FloatingActionMenu) findViewById(R.id.fab_menu), this);
}
/**
......@@ -356,8 +357,10 @@ public class MainActivity extends BaseActivity implements
// First, query the Realm db for the current mapType
final RealmResults<RealmPlacemark> realmPlacemarks = RealmQueries
.queryPlacemarksByMapType(mRealm, type)
.findAll();
.queryPlacemarksByMapType(mRealm,
type,
UserPrefs.getInstance(this).getEnabledLayers()
).findAll();
if (realmPlacemarks.size() > 0) {
// Has cached data
......@@ -460,4 +463,22 @@ public class MainActivity extends BaseActivity implements
public void onLocationSettingsActivityResult(int resultCode, Intent data) {
mLocationManger.onLocationSettingsResult(resultCode, data);
}
/**
* MapLayersManager.LayersFilterCallbacks
*/
@Override
public void onFiltersChange() {
MapUtils.clearMap(vMap);
}
/**
* MapLayersManager.LayersFilterCallbacks
*/
@Override
public void onFiltersApply() {
toggleProgressBar(true);
loadMapData(mMapType);
}
}
......@@ -36,13 +36,13 @@ import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import java.util.HashSet;
import java.util.Set;
import ca.mudar.mtlaucasou.Const;
import ca.mudar.mtlaucasou.Const.MapTypes;
import ca.mudar.mtlaucasou.R;
import ca.mudar.mtlaucasou.data.UserPrefs;
import ca.mudar.mtlaucasou.model.LayerType;
import ca.mudar.mtlaucasou.model.MapType;
import ca.mudar.mtlaucasou.util.MapUtils;
......@@ -51,10 +51,12 @@ import static ca.mudar.mtlaucasou.util.LogUtils.makeLogTag;
public class MapLayersManager implements
GoogleMap.OnMapClickListener,
GoogleMap.OnMarkerClickListener,
View.OnClickListener {
View.OnClickListener,
FloatingActionMenu.OnMenuToggleListener {
private static final String TAG = makeLogTag("MapLayersManager");
private final Context mContext;
private final LayersFilterCallbacks mListener;
private final FloatingActionMenu mMenuFAB;
private final FloatingActionButton mAirConditioningFAB;
private final FloatingActionButton mPoolsFAB;
......@@ -62,23 +64,24 @@ public class MapLayersManager implements
private final FloatingActionButton mPlayFountainsFAB;
private final FloatingActionButton mHospitalsFAB;
private final FloatingActionButton mClscFAB;
private Set<String> mActiveLayers;
private boolean mMapTypeHasMenu;
private boolean mHasChangedFilters;
@ColorInt
private int mMapTypeColor;
@ColorInt
private int mNormalColor;
public MapLayersManager(@NonNull Context context, @NonNull FloatingActionMenu menu) {
public MapLayersManager(@NonNull Context context, @NonNull FloatingActionMenu menu, LayersFilterCallbacks listener) {
mContext = context;
mListener = listener;
mMenuFAB = menu;
mMenuFAB.setOnMenuToggleListener(this);
mMenuFAB.setIconAnimated(false);
mMenuFAB.hideMenu(false);
ViewCompat.setElevation(mMenuFAB,
mContext.getResources().getDimensionPixelSize(R.dimen.fab_menu_elevation));
mActiveLayers = new HashSet<>();
mNormalColor = ContextCompat.getColor(mContext, R.color.fab_menu_item_color_normal);
// The menu items
......@@ -94,11 +97,11 @@ public class MapLayersManager implements
}
/**
* Provide reference to the GoogleMap to setup click listeners
* Sets map and markers click listeners that toggle the menu
*
* @param map The GoogleMap
*/
public void setMap(GoogleMap map) {
public void setMap(@NonNull GoogleMap map) {
map.setOnMapClickListener(this);
map.setOnMarkerClickListener(this);
}
......@@ -141,6 +144,7 @@ public class MapLayersManager implements
}
/**
* Updates menu item colors, sharedPrefs and toggles map data
* Implements View.OnClickListener
*
* @param view
......@@ -149,10 +153,28 @@ public class MapLayersManager implements
public void onClick(final View view) {
if (view instanceof FloatingActionButton) {
final boolean isActivated = !view.isActivated(); // The new (toggled) value
// Update layout
setMenuItemState((FloatingActionButton) view, isActivated);
// Updates userPrefs
UserPrefs.getInstance(mContext).setLayerEnabled(
MapUtils.getFilterItemLayerType(view.getId()),
isActivated);
// Notify map of layer changes (to clear map)
mHasChangedFilters = true;
mListener.onFiltersChange();
}
}
/**
* Update the map data only when closing the filter menu, for smoother interaction.
* Implements FloatingActionMenu.OnMenuToggleListener
*
* @param opened
*/
@Override
public void onMenuToggle(boolean opened) {
if (!opened && mHasChangedFilters && mListener != null) {
mListener.onFiltersApply();
}
}
......@@ -164,6 +186,7 @@ public class MapLayersManager implements
*/
public boolean toggleFilterMenu(@MapType String type) {
mMapTypeHasMenu = MapTypes.HEAT_WAVE.equals(type) || MapTypes.HEALTH.equals(type);
mHasChangedFilters = false;
toggleWaterSupplyFilterItems(MapTypes.HEAT_WAVE.equals(type));
toggleHospitalsFilterItems(MapTypes.HEALTH.equals(type));
......@@ -220,7 +243,7 @@ public class MapLayersManager implements
}
private void setupInitialValues(UserPrefs prefs) {
final Set<String> enabledLayers = prefs.getEnabledLayers();
final @LayerType Set<String> enabledLayers = prefs.getEnabledLayers();
mMapTypeColor = MapUtils.getMapTypeColor(mContext, MapTypes.HEAT_WAVE);
setMenuItemState(mAirConditioningFAB, enabledLayers.contains(Const.LayerTypes.AIR_CONDITIONING));
......@@ -249,4 +272,10 @@ public class MapLayersManager implements
fab.setActivated(activated);
fab.setColorNormal(color);
}
public interface LayersFilterCallbacks {
void onFiltersChange();
void onFiltersApply();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment