loadAndCacheAll<ChildData extends WithLangText, ParentData extends WithOffline, DDModel extends DropDownModel<ChildData, ParentData>> method

Future<bool> loadAndCacheAll<ChildData extends WithLangText, ParentData extends WithOffline, DDModel extends DropDownModel<ChildData, ParentData>>(
  1. DDModel caller,
  2. int depth, {
  3. String? name,
  4. String? parentID,
})

Andere Methoden wie loadAndCacheAll, setOnlineAll etc. können hier bleiben ...

Implementation

Future<bool> loadAndCacheAll<
    ChildData extends WithLangText,
    ParentData extends WithOffline,
    DDModel extends DropDownModel<ChildData, ParentData>>(
  DDModel caller,
  int depth, {
  String? name,
  String? parentID,
}) async {
  // base-case: CheckPointDefects have no children
  if (depth == 0) return true;
  depth--;
  try {
    //fail early if no connection
    await API().tryNetwork(requestType: Helper.SimulatedRequestType.GET);
    //get all children, this will also cache them internally
    var children = await caller
        .all(preloadFullImages: Options().preloadFullImagesOnManualDownload)
        .last;
    if (caller.currentData is InspectionLocation) {
      final location = caller.currentData as InspectionLocation;
      if (location.dokuspaths != null && location.dokuspaths!.isNotEmpty) {
        var docus = location.dokuspaths;
        if (docus != null) {
          assert((await API().user) != null,
              'Niemand eingeloggt'); // Using string directly instead of S.current
          for (var doc in docus) {
            await API().getDocument(doc.docupath);
          }
        }
      }
    }

    var didSucceed = await Future.wait(children.map(
      (child) async {
        if (depth == 0)
          return true; //base-case as to not call generateNextModel
        bool childSucceeded = await loadAndCacheAll(
            caller.generateNextModel(child), depth,
            name: name, parentID: caller.currentData.id);
        return childSucceeded;
      },
    ));

    //if all children succeeded recursive calling succeeded
    bool success = didSucceed.every((el) => el);
    if (success) {
      caller.currentData.forceOffline = true;
      if (parentID == null) return false;

      try {
        await API().local.storeData(caller.currentData, forId: parentID);
      } catch (e) {
        return false;
      }
    }

    return success;
  } catch (error) {
    debugPrint('failed! ${depth + 1}');
    showToast(error.toString() +
        "\n" +
        'Probiere es später nochmal'); // Using string directly instead of S.current
    return false; //failed
  }
}