From 531451f4da33eff3bbe6aa58963e6e9f52e05f91 Mon Sep 17 00:00:00 2001 From: Daniel Luberda Date: Sun, 25 Sep 2016 12:58:37 +0200 Subject: [PATCH] ImageLoaderTask refactorings --- .../Work/ImageLoaderTask.cs | 60 +++++++++---------- .../Work/ImageLoaderTask.cs | 48 ++++++++------- .../Work/ImageLoaderTask.cs | 3 +- 3 files changed, 54 insertions(+), 57 deletions(-) diff --git a/source/FFImageLoading.Droid/Work/ImageLoaderTask.cs b/source/FFImageLoading.Droid/Work/ImageLoaderTask.cs index bb4bbde79..128c1e0a3 100644 --- a/source/FFImageLoading.Droid/Work/ImageLoaderTask.cs +++ b/source/FFImageLoading.Droid/Work/ImageLoaderTask.cs @@ -1,19 +1,12 @@ using System.Threading; using Android.Content; -using Android.Content.Res; using Android.Graphics; using Android.Graphics.Drawables; -using Android.OS; -using Android.Widget; -using Object = Java.Lang.Object; using System.Threading.Tasks; using System.IO; -using FFImageLoading.Work; using FFImageLoading.Helpers; -using FFImageLoading.IO; using FFImageLoading.Cache; using FFImageLoading.Extensions; -using Android.Runtime; using System; using FFImageLoading.Work.StreamResolver; using System.Linq; @@ -85,7 +78,7 @@ protected Context Context { get { - return new Android.Content.ContextWrapper(Android.App.Application.Context); + return new ContextWrapper(Android.App.Application.Context); } } @@ -129,11 +122,11 @@ protected override async Task TryGeneratingImageAsync() // Post on main thread await MainThreadDispatcher.PostAsync(() => - { - _target.Set(this, drawableWithResult.Item, drawableWithResult.Result.IsLocalOrCachedResult(), false); - Completed = true; - Parameters?.OnSuccess(drawableWithResult.ImageInformation, drawableWithResult.Result); - }).ConfigureAwait(false); + { + _target.Set(this, drawableWithResult.Item, drawableWithResult.Result.IsLocalOrCachedResult(), false); + Completed = true; + Parameters?.OnSuccess(drawableWithResult.ImageInformation, drawableWithResult.Result); + }).ConfigureAwait(false); if (!Completed) return GenerateResult.Failed; @@ -185,20 +178,20 @@ public override async Task TryLoadingFromCacheAsync() { Logger.Debug(string.Format("Image from cache: {0}", key)); await MainThreadDispatcher.PostAsync(() => - { - if (IsCancelled) - return; + { + if (IsCancelled) + return; - var ffDrawable = value as FFBitmapDrawable; - if (ffDrawable != null) - ffDrawable.StopFadeAnimation(); + var ffDrawable = value as FFBitmapDrawable; + if (ffDrawable != null) + ffDrawable.StopFadeAnimation(); - _target.Set(this, value, true, false); + _target.Set(this, value, true, false); - Completed = true; + Completed = true; - Parameters?.OnSuccess(cacheEntry.Item2, LoadingResult.MemoryCache); - }).ConfigureAwait(false); + Parameters?.OnSuccess(cacheEntry.Item2, LoadingResult.MemoryCache); + }).ConfigureAwait(false); if (!Completed) return CacheResult.NotFound; // not sure what to return in that case @@ -254,12 +247,12 @@ public override async Task LoadFromStreamAsync(Stream stream) // Post on main thread await MainThreadDispatcher.PostAsync(() => - { - _target.Set(this, resultWithDrawable.Item, true, false); - - Completed = true; - Parameters?.OnSuccess(resultWithDrawable.ImageInformation, resultWithDrawable.Result); - }).ConfigureAwait(false); + { + _target.Set(this, resultWithDrawable.Item, true, false); + + Completed = true; + Parameters?.OnSuccess(resultWithDrawable.ImageInformation, resultWithDrawable.Result); + }).ConfigureAwait(false); if (!Completed) return GenerateResult.Failed; @@ -335,7 +328,7 @@ protected virtual async Task> Get } else { - stream.Seek(0, SeekOrigin.Begin); + stream.Seek(0, SeekOrigin.Begin); } } catch (Exception ex) @@ -446,13 +439,15 @@ protected virtual async Task> Get if (Parameters.Transformations != null && Parameters.Transformations.Count > 0 && (!isPlaceholder || (isPlaceholder && transformPlaceholdersEnabled))) { + var transformations = Parameters.Transformations.ToList(); + await _decodingLock.WaitAsync().ConfigureAwait(false); // Applying transformations is both CPU and memory intensive try { if (IsCancelled) return new WithLoadingResult(LoadingResult.Canceled); - foreach (var transformation in Parameters.Transformations.ToList() /* to prevent concurrency issues */) + foreach (var transformation in transformations) { if (IsCancelled) return new WithLoadingResult(LoadingResult.Canceled); @@ -522,8 +517,7 @@ protected virtual async Task> Get } finally { - if (stream != null) - stream.Dispose(); + stream?.Dispose(); } } diff --git a/source/FFImageLoading.Touch/Work/ImageLoaderTask.cs b/source/FFImageLoading.Touch/Work/ImageLoaderTask.cs index e38f0359a..39e828ed2 100644 --- a/source/FFImageLoading.Touch/Work/ImageLoaderTask.cs +++ b/source/FFImageLoading.Touch/Work/ImageLoaderTask.cs @@ -97,14 +97,14 @@ protected override async Task TryGeneratingImageAsync() { // Post on main thread await MainThreadDispatcher.PostAsync(() => - { - if (IsCancelled) - return; + { + if (IsCancelled) + return; - _target.Set(this, image, imageWithResult.Result.IsLocalOrCachedResult(), false); - Completed = true; - Parameters?.OnSuccess(imageWithResult.ImageInformation, imageWithResult.Result); - }).ConfigureAwait(false); + _target.Set(this, image, imageWithResult.Result.IsLocalOrCachedResult(), false); + Completed = true; + Parameters?.OnSuccess(imageWithResult.ImageInformation, imageWithResult.Result); + }).ConfigureAwait(false); if (!Completed) return GenerateResult.Failed; @@ -142,14 +142,14 @@ public override async Task TryLoadingFromCacheAsync() return CacheResult.NotFound; // not sure what to return in that case await MainThreadDispatcher.PostAsync(() => - { - if (IsCancelled) - return; - - _target.Set(this, value, true, false); - Completed = true; - Parameters?.OnSuccess(cacheEntry.Item2, LoadingResult.MemoryCache); - }).ConfigureAwait(false); + { + if (IsCancelled) + return; + + _target.Set(this, value, true, false); + Completed = true; + Parameters?.OnSuccess(cacheEntry.Item2, LoadingResult.MemoryCache); + }).ConfigureAwait(false); if (!Completed) return CacheResult.NotFound; // not sure what to return in that case @@ -211,14 +211,14 @@ public override async Task LoadFromStreamAsync(Stream stream) { // Post on main thread await MainThreadDispatcher.PostAsync(() => - { - if (IsCancelled) - return; + { + if (IsCancelled) + return; - _target.Set(this, image, true, false); - Completed = true; - Parameters?.OnSuccess(resultWithImage.ImageInformation, resultWithImage.Result); - }).ConfigureAwait(false); + _target.Set(this, image, true, false); + Completed = true; + Parameters?.OnSuccess(resultWithImage.ImageInformation, resultWithImage.Result); + }).ConfigureAwait(false); if (!Completed) return GenerateResult.Failed; @@ -372,6 +372,8 @@ protected virtual async Task> GetImageAsync(string so if (Parameters.Transformations != null && Parameters.Transformations.Count > 0 && (!isPlaceholder || (isPlaceholder && transformPlaceholdersEnabled))) { + var transformations = Parameters.Transformations.ToList(); + await _decodingLock.WaitAsync().ConfigureAwait(false); // Applying transformations is both CPU and memory intensive try @@ -379,7 +381,7 @@ protected virtual async Task> GetImageAsync(string so if (IsCancelled) return new WithLoadingResult(LoadingResult.Canceled); - foreach (var transformation in Parameters.Transformations.ToList() /* to prevent concurrency issues */) + foreach (var transformation in transformations) { if (IsCancelled) return new WithLoadingResult(LoadingResult.Canceled); diff --git a/source/FFImageLoading.Windows/Work/ImageLoaderTask.cs b/source/FFImageLoading.Windows/Work/ImageLoaderTask.cs index 397ce1067..9918a23f8 100644 --- a/source/FFImageLoading.Windows/Work/ImageLoaderTask.cs +++ b/source/FFImageLoading.Windows/Work/ImageLoaderTask.cs @@ -338,6 +338,7 @@ protected virtual async Task> GetImageAsync(s if (Parameters.Transformations != null && Parameters.Transformations.Count > 0 && (!isPlaceholder || (isPlaceholder && transformPlaceholdersEnabled))) { + var transformations = Parameters.Transformations.ToList(); BitmapHolder imageIn = null; try @@ -357,7 +358,7 @@ protected virtual async Task> GetImageAsync(s if (IsCancelled) return new WithLoadingResult(LoadingResult.Canceled); - foreach (var transformation in Parameters.Transformations.ToList() /* to prevent concurrency issues */) + foreach (var transformation in transformations) { if (IsCancelled) return new WithLoadingResult(LoadingResult.Canceled);