/** * Switches the current image one to the left. The means changing the index of the current image within * the cache and also updating one of the cache positions. Because the cache always holds the same number of * images before and the same number after the current image, moving the current image left means that one of * cache positions after the current image will need to store an image before the current one. * * Once this is done, the cached images will be loaded according to priority. From highest to lowest priority, * this is the current image, the ones after it, and the ones before it. This corresponds to how likely it is * the user will view each image. */ private async void MoveCacheLeft() { // First, move the current image index one to the left _imageCacheCurrIndex--; if (_imageCacheCurrIndex < 0) { _imageCacheCurrIndex += CACHE_LEN; } // Which position in the cache is now stale and should be replaced int cacheUpdateIndex = _imageCacheCurrIndex - BACK_CACHE_NUM; if (cacheUpdateIndex < 0) { cacheUpdateIndex += CACHE_LEN; } // What image in the gallery will replace the stale cache position int galleryUpdateIndex = CurrentIndex - BACK_CACHE_NUM; if (galleryUpdateIndex < 0) { galleryUpdateIndex += _galleryItems.Count(); } // Update the cache position with the new image Photo p = _galleryItems[galleryUpdateIndex]; _imageCache[cacheUpdateIndex].Photo = p; // If the current image has been loaded (i.e., there isn't an old image // still loaded), either the preview or the full resolution, then update the // view to the user. Otherwise, when the image loads, the view will update. if (CurrentImageViewModel.PreviewLoaded()) { OnPropertyChanged("CurrentImage"); } // Update sidebar first so that it's not waiting on the image load to update CurrentImageChanged(); // Then load the new cache position's image await Task.Run(() => { _imageCache[cacheUpdateIndex].UpdateImage(); }); }
/// <summary> /// Called when this page is no longer visible to the user. This cancels any image loading operations that might be going on. /// </summary> public override void NavigatorLostFocus() { CurrentImageViewModel.CancelAllLoads(); }