loadAndCacheAll<ChildData extends WithLangText, ParentData extends WithOffline, DDModel extends DropDownModel<ChildData, ParentData> > method
recursivle cache all elements that underly the caller
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,
S.current!.wontFetchAnythingSinceNoOneIsLoggedIn);
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" + S.current!.tryAgainLater_noNetwork);
return false; //failed
}
}