processObjects<T extends VasatModel<T>> method

Future<List<T>> processObjects<T extends VasatModel<T>>(
  1. Map<String, dynamic> data,
  2. VasatFromJsonFactory<T> factory, {
  3. bool updateTimestamp = false,
})

Implementation

Future<List<T>> processObjects<T extends VasatModel<T>>(Map<String, dynamic> data, VasatFromJsonFactory<T> factory, {bool updateTimestamp = false}) async {

  // data Map can be the entire  Vasat response or just the Payload part of it so we need to check this
  var finalData = data.containsKey("payload") ? data["payload"] : data;
  debug("processObjects data total -> ${finalData["total"]} offset -> ${finalData["offset"]}");

  // Check if we need to update the local value of the last updated timestamp for the object.
  if (updateTimestamp && data.containsKey("time") && data["time"] is int) {
    var timestamp = data["time"] as int;
    var vasatObjectName = factory({}).getObjectName();
    debug("processObjects storing new timestamp -> $timestamp for object -> $vasatObjectName");
    await storageProvider.setObjectLastTimestamp(vasatObjectName, timestamp);
  }

  List<T> res = [];
  if (finalData.containsKey("results") && finalData["results"] is List) {
    List<dynamic> payload = finalData["results"];
    debug("processObjects data result length -> ${payload.length}");
    res = payload.map((item) {
      // Mark object as not dirty as it is fresh from the server
      item["dirty"] = false;
      return factory(item);
    }).toList();
  }
  debug("processObjects for ${T.toString()} finished parsing json data");
  return res;
}