Skip to content
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

Fixed Android's StreamImageSourceService.LoadDrawableAsync() #14109

Merged
merged 6 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

<ItemGroup>
<MauiImage Include="Resources\Images\*" />
<MauiImage Remove="Resources\Images\red-embedded.png" />
<None Remove="Resources\Images\red-embedded.png" />
<EmbeddedResource Include="Resources\Images\red-embedded.png" LogicalName="red-embedded.png" />
<MauiIcon Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" Color="#512BD4" />
<MauiSplashScreen Include="Resources\appiconfg.svg" Color="#512BD4" BaseSize="128,128" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading.Tasks;
using Android.Graphics.Drawables;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.DeviceTests
Expand Down Expand Up @@ -37,6 +39,29 @@ await InvokeOnMainThreadAsync(async () =>
await bitmapDrawable1.Bitmap.AssertNotEqualAsync(bitmapDrawable2.Bitmap);
});
}

[Fact]
public async Task ImageSetFromStreamRenders()
{
SetupBuilder();
var layout = new VerticalStackLayout();

using var stream = GetType().Assembly.GetManifestResourceStream("red-embedded.png");

var image = new Image
{
Source = ImageSource.FromStream(() => stream)
};

layout.Add(image);

await InvokeOnMainThreadAsync(async () =>
{
var handler = CreateHandler<LayoutHandler>(layout);
await image.Wait();
await handler.ToPlatform().AssertContainsColor(Colors.Red, MauiContext);
});
}
}

// This subclass of memory stream is deliberately set up to trick Glide into using the cached image
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? s
return;
this.completed = true;

// set the image
this.view.setImageDrawable(resource);
post(() -> {
// set the image
this.view.setImageDrawable(resource);
Comment on lines +49 to +51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change to post() might be OK, did this fix something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the ConfigureAwait(false) from StreamImageSourceService.Android.cs seemed to have worked by itself. The post() changes didn't fix anything on their own (i.e. I tested adding back the ConfigureAwait(false) and ended up with errors again).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we don't need this anymore then? Is there any fears that there are related issues?


// trigger the callback out of this target
post(() -> callback.onComplete(true, resource, this::clear));
// trigger the callback out of this target
callback.onComplete(true, resource, this::clear);
});
}

private void post(Runnable runnable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class StreamImageSourceService
Stream? stream = null;
try
{
stream = await streamImageSource.GetStreamAsync(cancellationToken).ConfigureAwait(false);
stream = await streamImageSource.GetStreamAsync(cancellationToken);
jstedfast marked this conversation as resolved.
Show resolved Hide resolved
jstedfast marked this conversation as resolved.
Show resolved Hide resolved

var callback = new ImageLoaderCallback();

Expand Down