Public Access
feat(departures): make some adaptations to supp diff departure srces
with these adaptations to the api_handler and Departure.fromJson, the fromJson-factory now works for responses from both /departureBoard and /tripDetail for /tripDetail a little json-copying is used, see ApiHandler._expandDeparturesJson
This commit is contained in:
+22
-2
@@ -218,10 +218,30 @@ class ApiHandler {
|
||||
json.decode(jsonResponse.body) as Map<String, dynamic>;
|
||||
final DepartureList = httpPackageJson["Stops"]["Stop"]
|
||||
.map(
|
||||
(x) => Departure.fromJson(x),
|
||||
(x) => Departure.fromJson(
|
||||
// needed because I'm too lazy to write a second mostly identical
|
||||
// constructor and a wrapper
|
||||
_expandDeparturesJson(httpPackageJson, x, ["ref", "Product"]),
|
||||
),
|
||||
)
|
||||
.toList()
|
||||
.cast<Departure>();
|
||||
return DepartureList;
|
||||
}
|
||||
}
|
||||
|
||||
// workaround for the fact that json from tripDetail and DepartureBoard are
|
||||
// quite different from each other in their structure... but I don't wish to
|
||||
// build a second constructor >:(
|
||||
// so this function duplicates topLevel Maps into a lower level to be used in
|
||||
// mapping functions and such
|
||||
static Map<String, dynamic> _expandDeparturesJson(
|
||||
Map<String, dynamic> topLevelJson,
|
||||
Map<String, dynamic> subLevelJson,
|
||||
List<String> topLevelKeys,
|
||||
) {
|
||||
for (final String entry in topLevelKeys) {
|
||||
subLevelJson[entry] = topLevelJson[entry];
|
||||
}
|
||||
return subLevelJson;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user