-
Notifications
You must be signed in to change notification settings - Fork 12
/
BundleDataResolver.cs
218 lines (175 loc) · 6.01 KB
/
BundleDataResolver.cs
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using FFImageLoading.Work;
using System.IO;
using System.Threading.Tasks;
using Foundation;
using System.Linq;
using FFImageLoading.IO;
using FFImageLoading.Extensions;
using System.Threading;
using FFImageLoading.Helpers;
#if __MACOS__
using AppKit;
using PImage = AppKit.NSImage;
#elif __IOS__
using UIKit;
using PImage = UIKit.UIImage;
#endif
namespace FFImageLoading.DataResolvers
{
public class BundleDataResolver : IDataResolver
{
private readonly string[] _fileTypes = { null, "png", "jpg", "jpeg", "webp", "gif" };
public BundleDataResolver(IMainThreadDispatcher mainThreadDispatcher)
{
MainThreadDispatcher = mainThreadDispatcher;
}
protected readonly IMainThreadDispatcher MainThreadDispatcher;
public virtual async Task<DataResolverResult> Resolve(string identifier, TaskParameter parameters, CancellationToken token)
{
var fileName = Path.GetFileNameWithoutExtension(identifier);
var result = await ResolveFromBundlesAsync(fileName, identifier, parameters, token).ConfigureAwait(false);
if (result != null)
return result;
token.ThrowIfCancellationRequested();
result = await ResolveFromNamedResourceAsync(fileName, identifier, parameters, token, MainThreadDispatcher).ConfigureAwait(false);
if (result != null)
return result;
token.ThrowIfCancellationRequested();
result = await ResolveFromAssetCatalogAsync(fileName, identifier, parameters, token, MainThreadDispatcher).ConfigureAwait(false);
if (result != null)
return result;
throw new FileNotFoundException(identifier);
}
private Task<DataResolverResult> ResolveFromBundlesAsync(string fileName, string identifier, TaskParameter parameters, CancellationToken token)
{
NSBundle bundle = null;
var ext = Path.GetExtension(identifier)?.TrimStart(new char[] { '.' });
var tmpPath = Path.GetDirectoryName(identifier)?.Trim('/');
var filenamePath = string.IsNullOrWhiteSpace(tmpPath) ? null : tmpPath + "/";
var hasExtension = !string.IsNullOrWhiteSpace(ext);
var fileTypes = hasExtension ? new[] { ext } : _fileTypes;
foreach (var fileType in fileTypes)
{
string file = null;
string lastPath = null;
token.ThrowIfCancellationRequested();
var scale = (int)parameters.Scale;
if (scale > 1)
{
while (scale > 1)
{
token.ThrowIfCancellationRequested();
var tmpFile = string.Format("{0}@{1}x", fileName, scale);
bundle = NSBundle.AllBundles.FirstOrDefault(bu =>
{
lastPath = string.IsNullOrWhiteSpace(filenamePath) ?
bu.PathForResource(tmpFile, fileType) :
bu.PathForResource(tmpFile, fileType, filenamePath);
return !string.IsNullOrWhiteSpace(lastPath);
});
if (bundle != null)
{
file = tmpFile;
break;
}
scale--;
}
parameters.Scale = scale;
}
token.ThrowIfCancellationRequested();
if (file == null)
{
bundle = NSBundle.AllBundles.FirstOrDefault(bu =>
{
lastPath = string.IsNullOrWhiteSpace(filenamePath) ?
bu.PathForResource(fileName, fileType) :
bu.PathForResource(fileName, fileType, filenamePath);
return !string.IsNullOrWhiteSpace(lastPath);
});
}
token.ThrowIfCancellationRequested();
if (bundle != null && !string.IsNullOrEmpty(lastPath))
{
var stream = FileStore.GetInputStream(lastPath, true);
var imageInformation = new ImageInformation();
imageInformation.SetPath(identifier);
imageInformation.SetFilePath(lastPath);
var result = new DataResolverResult(stream, LoadingResult.CompiledResource, imageInformation);
return Task.FromResult(result);
}
}
return Task.FromResult<DataResolverResult>(null);
}
private async Task<DataResolverResult> ResolveFromAssetCatalogAsync(string fileName, string identifier, TaskParameter parameters, CancellationToken token, IMainThreadDispatcher mainThreadDispatcher)
{
#if __IOS__
if (!UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
return null;
#endif
NSDataAsset asset = null;
try
{
await mainThreadDispatcher.PostAsync(() => asset = new NSDataAsset(identifier, NSBundle.MainBundle)).ConfigureAwait(false);
}
catch { }
if (asset == null && fileName != identifier)
{
try
{
await mainThreadDispatcher.PostAsync(() => asset = new NSDataAsset(fileName, NSBundle.MainBundle)).ConfigureAwait(false);
}
catch { }
}
if (asset != null)
{
token.ThrowIfCancellationRequested();
var stream = asset.Data?.AsStream();
var imageInformation = new ImageInformation();
imageInformation.SetPath(identifier);
imageInformation.SetFilePath(null);
return new DataResolverResult(stream, LoadingResult.CompiledResource, imageInformation);
}
return null;
}
private async Task<DataResolverResult> ResolveFromNamedResourceAsync(string fileName, string identifier, TaskParameter parameters, CancellationToken token, IMainThreadDispatcher mainThreadDispatcher)
{
PImage image = null;
try
{
#if __IOS__
await mainThreadDispatcher.PostAsync(() => image = PImage.FromBundle(identifier)).ConfigureAwait(false);
#elif __MACOS__
await mainThreadDispatcher.PostAsync(() => image = PImage.ImageNamed(identifier)).ConfigureAwait(false);
#endif
}
catch { }
if (image == null && fileName != identifier)
{
try
{
#if __IOS__
await MainThreadDispatcher.PostAsync(() => image = PImage.FromBundle(fileName)).ConfigureAwait(false);
#elif __MACOS__
await MainThreadDispatcher.PostAsync(() => image = PImage.ImageNamed(fileName)).ConfigureAwait(false);
#endif
}
catch { }
}
if (image != null)
{
token.ThrowIfCancellationRequested();
var imageInformation = new ImageInformation();
imageInformation.SetPath(identifier);
imageInformation.SetFilePath(null);
var container = new DecodedImage<object>()
{
Image = image
};
var result = new DataResolverResult(container, LoadingResult.CompiledResource, imageInformation);
return result;
}
return null;
}
}
}