Commit e2f37f26 authored by Mudar Noufal's avatar Mudar Noufal

Store layer filters in sharedPrefs

Related to #31
parent 57e8521a
......@@ -27,6 +27,10 @@ import android.os.Build;
import com.google.android.gms.maps.model.LatLng;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Const {
/**
* MTL au cas ou
......@@ -129,6 +133,7 @@ public class Const {
String LANGUAGE = "prefs_language";
String PERMISSIONS = "prefs_permissions";
String UNITS_SYSTEM = "prefs_units_system";
String LAYERS_ENABLED = "prefs_layers_enabled";
// String LIST_SORT = "prefs_list_sort_by";
// String FOLLOW_LOCATION_CHANGES = "prefs_follow_location_changes";
// String LAST_UPDATE_TIME = "prefs_last_update_time";
......@@ -145,6 +150,10 @@ public class Const {
String UNITS_IMP = "imp";
// String LIST_SORT_NAME = "name";
// String LIST_SORT_DISTANCE = "distance";
Set<String> DEFAULT_LAYERS = new HashSet<>(
Arrays.asList(Const.LayerTypes.HOSPITALS,
Const.LayerTypes.AIR_CONDITIONING)
);
}
public interface RequestCodes {
......
......@@ -29,10 +29,13 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import ca.mudar.mtlaucasou.Const;
import ca.mudar.mtlaucasou.R;
import ca.mudar.mtlaucasou.model.LayerType;
import static ca.mudar.mtlaucasou.util.LogUtils.makeLogTag;
......@@ -135,6 +138,33 @@ public class UserPrefs implements
.apply();
}
public Set<String> getEnabledLayers() {
return mPrefs.getStringSet(LAYERS_ENABLED, Const.PrefsValues.DEFAULT_LAYERS);
}
public boolean isLayerEnabled(@LayerType String layerType) {
return mPrefs.getStringSet(LAYERS_ENABLED, Const.PrefsValues.DEFAULT_LAYERS)
.contains(layerType);
}
public void setLayerEnabled(@LayerType String layerType, boolean enabled) {
// We need to make a copy of the hashset, not just get a reference
final Set<String> enabledLayers = new HashSet<>(mPrefs.getStringSet(LAYERS_ENABLED,
Const.PrefsValues.DEFAULT_LAYERS));
boolean result = false;
if (enabled && !enabledLayers.contains(layerType)) {
result = enabledLayers.add(layerType);
} else if (!enabled) {
result = enabledLayers.remove(layerType);
}
if (result) {
edit().putStringSet(LAYERS_ENABLED, enabledLayers)
.apply();
}
}
/**
* Check if the API dataset has updates for the requested dataset item
*
......
......@@ -39,8 +39,10 @@ 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.MapType;
import ca.mudar.mtlaucasou.util.MapUtils;
......@@ -64,6 +66,8 @@ public class MapLayersManager implements
private boolean mMapTypeHasMenu;
@ColorInt
private int mMapTypeColor;
@ColorInt
private int mNormalColor;
public MapLayersManager(@NonNull Context context, @NonNull FloatingActionMenu menu) {
mContext = context;
......@@ -75,6 +79,8 @@ public class MapLayersManager implements
mActiveLayers = new HashSet<>();
mNormalColor = ContextCompat.getColor(mContext, R.color.fab_menu_item_color_normal);
// The menu items
mAirConditioningFAB = (FloatingActionButton) mMenuFAB.findViewById(R.id.fab_air_conditioning);
mPoolsFAB = (FloatingActionButton) mMenuFAB.findViewById(R.id.fab_pools);
......@@ -83,6 +89,7 @@ public class MapLayersManager implements
mHospitalsFAB = (FloatingActionButton) mMenuFAB.findViewById(R.id.fab_hospitals);
mClscFAB = (FloatingActionButton) mMenuFAB.findViewById(R.id.fab_clsc);
setupInitialValues(UserPrefs.getInstance(context));
setupMenuItemsListeners();
}
......@@ -142,10 +149,10 @@ public class MapLayersManager implements
public void onClick(final View view) {
if (view instanceof FloatingActionButton) {
final boolean isActivated = !view.isActivated(); // The new (toggled) value
@ColorInt final int color = isActivated ? mMapTypeColor :
ContextCompat.getColor(mContext, R.color.fab_menu_item_color_normal);
view.setActivated(isActivated);
((FloatingActionButton) view).setColorNormal(color);
setMenuItemState((FloatingActionButton) view, isActivated);
UserPrefs.getInstance(mContext).setLayerEnabled(
MapUtils.getFilterItemLayerType(view.getId()),
isActivated);
}
}
......@@ -212,6 +219,22 @@ public class MapLayersManager implements
mMenuFAB.setMenuButtonColorPressed(mMapTypeColor);
}
private void setupInitialValues(UserPrefs prefs) {
final Set<String> enabledLayers = prefs.getEnabledLayers();
mMapTypeColor = MapUtils.getMapTypeColor(mContext, MapTypes.HEAT_WAVE);
setMenuItemState(mAirConditioningFAB, enabledLayers.contains(Const.LayerTypes.AIR_CONDITIONING));
setMenuItemState(mPoolsFAB, enabledLayers.contains(Const.LayerTypes.POOLS));
setMenuItemState(mWadingPoolsFAB, enabledLayers.contains(Const.LayerTypes.WADING_POOLS));
setMenuItemState(mPlayFountainsFAB, enabledLayers.contains(Const.LayerTypes.PLAY_FOUNTAINS));
mMapTypeColor = MapUtils.getMapTypeColor(mContext, MapTypes.HEALTH);
setMenuItemState(mHospitalsFAB, enabledLayers.contains(Const.LayerTypes.HOSPITALS));
setMenuItemState(mClscFAB, enabledLayers.contains(Const.LayerTypes.CLSC));
mMapTypeColor = mNormalColor;
}
private void setupMenuItemsListeners() {
mAirConditioningFAB.setOnClickListener(this);
mPoolsFAB.setOnClickListener(this);
......@@ -220,4 +243,10 @@ public class MapLayersManager implements
mHospitalsFAB.setOnClickListener(this);
mClscFAB.setOnClickListener(this);
}
private void setMenuItemState(FloatingActionButton fab, boolean activated) {
@ColorInt final int color = activated ? mMapTypeColor : mNormalColor;
fab.setActivated(activated);
fab.setColorNormal(color);
}
}
......@@ -27,6 +27,7 @@ import android.content.Context;
import android.location.Location;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
......@@ -52,6 +53,7 @@ import java.util.List;
import ca.mudar.mtlaucasou.Const;
import ca.mudar.mtlaucasou.R;
import ca.mudar.mtlaucasou.model.LayerType;
import ca.mudar.mtlaucasou.model.MapType;
import ca.mudar.mtlaucasou.model.Placemark;
......@@ -92,7 +94,7 @@ public class MapUtils {
* Get the app's colors for each section. Used for ProgressBar
*
* @param context Context to resolve resources
* @param type Selected map type {fire_halls|spvm_stations|water_supplies|emergency_hostels|health}
* @param type Selected map type {fire_halls|spvm_stations|water_supplies|emergency_hostels|health}
* @return the section's color
*/
@ColorInt
......@@ -120,6 +122,26 @@ public class MapUtils {
return ContextCompat.getColor(context, color);
}
@LayerType
public static String getFilterItemLayerType(@IdRes int id) {
if (id == R.id.fab_air_conditioning) {
return Const.LayerTypes.AIR_CONDITIONING;
} else if (id == R.id.fab_pools) {
return Const.LayerTypes.POOLS;
} else if (id == R.id.fab_wading_pools) {
return Const.LayerTypes.WADING_POOLS;
} else if (id == R.id.fab_play_fountains) {
return Const.LayerTypes.PLAY_FOUNTAINS;
} else if (id == R.id.fab_hospitals) {
return Const.LayerTypes.HOSPITALS;
} else if (id == R.id.fab_clsc) {
return Const.LayerTypes.CLSC;
}
return null;
}
/**
* Clean the HTML description provided by the city's data. Also removes duplicate title.
*
......
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