Skip to content

Commit

Permalink
ImageLoaderTask refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-luberda committed Sep 25, 2016
1 parent 6960cb6 commit 531451f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 57 deletions.
60 changes: 27 additions & 33 deletions source/FFImageLoading.Droid/Work/ImageLoaderTask.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -85,7 +78,7 @@ protected Context Context
{
get
{
return new Android.Content.ContextWrapper(Android.App.Application.Context);
return new ContextWrapper(Android.App.Application.Context);
}
}

Expand Down Expand Up @@ -129,11 +122,11 @@ protected override async Task<GenerateResult> 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;
Expand Down Expand Up @@ -185,20 +178,20 @@ public override async Task<CacheResult> 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
Expand Down Expand Up @@ -254,12 +247,12 @@ public override async Task<GenerateResult> 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;
Expand Down Expand Up @@ -335,7 +328,7 @@ protected virtual async Task<WithLoadingResult<SelfDisposingBitmapDrawable>> Get
}
else
{
stream.Seek(0, SeekOrigin.Begin);
stream.Seek(0, SeekOrigin.Begin);
}
}
catch (Exception ex)
Expand Down Expand Up @@ -446,13 +439,15 @@ protected virtual async Task<WithLoadingResult<SelfDisposingBitmapDrawable>> 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<SelfDisposingBitmapDrawable>(LoadingResult.Canceled);

foreach (var transformation in Parameters.Transformations.ToList() /* to prevent concurrency issues */)
foreach (var transformation in transformations)
{
if (IsCancelled)
return new WithLoadingResult<SelfDisposingBitmapDrawable>(LoadingResult.Canceled);
Expand Down Expand Up @@ -522,8 +517,7 @@ protected virtual async Task<WithLoadingResult<SelfDisposingBitmapDrawable>> Get
}
finally
{
if (stream != null)
stream.Dispose();
stream?.Dispose();
}
}

Expand Down
48 changes: 25 additions & 23 deletions source/FFImageLoading.Touch/Work/ImageLoaderTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ protected override async Task<GenerateResult> 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;
Expand Down Expand Up @@ -142,14 +142,14 @@ public override async Task<CacheResult> 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
Expand Down Expand Up @@ -211,14 +211,14 @@ public override async Task<GenerateResult> 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;
Expand Down Expand Up @@ -372,14 +372,16 @@ protected virtual async Task<WithLoadingResult<UIImage>> 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
{
if (IsCancelled)
return new WithLoadingResult<UIImage>(LoadingResult.Canceled);

foreach (var transformation in Parameters.Transformations.ToList() /* to prevent concurrency issues */)
foreach (var transformation in transformations)
{
if (IsCancelled)
return new WithLoadingResult<UIImage>(LoadingResult.Canceled);
Expand Down
3 changes: 2 additions & 1 deletion source/FFImageLoading.Windows/Work/ImageLoaderTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ protected virtual async Task<WithLoadingResult<WriteableBitmap>> GetImageAsync(s
if (Parameters.Transformations != null && Parameters.Transformations.Count > 0
&& (!isPlaceholder || (isPlaceholder && transformPlaceholdersEnabled)))
{
var transformations = Parameters.Transformations.ToList();
BitmapHolder imageIn = null;

try
Expand All @@ -357,7 +358,7 @@ protected virtual async Task<WithLoadingResult<WriteableBitmap>> GetImageAsync(s
if (IsCancelled)
return new WithLoadingResult<WriteableBitmap>(LoadingResult.Canceled);

foreach (var transformation in Parameters.Transformations.ToList() /* to prevent concurrency issues */)
foreach (var transformation in transformations)
{
if (IsCancelled)
return new WithLoadingResult<WriteableBitmap>(LoadingResult.Canceled);
Expand Down

0 comments on commit 531451f

Please sign in to comment.