-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve some things and update dependencies
- Loading branch information
1 parent
2c10353
commit fb7c447
Showing
12 changed files
with
347 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
app/src/main/java/ch/unstable/ost/api/search/types/ConnectionDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ch.unstable.ost.api.search.types; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import ch.unstable.ost.api.model.Connection; | ||
import ch.unstable.ost.api.model.Section; | ||
|
||
public class ConnectionDeserializer implements JsonDeserializer<Connection> { | ||
@Override | ||
public Connection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
JsonObject connectionObj = json.getAsJsonObject(); | ||
Section[] sections = context.deserialize(connectionObj.get("legs"), Section[].class); | ||
return new Connection(sections); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
app/src/main/java/ch/unstable/ost/api/search/types/PassingCheckpointsDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package ch.unstable.ost.api.search.types; | ||
|
||
import android.support.annotation.Nullable; | ||
import android.util.Log; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
|
||
import java.lang.reflect.Type; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
import java.util.TimeZone; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import ch.unstable.ost.api.model.Location; | ||
import ch.unstable.ost.api.model.PassingCheckpoint; | ||
import ch.unstable.ost.utils.LogUtils; | ||
|
||
public enum PassingCheckpointsDeserializer implements JsonDeserializer<PassingCheckpoint> { | ||
INSTANCE; | ||
|
||
private static final String TAG = "PassingCPDeserializer"; | ||
private static final Logger LOGGER = Logger.getLogger(TAG); | ||
|
||
@Nullable | ||
public static Date getDate(SimpleDateFormat dateFormat, JsonObject object, String name) { | ||
if (!object.has(name)) { | ||
return null; | ||
} | ||
JsonElement dateField = object.get(name); | ||
if (dateField.isJsonNull()) { | ||
return null; | ||
} | ||
try { | ||
return dateFormat.parse(dateField.getAsString()); | ||
} catch (ParseException e) { | ||
throw new JsonParseException("Unable to parse departure date", e); | ||
} | ||
} | ||
|
||
public static SimpleDateFormat getDateFormat() { | ||
SimpleDateFormat departureFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ROOT); | ||
departureFormat.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); | ||
return departureFormat; | ||
} | ||
|
||
@Override | ||
public PassingCheckpoint deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
SimpleDateFormat dateFormat = getDateFormat(); | ||
JsonObject object = json.getAsJsonObject(); | ||
String stopId = object.get("stopid").getAsString(); | ||
String name = object.get("name").getAsString(); | ||
Date departure = getDate(dateFormat, object, "departure"); | ||
Date arrival = getDate(dateFormat, object, "arrival"); | ||
|
||
if(departure == null && arrival != null) { | ||
Log.w(TAG, "Departure is null"); | ||
departure = arrival; | ||
} else if(arrival == null && departure != null) { | ||
Log.w(TAG, "Arrival is null"); | ||
arrival = departure; | ||
} else if(arrival == null && departure == null) { | ||
LOGGER.log(Level.WARNING, "neither arrival nor departure is set: " + LogUtils.prettyJson(object)); | ||
Log.w(TAG, "neither arrival nor departure is set: " + LogUtils.prettyJson(object)); | ||
return null; | ||
} | ||
|
||
Location location = new Location(name, Location.StationType.UNKNOWN, stopId); | ||
// TODO: Find out if track is sent | ||
String platform = null; | ||
if(object.has("track")) { | ||
platform = object.get("track").getAsString(); | ||
} | ||
return new PassingCheckpoint(arrival, departure, location, platform); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.