async Task DownloadImages() { if (DownloadedImages.Count >= LoadedWork.PageCount) { return; } var lworkid = LoadedWork.Id; using (await locker.LockAsync()) { try { if (lworkid != LoadedWork.Id) { return; } SetProgressBar(true); for (int i = 0; i < LoadedWork.PageCount; i++) { // if page is not yet downloaded, download it ImageSource dimg = null; var page = DownloadedImages.Find(x => x.Page == i + 1); if (page == null) { // start downloading it - befor and after download - check if work has been switched if (isClosing || lworkid != LoadedWork.Id) { break; } dimg = await Task.Run(() => PixivWork.GetImage(LoadedWork.GetImageUri(LoadedWork.OriginalImageUrl, i))); if (isClosing || lworkid != LoadedWork.Id) { break; } } else { dimg = page.ImageData; } // cache image and update status var dwImage = new DownloadedImageData(i + 1, dimg); DownloadedImages.Add(dwImage); CacheDownloads(lworkid.Value, dwImage); SetPageStatus(); ImageDownloaded?.Invoke(this, dimg); } } finally { isClosing = false; SetProgressBar(false); } } }
void CacheDownloads(long wrkId, DownloadedImageData img) => PreviousDownloadsCache.Add(new Tuple <long, DownloadedImageData>(wrkId, img), x => x.Item1 == wrkId && x.Item2.Page == img.Page);