Skip to content

Commit f572990

Browse files
Configuration: Added ExecuteCallbacksOnUIThread property (default disabled)
1 parent f7fc2f8 commit f572990

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

source/FFImageLoading.Common/Config/Configuration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public Configuration()
2929
VerboseLogging = false;
3030
SchedulerMaxParallelTasks = Math.Max(2, (int)(Environment.ProcessorCount / 2d));
3131
DiskCacheDuration = TimeSpan.FromDays(30d);
32+
ExecuteCallbacksOnUIThread = false;
3233
}
3334

3435
/// <summary>
@@ -189,6 +190,13 @@ public bool LoadWithTransparencyChannel
189190
/// </summary>
190191
/// <value>The duration of the cache.</value>
191192
public TimeSpan DiskCacheDuration { get; set; }
193+
194+
/// <summary>
195+
/// Gets or sets a value indicating whether callbacs (OnFinish, OnSuccess, etc)
196+
/// should execute on UI thread
197+
/// </summary>
198+
/// <value><c>true</c> if execute callbacks on UIT hread; otherwise, <c>false</c>.</value>
199+
public bool ExecuteCallbacksOnUIThread { get; set; }
192200
}
193201
}
194202

source/FFImageLoading.Common/Work/ImageLoaderTask.cs

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,21 @@ public async virtual Task<bool> TryLoadFromMemoryCacheAsync()
214214
if (result)
215215
{
216216
Logger.Debug(string.Format("Image loaded from cache: {0}", Key));
217-
Parameters?.OnSuccess?.Invoke(ImageInformation, LoadingResult.MemoryCache);
218-
Parameters?.OnFinish?.Invoke(this);
217+
218+
if (Configuration.ExecuteCallbacksOnUIThread && (Parameters?.OnSuccess != null || Parameters?.OnFinish != null))
219+
{
220+
await MainThreadDispatcher.PostAsync(() =>
221+
{
222+
Parameters?.OnSuccess?.Invoke(ImageInformation, LoadingResult.MemoryCache);
223+
Parameters?.OnFinish?.Invoke(this);
224+
});
225+
}
226+
else
227+
{
228+
Parameters?.OnSuccess?.Invoke(ImageInformation, LoadingResult.MemoryCache);
229+
Parameters?.OnFinish?.Invoke(this);
230+
}
231+
219232
IsCompleted = true;
220233
}
221234
else
@@ -249,7 +262,18 @@ await ShowPlaceholder(Parameters.LoadingPlaceholderPath, KeyForLoadingPlaceholde
249262
else
250263
{
251264
Logger.Error(string.Format("Image loading failed: {0}", Key), ex);
252-
Parameters?.OnError?.Invoke(ex);
265+
266+
if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnError != null)
267+
{
268+
await MainThreadDispatcher.PostAsync(() =>
269+
{
270+
Parameters?.OnError?.Invoke(ex);
271+
});
272+
}
273+
else
274+
{
275+
Parameters?.OnError?.Invoke(ex);
276+
}
253277
}
254278
}
255279

@@ -334,8 +358,19 @@ public async Task RunAsync()
334358

335359
if (loadingResult == LoadingResult.Internet)
336360
Logger?.Debug(string.Format("DownloadOnly success: {0}", Key));
337-
338-
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
361+
362+
if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnSuccess != null)
363+
{
364+
await MainThreadDispatcher.PostAsync(() =>
365+
{
366+
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
367+
});
368+
}
369+
else
370+
{
371+
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
372+
}
373+
339374
return;
340375
}
341376

@@ -362,7 +397,17 @@ public async Task RunAsync()
362397
}
363398
}
364399

365-
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
400+
if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnSuccess != null)
401+
{
402+
await MainThreadDispatcher.PostAsync(() =>
403+
{
404+
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
405+
});
406+
}
407+
else
408+
{
409+
Parameters?.OnSuccess?.Invoke(ImageInformation, loadingResult);
410+
}
366411
}
367412
catch (Exception ex)
368413
{
@@ -381,7 +426,18 @@ public async Task RunAsync()
381426
else
382427
{
383428
Logger.Error(string.Format("Image loading failed: {0}", Key), ex);
384-
Parameters?.OnError?.Invoke(ex);
429+
430+
if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnError != null)
431+
{
432+
await MainThreadDispatcher.PostAsync(() =>
433+
{
434+
Parameters?.OnError?.Invoke(ex);
435+
});
436+
}
437+
else
438+
{
439+
Parameters?.OnError?.Invoke(ex);
440+
}
385441

386442
try
387443
{
@@ -405,7 +461,18 @@ await ShowPlaceholder(Parameters.ErrorPlaceholderPath, KeyForErrorPlaceholder,
405461
{
406462
using (Parameters)
407463
{
408-
Parameters?.OnFinish?.Invoke(this);
464+
if (Configuration.ExecuteCallbacksOnUIThread && Parameters?.OnFinish != null)
465+
{
466+
await MainThreadDispatcher.PostAsync(() =>
467+
{
468+
Parameters?.OnFinish?.Invoke(this);
469+
});
470+
}
471+
else
472+
{
473+
Parameters?.OnFinish?.Invoke(this);
474+
}
475+
409476
ImageService.RemovePendingTask(this);
410477
}
411478

0 commit comments

Comments
 (0)