-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IImageSourceHandler impl #70
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -3,9 +3,12 @@ | |||||||
using System.Threading; | ||||||||
using System.Threading.Tasks; | ||||||||
using Android.App; | ||||||||
using Android.Content; | ||||||||
using Android.Graphics; | ||||||||
using Android.Views; | ||||||||
using Android.Widget; | ||||||||
using Bumptech.Glide; | ||||||||
using Java.Util.Concurrent; | ||||||||
using Xamarin.Forms; | ||||||||
using Xamarin.Forms.Platform.Android; | ||||||||
using static Bumptech.Glide.Glide; | ||||||||
|
@@ -60,7 +63,7 @@ public static async Task LoadViaGlide (this ImageView imageView, ImageSource sou | |||||||
CancelGlide (imageView); | ||||||||
return; | ||||||||
} | ||||||||
stream.CopyTo (memoryStream); | ||||||||
await stream.CopyToAsync (memoryStream, token); | ||||||||
builder = request.Load (memoryStream.ToArray ()); | ||||||||
} | ||||||||
break; | ||||||||
|
@@ -86,6 +89,100 @@ public static async Task LoadViaGlide (this ImageView imageView, ImageSource sou | |||||||
} | ||||||||
} | ||||||||
|
||||||||
public static async Task<Bitmap> LoadViaGlide (this ImageSource source, Context context, CancellationToken token) | ||||||||
{ | ||||||||
try { | ||||||||
if (!IsActivityAlive (context, source)) | ||||||||
return null; | ||||||||
|
||||||||
RequestManager request = With (context); | ||||||||
RequestBuilder builder = null; | ||||||||
|
||||||||
if (source is null) { | ||||||||
Forms.Debug ("`{0}` is null", nameof (ImageSource)); | ||||||||
return null; | ||||||||
} | ||||||||
|
||||||||
switch (source) { | ||||||||
case FileImageSource fileSource: | ||||||||
var fileName = fileSource.File; | ||||||||
var drawable = ResourceManager.GetDrawableByName (fileName); | ||||||||
if (drawable != 0) { | ||||||||
Forms.Debug ("Loading `{0}` as an Android resource", fileName); | ||||||||
builder = request.AsBitmap ().Load (drawable); | ||||||||
} else { | ||||||||
Forms.Debug ("Loading `{0}` from disk", fileName); | ||||||||
builder = request.AsBitmap ().Load (fileName); | ||||||||
} | ||||||||
break; | ||||||||
|
||||||||
case UriImageSource uriSource: | ||||||||
var url = uriSource.Uri.OriginalString; | ||||||||
Forms.Debug ("Loading `{0}` as a web URL", url); | ||||||||
builder = request.AsBitmap ().Load (url); | ||||||||
break; | ||||||||
|
||||||||
case StreamImageSource streamSource: | ||||||||
Forms.Debug ("Loading `{0}` as a byte[]. Consider using `AndroidResource` instead, as it would be more performant", nameof (StreamImageSource)); | ||||||||
using (var memoryStream = new MemoryStream ()) | ||||||||
using (var stream = await streamSource.Stream (token)) { | ||||||||
if (token.IsCancellationRequested || stream == null) | ||||||||
return null; | ||||||||
if (!IsActivityAlive (context, source)) { | ||||||||
return null; | ||||||||
} | ||||||||
await stream.CopyToAsync (memoryStream, token); | ||||||||
builder = request.AsBitmap ().Load (memoryStream.ToArray ()); | ||||||||
} | ||||||||
break; | ||||||||
} | ||||||||
|
||||||||
var handler = Forms.GlideHandler; | ||||||||
if (handler != null) { | ||||||||
Forms.Debug ("Calling into {0} of type `{1}`.", nameof (IGlideHandler), handler.GetType ()); | ||||||||
handler.Build (source, builder, token); | ||||||||
} | ||||||||
|
||||||||
if (builder is null) { | ||||||||
return null; | ||||||||
} else { | ||||||||
var result = await builder.Submit ().GetAsync (); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't the proper way to await an
Suggested change
|
||||||||
return (Bitmap) result; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code throws
Suggested change
|
||||||||
} | ||||||||
|
||||||||
} catch (Exception exc) { | ||||||||
//Since developers can't catch this themselves, I think we should log it and silently fail | ||||||||
Forms.Warn ("Unexpected exception in glidex: {0}", exc); | ||||||||
return null; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
static bool IsActivityAlive (Context context, ImageSource source) | ||||||||
{ | ||||||||
if (context == null || context.Handle == IntPtr.Zero) { | ||||||||
Forms.Warn ("imageView.Handle is IntPtr.Zero, aborting image load for `{0}`.", source); | ||||||||
return false; | ||||||||
} | ||||||||
|
||||||||
//NOTE: in some cases ContextThemeWrapper is Context | ||||||||
var activity = context as Activity ?? Forms.Activity; | ||||||||
if (activity != null) { | ||||||||
if (activity.IsFinishing) { | ||||||||
Forms.Warn ("Activity of type `{0}` is finishing, aborting image load for `{1}`.", activity.GetType ().FullName, source); | ||||||||
return false; | ||||||||
} | ||||||||
if (activity.IsDestroyed) { | ||||||||
Forms.Warn ("Activity of type `{0}` is destroyed, aborting image load for `{1}`.", activity.GetType ().FullName, source); | ||||||||
return false; | ||||||||
} | ||||||||
} else { | ||||||||
Forms.Warn ("Context `{0}` is not an Android.App.Activity and could not use Android.Glide.Forms.Activity, aborting image load for `{1}`.", context, source); | ||||||||
return false; | ||||||||
} | ||||||||
|
||||||||
return true; | ||||||||
} | ||||||||
|
||||||||
/// <summary> | ||||||||
/// NOTE: see https://github.com/bumptech/glide/issues/1484#issuecomment-365625087 | ||||||||
/// </summary> | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,6 @@ public interface IGlideHandler | |
/// <param name="token">The CancellationToken if you need it</param> | ||
/// <returns>True if the image was handled. Return false if you need the image to be cleared for you.</returns> | ||
bool Build (ImageView imageView, ImageSource source, RequestBuilder builder, CancellationToken token); | ||
void Build (ImageSource source, RequestBuilder builder, CancellationToken token); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a C# 8 default interface method, so that existing This should also use |
||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,7 @@ | ||||||
using System.Threading; | ||||||
using System.Threading.Tasks; | ||||||
using Android.Content; | ||||||
using Android.Graphics; | ||||||
using Android.Runtime; | ||||||
using Android.Widget; | ||||||
using Xamarin.Forms; | ||||||
|
@@ -12,7 +14,7 @@ | |||||
namespace Android.Glide | ||||||
{ | ||||||
[Preserve (AllMembers = true)] | ||||||
public class ImageViewHandler : IImageViewHandler | ||||||
public class ImageViewHandler : IImageViewHandler, IImageSourceHandler | ||||||
{ | ||||||
public ImageViewHandler () | ||||||
{ | ||||||
|
@@ -24,5 +26,11 @@ public ImageViewHandler () | |||||
Forms.Debug ("IImageViewHandler of type `{0}`, `{1}` called.", GetType (), nameof (LoadImageAsync)); | ||||||
await imageView.LoadViaGlide (source, token); | ||||||
} | ||||||
|
||||||
public async Task<Bitmap> LoadImageAsync (ImageSource source, Context context, CancellationToken token = default) | ||||||
{ | ||||||
Forms.Debug ("IImageViewHandler of type `{0}`, `{1}` called.", GetType (), nameof (LoadImageAsync)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return await source.LoadViaGlide (context, token); | ||||||
} | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This long
switch
statement is a lot of copy-paste. This should be refactored out into smaller methods that the otherLoadViaGlide()
method can share.