cacheInspectionImagesForDownload function
Implementation
@visibleForTesting
Future<bool> cacheInspectionImagesForDownload(
List<Data> assets, {
DownloadProgressSession? progressSession,
ImageCacheLookup? readCachedImage,
ImageDownloadLookup? downloadImage,
}) async {
final readCached = readCachedImage ??
(String hash, Data owner) => API().local.getImageByHash(
hash,
owner: owner,
);
final download = downloadImage ??
(String hash, Data owner) => API().getImageByHash(
hash,
owner: owner,
);
final jobs = <({Data data, String hash})>[];
final seen = <String>{};
for (final data in assets) {
for (final hash in _imageHashesForData(data)) {
final key = '${data.id}|$hash';
if (!seen.add(key)) continue;
jobs.add((data: data, hash: hash));
}
}
progressSession?.setStep(
InspectionDownloadSteps.photos,
label: jobs.isEmpty
? InspectionDownloadSteps.noPhotosLabel
: InspectionDownloadSteps.photosLabel,
);
if (jobs.isEmpty) return true;
for (final job in jobs) {
progressSession?.reserveTask(
'/image/get|${job.hash}',
step: InspectionDownloadSteps.photos,
);
}
var allSucceeded = true;
for (var index = 0; index < jobs.length; index++) {
final job = jobs[index];
final current = index + 1;
final total = jobs.length;
final key = '/image/get|${job.hash}';
final token = progressSession?.beginTask(
'Fotos prüfen ($current/$total)',
key: key,
step: InspectionDownloadSteps.photos,
);
var isCached = false;
try {
final image = await readCached(job.hash, job.data);
isCached = image != null;
} catch (_) {}
if (isCached) {
if (token != null) progressSession?.endTask(token, success: true);
continue;
}
progressSession?.setStep(
InspectionDownloadSteps.photos,
label: 'Fotos speichern ($current/$total)',
);
try {
final image = await download(job.hash, job.data);
final succeeded = image != null;
if (!succeeded) {
allSucceeded = false;
debugPrint(
'Bild ${job.hash} konnte nicht offline gespeichert werden',
);
}
if (token != null) progressSession?.endTask(token, success: succeeded);
} catch (_) {
allSucceeded = false;
if (token != null) progressSession?.endTask(token, success: false);
debugPrint(
'Bild ${job.hash} konnte nicht offline gespeichert werden',
);
}
}
return allSucceeded;
}