-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathImageService.cs
More file actions
144 lines (121 loc) · 5.34 KB
/
ImageService.cs
File metadata and controls
144 lines (121 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.IO;
using FFImageLoading.Cache;
using FFImageLoading.Drawables;
using FFImageLoading.Helpers;
using FFImageLoading.Work;
using FFImageLoading.DataResolvers;
using System.Runtime.CompilerServices;
using FFImageLoading.Config;
namespace FFImageLoading
{
/// <summary>
/// FFImageLoading by Daniel Luberda
/// </summary>
public class ImageService : ImageServiceBase<SelfDisposingBitmapDrawable>
{
readonly Android.Util.DisplayMetrics _metrics = Android.Content.Res.Resources.System.DisplayMetrics;
static ConditionalWeakTable<object, IImageLoaderTask> _viewsReferences = new ConditionalWeakTable<object, IImageLoaderTask>();
static IImageService _instance;
/// <summary>
/// FFImageLoading instance.
/// </summary>
/// <value>The instance.</value>
public static IImageService Instance
{
get
{
if (_instance == null)
_instance = new ImageService();
return _instance;
}
}
/// <summary>
/// Set this to use FFImageLoading in a unit test environment.
/// Instead throwing DoNotReference exception - use Mock implementation
/// </summary>
public static bool EnableMockImageService { get; set; }
protected override IMemoryCache<SelfDisposingBitmapDrawable> MemoryCache => ImageCache.Instance;
protected override IMD5Helper CreatePlatformMD5HelperInstance(Configuration configuration) => new MD5Helper();
protected override IMiniLogger CreatePlatformLoggerInstance(Configuration configuration) => new MiniLogger();
protected override IPlatformPerformance CreatePlatformPerformanceInstance(Configuration configuration) => new PlatformPerformance();
protected override IMainThreadDispatcher CreateMainThreadDispatcherInstance(Configuration configuration) => new MainThreadDispatcher();
protected override IDataResolverFactory CreateDataResolverFactoryInstance(Configuration configuration) => new DataResolverFactory();
protected override IDiskCache CreatePlatformDiskCacheInstance(Configuration configuration)
{
if (string.IsNullOrWhiteSpace(configuration.DiskCachePath))
{
var context = new Android.Content.ContextWrapper(Android.App.Application.Context);
string tmpPath = context.CacheDir.AbsolutePath;
string cachePath = Path.Combine(tmpPath, "FFSimpleDiskCache");
Java.IO.File androidTempFolder = new Java.IO.File(cachePath);
if (!androidTempFolder.Exists())
androidTempFolder.Mkdir();
if (!androidTempFolder.CanRead())
androidTempFolder.SetReadable(true, false);
if (!androidTempFolder.CanWrite())
androidTempFolder.SetWritable(true, false);
configuration.DiskCachePath = cachePath;
}
return new SimpleDiskCache(configuration.DiskCachePath, configuration);
}
internal static IImageLoaderTask CreateTask<TImageView>(TaskParameter parameters, ITarget<SelfDisposingBitmapDrawable, TImageView> target) where TImageView : class
{
return new PlatformImageLoaderTask<TImageView>(target, parameters, Instance);
}
internal static IImageLoaderTask CreateTask(TaskParameter parameters)
{
return new PlatformImageLoaderTask<object>(null, parameters, Instance);
}
protected override void SetTaskForTarget(IImageLoaderTask currentTask)
{
var targetView = currentTask?.Target?.TargetControl;
if (!(targetView is Android.Views.View))
return;
lock (_viewsReferences)
{
if (_viewsReferences.TryGetValue(targetView, out var existingTask))
{
try
{
if (existingTask != null && !existingTask.IsCancelled && !existingTask.IsCompleted)
{
existingTask.Cancel();
}
}
catch (ObjectDisposedException) { }
_viewsReferences.Remove(targetView);
}
_viewsReferences.Add(targetView, currentTask);
}
}
public override void CancelWorkForView(object view)
{
lock (_viewsReferences)
{
if (_viewsReferences.TryGetValue(view, out var existingTask))
{
try
{
if (existingTask != null && !existingTask.IsCancelled && !existingTask.IsCompleted)
{
existingTask.Cancel();
}
}
catch (ObjectDisposedException) { }
}
}
}
public override int DpToPixels(double dp)
{
double px = dp * ((float)_metrics.DensityDpi / 160f);
return (int)Math.Floor(px);
}
public override double PixelsToDp(double px)
{
if (Math.Abs(px) < double.Epsilon)
return 0;
return px / ((float)_metrics.DensityDpi / 160f);
}
}
}