Skip to content

Commit

Permalink
Configuration: Added ExecuteCallbacksOnUIThread property (default dis…
Browse files Browse the repository at this point in the history
…abled)
  • Loading branch information
daniel-luberda committed Oct 31, 2016
1 parent f7fc2f8 commit f572990
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
8 changes: 8 additions & 0 deletions source/FFImageLoading.Common/Config/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public Configuration()
VerboseLogging = false;
SchedulerMaxParallelTasks = Math.Max(2, (int)(Environment.ProcessorCount / 2d));
DiskCacheDuration = TimeSpan.FromDays(30d);
ExecuteCallbacksOnUIThread = false;
}

/// <summary>
Expand Down Expand Up @@ -189,6 +190,13 @@ public bool LoadWithTransparencyChannel
/// </summary>
/// <value>The duration of the cache.</value>
public TimeSpan DiskCacheDuration { get; set; }

/// <summary>
/// Gets or sets a value indicating whether callbacs (OnFinish, OnSuccess, etc)
/// should execute on UI thread
/// </summary>
/// <value><c>true</c> if execute callbacks on UIT hread; otherwise, <c>false</c>.</value>
public bool ExecuteCallbacksOnUIThread { get; set; }
}
}

83 changes: 75 additions & 8 deletions source/FFImageLoading.Common/Work/ImageLoaderTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,21 @@ public async virtual Task<bool> TryLoadFromMemoryCacheAsync()
if (result)
{
Logger.Debug(string.Format("Image loaded from cache: {0}", Key));
Parameters?.OnSuccess?.Invoke(ImageInformation, LoadingResult.MemoryCache);
Parameters?.OnFinish?.Invoke(this);

if (Configuration.ExecuteCallbacksOnUIThread && (Parameters?.OnSuccess != null || Parameters?.OnFinish != null))
{
await MainThreadDispatcher.PostAsync(() =>
{
Parameters?.OnSuccess?.Invoke(ImageInformation, LoadingResult.MemoryCache);
Parameters?.OnFinish?.Invoke(this);
});
}
else
{
Parameters?.OnSuccess?.Invoke(ImageInformation, LoadingResult.MemoryCache);
Parameters?.OnFinish?.Invoke(this);
}

IsCompleted = true;
}
else
Expand Down Expand Up @@ -249,7 +262,18 @@ await ShowPlaceholder(Parameters.LoadingPlaceholderPath, KeyForLoadingPlaceholde
else
{
Logger.Error(string.Format("Image loading failed: {0}", Key), ex);
Parameters?.OnError?.Invoke(ex);

if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnError != null)
{
await MainThreadDispatcher.PostAsync(() =>
{
Parameters?.OnError?.Invoke(ex);
});
}
else
{
Parameters?.OnError?.Invoke(ex);
}
}
}

Expand Down Expand Up @@ -334,8 +358,19 @@ public async Task RunAsync()

if (loadingResult == LoadingResult.Internet)
Logger?.Debug(string.Format("DownloadOnly success: {0}", Key));

Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);

if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnSuccess != null)
{
await MainThreadDispatcher.PostAsync(() =>
{
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
});
}
else
{
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
}

return;
}

Expand All @@ -362,7 +397,17 @@ public async Task RunAsync()
}
}

Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnSuccess != null)
{
await MainThreadDispatcher.PostAsync(() =>
{
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
});
}
else
{
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
}
}
catch (Exception ex)
{
Expand All @@ -381,7 +426,18 @@ public async Task RunAsync()
else
{
Logger.Error(string.Format("Image loading failed: {0}", Key), ex);
Parameters?.OnError?.Invoke(ex);

if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnError != null)
{
await MainThreadDispatcher.PostAsync(() =>
{
Parameters?.OnError?.Invoke(ex);
});
}
else
{
Parameters?.OnError?.Invoke(ex);
}

try
{
Expand All @@ -405,7 +461,18 @@ await ShowPlaceholder(Parameters.ErrorPlaceholderPath, KeyForErrorPlaceholder,
{
using (Parameters)
{
Parameters?.OnFinish?.Invoke(this);
if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnFinish != null)
{
await MainThreadDispatcher.PostAsync(() =>
{
Parameters?.OnFinish?.Invoke(this);
});
}
else
{
Parameters?.OnFinish?.Invoke(this);
}

ImageService.RemovePendingTask(this);
}

Expand Down

0 comments on commit f572990

Please sign in to comment.