22using System . Threading ;
33using System . Threading . Tasks ;
44using FFImageLoading . Work ;
5-
6- #if __ANDROID__
5+ using Android . Widget ;
6+ using FFImageLoading . Forms . Handlers ;
77using Xamarin . Forms . Platform . Android ;
8- using TNativeImageView = Android . Widget . ImageView ;
9- #endif
108
119//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(Xamarin.Forms.FileImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
1210//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(Xamarin.Forms.StreamImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
1715namespace FFImageLoading . Forms . Platform
1816{
1917 [ Preserve ( AllMembers = true ) ]
20- public class FFImageLoadingImageViewHandler : IImageViewHandler
18+ public class FFImageLoadingImageViewHandler : HandlerBase < ImageView > , IImageViewHandler
2119 {
22- public Task LoadImageAsync ( Xamarin . Forms . ImageSource imagesource , TNativeImageView imageView , CancellationToken cancellationToken = default )
20+ public Task LoadImageAsync ( Xamarin . Forms . ImageSource imageSource , ImageView imageView , CancellationToken cancellationToken = default )
2321 {
24- #if __ANDROID__
25- if ( ! IsValid ( imageView ) )
26- return Task . CompletedTask ;
27- #endif
28-
29- var source = ImageSourceBinding . GetImageSourceBinding ( imagesource , null ) ;
30- if ( source == null )
22+ try
3123 {
32- #if __ANDROID__
33- imageView . SetImageResource ( Android . Resource . Color . Transparent ) ;
34- #endif
35- return Task . CompletedTask ;
36- }
24+ if ( ! IsValid ( imageView ) )
25+ return Task . CompletedTask ;
3726
38- TaskParameter imageLoader ;
39-
40- if ( source . ImageSource == ImageSource . Url )
41- {
42- var urlSource = ( Xamarin . Forms . UriImageSource ) imagesource ;
43- imageLoader = ImageService . Instance . LoadUrl ( source . Path , urlSource . CacheValidity ) ;
44-
45- if ( ! urlSource . CachingEnabled )
27+ var source = ImageSourceBinding . GetImageSourceBinding ( imageSource , null ) ;
28+ if ( source == null )
4629 {
47- imageLoader . WithCache ( Cache . CacheType . None ) ;
30+ imageView . SetImageResource ( Android . Resource . Color . Transparent ) ;
31+ return Task . CompletedTask ;
4832 }
33+
34+ return LoadImageAsync ( source , imageSource , imageView , cancellationToken ) ;
4935 }
50- else if ( source . ImageSource == ImageSource . CompiledResource )
51- {
52- imageLoader = ImageService . Instance . LoadCompiledResource ( source . Path ) ;
53- }
54- else if ( source . ImageSource == ImageSource . ApplicationBundle )
55- {
56- imageLoader = ImageService . Instance . LoadFileFromApplicationBundle ( source . Path ) ;
57- }
58- else if ( source . ImageSource == ImageSource . Filepath )
59- {
60- imageLoader = ImageService . Instance . LoadFile ( source . Path ) ;
61- }
62- else if ( source . ImageSource == ImageSource . Stream )
63- {
64- imageLoader = ImageService . Instance . LoadStream ( source . Stream ) ;
65- }
66- else if ( source . ImageSource == ImageSource . EmbeddedResource )
67- {
68- imageLoader = ImageService . Instance . LoadEmbeddedResource ( source . Path ) ;
69- }
70- else
36+ catch ( Exception )
7137 {
7238 return Task . CompletedTask ;
7339 }
74-
75- if ( imageLoader != null )
76- {
77- var tcs = new TaskCompletionSource < IScheduledWork > ( ) ;
78-
79- imageLoader
80- . FadeAnimation ( false , false )
81- . Error ( ex => {
82- tcs . TrySetException ( ex ) ;
83- } )
84- . Finish ( scheduledWork => {
85- tcs . TrySetResult ( scheduledWork ) ;
86- } ) ;
87-
88- var task = imageLoader . Into ( imageView ) ;
89-
90- if ( cancellationToken != default )
91- cancellationToken . Register ( ( ) =>
92- {
93- try
94- {
95- task ? . Cancel ( ) ;
96- }
97- catch { }
98- } ) ;
99-
100- return tcs . Task ;
101- }
102-
103- return Task . CompletedTask ;
10440 }
105- #if __ANDROID__
106- private static bool IsValid ( TNativeImageView imageView )
41+
42+ private static bool IsValid ( ImageView imageView )
10743 {
10844 if ( imageView == null || imageView . Handle == IntPtr . Zero )
10945 return false ;
110-
111- //NOTE: in some cases ContextThemeWrapper is Context
46+
47+ #pragma warning disable CS0618 // Type or member is obsolete
11248 var activity = imageView . Context as Android . App . Activity ?? ( Android . App . Activity ) Xamarin . Forms . Forms . Context ;
49+ #pragma warning restore CS0618 // Type or member is obsolete
11350 if ( activity != null )
11451 {
11552 if ( activity . IsFinishing )
@@ -124,6 +61,10 @@ private static bool IsValid(TNativeImageView imageView)
12461
12562 return true ;
12663 }
127- #endif
64+
65+ protected override IImageLoaderTask GetImageLoaderTask ( TaskParameter parameters , ImageView imageView )
66+ {
67+ return parameters . Into ( imageView ) as IImageLoaderTask ;
68+ }
12869 }
12970}
0 commit comments