Commit b6184dd7 authored by Mudar Noufal's avatar Mudar Noufal

API parsing issues fixed

related to dataType vs layerType refactoring
parent 09599fda
......@@ -55,6 +55,7 @@ public class Const {
String POOLS = "pools";
String WADING_POOLS = "wading_pools";
String PLAY_FOUNTAINS = "play_fountains";
String _HEAT_WAVE_MIXED = "water_supplies";
// Health x2
String HOSPITALS = "hospitals";
String CLSC = "clsc";
......@@ -83,8 +84,7 @@ public class Const {
String TYPE_WADING_POOLS = "pataugeoire";
String TYPE_POOLS_EXT = "piscine-ext";
String TYPE_POOLS_INT = "piscine-int";
String TYPE_HOSPITALS = "hopital";
String TYPE_CLSC = "clsc";
String TYPE_BEACH = "plage";
}
public interface BundleKeys {
......
......@@ -27,16 +27,20 @@ import com.google.android.gms.maps.model.LatLngBounds;
import java.util.List;
import ca.mudar.mtlaucasou.Const;
import ca.mudar.mtlaucasou.model.LayerType;
import ca.mudar.mtlaucasou.model.MapType;
import ca.mudar.mtlaucasou.model.RealmPlacemark;
import ca.mudar.mtlaucasou.model.geojson.PointsFeature;
import ca.mudar.mtlaucasou.model.LayerType;
import io.realm.Case;
import io.realm.Realm;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import static ca.mudar.mtlaucasou.util.LogUtils.makeLogTag;
public class RealmQueries {
private static final String TAG = makeLogTag("RealmQueries");
/**
* Delete data from the Realm db
......@@ -47,9 +51,18 @@ public class RealmQueries {
public static void clearMapData(Realm realm, @LayerType String layerType) {
realm.beginTransaction();
realm.where(RealmPlacemark.class)
.equalTo(RealmPlacemark.FIELD_LAYER_TYPE, layerType)
.findAll()
final RealmQuery query = realm.where(RealmPlacemark.class);
if (Const.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
});
} else {
query.equalTo(RealmPlacemark.FIELD_LAYER_TYPE, layerType);
}
query.findAll()
.deleteAllFromRealm();
realm.commitTransaction();
......@@ -65,7 +78,8 @@ public class RealmQueries {
* @param transaction
*/
public static void cacheMapData(Realm realm, List<PointsFeature> pointsFeatures,
@MapType String mapType, @LayerType String layerType, boolean transaction) {
@MapType String mapType, @LayerType String layerType,
boolean transaction) {
if (transaction) {
realm.beginTransaction();
}
......
......@@ -40,6 +40,7 @@ import ca.mudar.mtlaucasou.Const;
Const.LayerTypes.POOLS,
Const.LayerTypes.WADING_POOLS,
Const.LayerTypes.PLAY_FOUNTAINS,
Const.LayerTypes._HEAT_WAVE_MIXED,
// Health x2
Const.LayerTypes.HOSPITALS,
Const.LayerTypes.CLSC
......
......@@ -23,10 +23,9 @@
package ca.mudar.mtlaucasou.model;
import android.text.TextUtils;
import com.google.android.gms.maps.model.LatLng;
import ca.mudar.mtlaucasou.Const;
import ca.mudar.mtlaucasou.model.geojson.PointsFeature;
import ca.mudar.mtlaucasou.util.ApiDataUtils;
import io.realm.RealmObject;
......@@ -161,10 +160,11 @@ public class RealmPlacemark extends RealmObject implements
}
public Builder layerType(@LayerType String layerType, String dataType) {
if (!TextUtils.isEmpty(layerType)) {
this.layerType = layerType;
if (Const.LayerTypes._HEAT_WAVE_MIXED.equals(layerType)) {
// water_supplies dataset is the only one with mixed layers
this.layerType = ApiDataUtils.getPlacemarkLayerType(dataType);
} else {
this.layerType = ApiDataUtils.getLayerType(dataType, mapType);
this.layerType = layerType;
}
return this;
......
......@@ -27,6 +27,7 @@ import com.google.gson.annotations.SerializedName;
import java.util.Date;
import ca.mudar.mtlaucasou.model.LayerType;
import ca.mudar.mtlaucasou.model.MapType;
public class Attributes {
......@@ -34,7 +35,8 @@ public class Attributes {
private Date created;
private Date updated;
@SerializedName("type")
private String dataType;
@LayerType
private String layerType;
@MapType
private String mapType;
......@@ -50,8 +52,9 @@ public class Attributes {
return updated;
}
public String getDataType() {
return dataType;
@LayerType
public String getLayerType() {
return layerType;
}
@MapType
......
......@@ -42,12 +42,12 @@ import ca.mudar.mtlaucasou.R;
import ca.mudar.mtlaucasou.api.ApiClient;
import ca.mudar.mtlaucasou.data.RealmQueries;
import ca.mudar.mtlaucasou.data.UserPrefs;
import ca.mudar.mtlaucasou.model.LayerType;
import ca.mudar.mtlaucasou.model.MapType;
import ca.mudar.mtlaucasou.model.geojson.PointsFeatureCollection;
import ca.mudar.mtlaucasou.model.jsonapi.Attributes;
import ca.mudar.mtlaucasou.model.jsonapi.DataItem;
import ca.mudar.mtlaucasou.model.jsonapi.HelloApi;
import ca.mudar.mtlaucasou.model.LayerType;
import ca.mudar.mtlaucasou.util.ApiDataUtils;
import ca.mudar.mtlaucasou.util.LogUtils;
import io.realm.Realm;
......@@ -91,7 +91,7 @@ public class SyncService extends IntentService {
importLocalData(R.raw.fire_halls, MapTypes.FIRE_HALLS, LayerTypes.FIRE_HALLS);
importLocalData(R.raw.spvm_stations, MapTypes.SPVM_STATIONS, LayerTypes.SPVM_STATIONS);
importLocalData(R.raw.water_supplies, MapTypes.HEAT_WAVE, null);
importLocalData(R.raw.water_supplies, MapTypes.HEAT_WAVE, LayerTypes._HEAT_WAVE_MIXED);
importLocalData(R.raw.air_conditioning, MapTypes.HEAT_WAVE, LayerTypes.AIR_CONDITIONING);
importLocalData(R.raw.emergency_hostels, MapTypes.EMERGENCY_HOSTELS, LayerTypes.EMERGENCY_HOSTELS);
importLocalData(R.raw.hospitals, MapTypes.HEALTH, LayerTypes.HOSPITALS);
......@@ -150,15 +150,15 @@ public class SyncService extends IntentService {
PointsFeatureCollection collection = response.body();
if (collection != null && collection.getFeatures() != null) {
final Attributes attributes = dataset.getAttributes();
final @LayerType String layerType = ApiDataUtils
.getLayerType(attributes.getDataType(), attributes.getMapType());
RealmQueries.clearMapData(mRealm, layerType);
RealmQueries.cacheMapData(mRealm,
collection.getFeatures(),
attributes.getMapType(),
layerType,
true);
if (attributes != null) {
RealmQueries.clearMapData(mRealm, attributes.getLayerType());
RealmQueries.cacheMapData(mRealm,
collection.getFeatures(),
attributes.getMapType(),
attributes.getLayerType(),
true);
}
}
return true;
......
......@@ -29,13 +29,14 @@ import java.util.Locale;
import ca.mudar.mtlaucasou.Const.ApiValues;
import ca.mudar.mtlaucasou.Const.LayerTypes;
import ca.mudar.mtlaucasou.Const.MapTypes;
import ca.mudar.mtlaucasou.model.LayerType;
import ca.mudar.mtlaucasou.model.MapType;
import static ca.mudar.mtlaucasou.Const.PrefsNames.ITEM_UPDATED_AT;
import static ca.mudar.mtlaucasou.util.LogUtils.makeLogTag;
public class ApiDataUtils {
private static final String TAG = makeLogTag("ApiDataUtils");
public static String getSharedPrefsKey(String id) {
return String.format(Locale.ROOT, ITEM_UPDATED_AT, id);
}
......@@ -44,36 +45,20 @@ public class ApiDataUtils {
* Get the local layerType from the remote dataType
*
* @param dataType api values to convert to layerType
* @param mapType fallback value, used for single-layer datasets
* @return
*/
@LayerType
public static String getLayerType(String dataType, @MapType String mapType) {
public static String getPlacemarkLayerType(String dataType) {
if (!TextUtils.isEmpty(dataType)) {
switch (dataType) {
case ApiValues.TYPE_PLAY_FOUNTAINS:
return LayerTypes.PLAY_FOUNTAINS;
case ApiValues.TYPE_WADING_POOLS:
return LayerTypes.WADING_POOLS;
case ApiValues.TYPE_BEACH:
case ApiValues.TYPE_POOLS_EXT:
case ApiValues.TYPE_POOLS_INT:
return LayerTypes.POOLS;
case ApiValues.TYPE_HOSPITALS:
return LayerTypes.HOSPITALS;
case ApiValues.TYPE_CLSC:
return LayerTypes.CLSC;
}
}
if (!TextUtils.isEmpty(mapType)) {
// These 3 mapTypes have a single layer type, the same as the mapType
switch (mapType) {
case MapTypes.FIRE_HALLS:
return LayerTypes.FIRE_HALLS;
case MapTypes.SPVM_STATIONS:
return LayerTypes.SPVM_STATIONS;
case MapTypes.EMERGENCY_HOSTELS:
return LayerTypes.EMERGENCY_HOSTELS;
}
}
......
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