getNextDatapoint<ChildData extends Data, ParentData extends WithOffline?> method
- ParentData data, {
- bool preloadFullImages = false,
gets all the ChildDatapoints for the given ParentData
if no ParentData is given it defaults to root
Implementation
Stream<List<ChildData>>
getNextDatapoint<ChildData extends Data, ParentData extends WithOffline?>(
ParentData data, {
bool preloadFullImages = false,
}) async* {
final requestType = Helper.SimulatedRequestType.GET;
assert((await API().user) != null,
S.current!.wontFetchAnythingSinceNoOneIsLoggedIn);
Future<List<ChildData>> merge(
List<ChildData> cached, List<ChildData> upstream) async {
try {
cached.retainWhere((element) => (element as WithOffline).forceOffline);
var cachedIds = cached.map((element) => element.id).toList();
upstream.retainWhere((element) => !cachedIds.contains(element.id));
upstream.addAll(cached);
return upstream;
} catch (e) {
debugPrint("error merging data: " + e.toString());
return cached;
}
}
Future<List<ChildData>> filterHiddenIfRootInspections(
List<ChildData> children) async {
if (typeOf<ChildData>() != typeOf<InspectionLocation>() || data != null) {
return children;
}
final hiddenPjNrs = await InspectionVisibility().getHiddenPjNrs();
if (hiddenPjNrs.isEmpty) return children;
return children.where((child) {
if (child is InspectionLocation) {
return !hiddenPjNrs.contains(child.pjNr.toString());
}
return true;
}).toList();
}
yield* _run(
itPrefersCache: _dataPrefersCache(data, type: requestType),
offline: () async => filterHiddenIfRootInspections(
await local.getNextDatapoint<ChildData, ParentData>(data)),
online: () async {
final rap = remote.getNextDatapoint<ChildData, ParentData>(
data,
preloadFullImages: preloadFullImages,
);
return RequestAndParser<http.Response, List<ChildData>>(
rd: rap.rd,
parser: (response) async =>
filterHiddenIfRootInspections(await rap.parser(response)),
);
},
onlineSuccessCB: (childDatas) async {
_cachePrueferIdsFromLocations(childDatas);
if (typeOf<ChildData>() == typeOf<InspectionLocation>() &&
data == null) {
await _pruneStaleRootInspectionCache(
childDatas.whereType<InspectionLocation>().toList(),
);
}
for (final childData in childDatas) {
// "offline" is a local-only flag; online data should clear it to avoid stale UI indicators.
try {
(childData as WithOffline).forceOffline = false;
} catch (_) {}
await local.storeData(
childData,
forId: data?.id ?? await API().rootID,
);
}
},
requestType: requestType,
merge: merge,
);
}