Skip to content

Commit

Permalink
fix(assets): Adjust assets caching by including assembly and package …
Browse files Browse the repository at this point in the history
…versions

(cherry picked from commit 57a48eb)

# Conflicts:
#	src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_Image.cs
  • Loading branch information
jeromelaban authored and mergify[bot] committed Jul 26, 2023
1 parent eeb30d9 commit d7317ff
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/Uno.UI.RuntimeTests/Assets/help.svg
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 @@ -450,19 +450,29 @@ public async Task When_Loaded_From_AppData_LocalFolder()

[TestMethod]
[RunsOnUIThread]
public async Task When_SVGImageSource()
[DataRow("ms-appx:///Assets/couch.svg")]
[DataRow("ms-appx:///Uno.UI.RuntimeTests/Assets/couch.svg")]
[DataRow("ms-appx:///Uno.UI.RuntimeTests/Assets/help.svg")]
public async Task When_SVGImageSource(string imagePath)
{
if (!ApiInformation.IsTypePresent("Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap"))
{
Assert.Inconclusive(); // System.NotImplementedException: RenderTargetBitmap is not supported on this platform.;
Assert.Inconclusive("RenderTargetBitmap is not supported on this platform");
}

<<<<<<< HEAD
var svgImageSource = new SvgImageSource(new Uri("ms-appx:///Assets/couch.svg"));
=======
var svgImageSource = new SvgImageSource(new Uri(imagePath));
>>>>>>> 57a48eb8f6 (fix(assets): Adjust assets caching by including assembly and package versions)
var image = new Image() { Source = svgImageSource, Width = 100, Height = 100 };
TestServices.WindowHelper.WindowContent = image;
await WindowHelper.WaitForLoaded(image);
}
<<<<<<< HEAD

=======
>>>>>>> 57a48eb8f6 (fix(assets): Adjust assets caching by including assembly and package versions)

[TestMethod]
[RunsOnUIThread]
Expand Down
26 changes: 22 additions & 4 deletions src/Uno.UWP/Storage/StorageFile.Android.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

using System;
using System.Globalization;
using System.IO;
Expand All @@ -15,12 +17,14 @@
using Windows.Storage.Streams;
using Windows.Storage.Helpers;
using Uno.Helpers;
using Windows.ApplicationModel.Background;

namespace Windows.Storage
{
partial class StorageFile
{
private static ConcurrentEntryManager _assetGate = new ConcurrentEntryManager();
private static string? _currentAppID;

private static async Task<StorageFile> GetFileFromApplicationUri(CancellationToken ct, Uri uri)
{
Expand All @@ -34,8 +38,10 @@ private static async Task<StorageFile> GetFileFromApplicationUri(CancellationTok

var path = AndroidResourceNameEncoder.EncodeResourcePath(originalPath);

EnsureAppID();

// Read the contents of our asset
var outputCachePath = global::System.IO.Path.Combine(Android.App.Application.Context.CacheDir.AbsolutePath, path);
var outputCachePath = global::System.IO.Path.Combine(Android.App.Application.Context.CacheDir!.AbsolutePath, _currentAppID, path);

if (typeof(StorageFile).Log().IsEnabled(Uno.Foundation.Logging.LogLevel.Debug))
{
Expand All @@ -50,18 +56,18 @@ private static async Task<StorageFile> GetFileFromApplicationUri(CancellationTok

if (!File.Exists(outputCachePath))
{
Directory.CreateDirectory(global::System.IO.Path.GetDirectoryName(outputCachePath));
Directory.CreateDirectory(global::System.IO.Path.GetDirectoryName(outputCachePath)!);

Stream GetAsset()
{
if (DrawableHelper.FindResourceIdFromPath(path) is { } resourceId)
{
return ContextHelper.Current.Resources.OpenRawResource(resourceId);
return ContextHelper.Current.Resources!.OpenRawResource(resourceId);
}
else
{
var assets = global::Android.App.Application.Context.Assets;
return assets.Open(path);
return assets!.Open(path);
}
}

Expand All @@ -73,5 +79,17 @@ Stream GetAsset()

return await StorageFile.GetFileFromPathAsync(outputCachePath);
}

private static void EnsureAppID()
{
if (_currentAppID is null)
{
var packageVersion = ApplicationModel.Package.Current.Id.Version;

_currentAppID =
ContextHelper.Current.GetType().Assembly.GetModules().First().ModuleVersionId.ToString()
+ $"_{packageVersion.Major}.{packageVersion.Minor}.{packageVersion.Build}.{packageVersion.Revision}";
}
}
}
}

0 comments on commit d7317ff

Please sign in to comment.