From 2a34725c5cdac6fda7832c21645efbae617ab047 Mon Sep 17 00:00:00 2001 From: Rafael Rosa Date: Wed, 17 Apr 2024 12:27:11 -0300 Subject: [PATCH 1/3] chore: add StorageFileHelper GetAllFiles --- .../Given_ApplicationStorage.cs | 27 ++++++++++++++++++ .../Storage/StorageFileHelper.Android.cs | 21 ++++++++++++++ .../Storage/StorageFileHelper.cs | 28 +++++++++++++++++++ .../Storage/StorageFileHelper.iOSmacOS.cs | 11 ++++++++ .../Storage/StorageFileHelper.wasm.cs | 8 ++++++ 5 files changed, 95 insertions(+) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_ApplicationStorage.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_ApplicationStorage.cs index 7b1ac14c76e6..2ae46f353cca 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_ApplicationStorage.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_ApplicationStorage.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Uno.Extensions.Specialized; using Windows.Storage; namespace Uno.UI.RuntimeTests.Tests.Windows_Storage @@ -8,6 +9,32 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_Storage public class Given_ApplicationStorage { + [TestMethod] + public async Task When_ExistFilesInPackage() + { + var fileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage(null); + + Assert.IsTrue(fileExists.Any()); + } + + [TestMethod] + public async Task When_ExtensionsFilterCountDifferentFromAllInPackage() + { + var filteredFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage([".png"]); + var allFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage(null); + + Assert.IsFalse(allFileExists.Count() == filteredFileExists.Count()); + } + + [TestMethod] + public async Task When_ExtensionsFilterOnlyPathsForPngFiles() + { + var filteredFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage([".png"]); + var allFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage(null); + + Assert.IsTrue(filteredFileExists.Count() == filteredFileExists.Where(e => e.ToString().EndsWith(".png")).Count()); + } + [TestMethod] public async Task When_FileDoesNotExistsInPackage() { diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs index ce0e005f0c0b..e07d673d1d37 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs @@ -88,5 +88,26 @@ private static bool ScanPackageAssets(ICollection scannedFiles, string r return false; } } + + /// + /// This method will return all the assets within current package + /// + /// Extensions list for filter files + private static Task GetFilesInPackage(string[]? extensionsFilter) + { + var context = global::Android.App.Application.Context; + + if (ScannedFiles is null) + { + ScannedFiles = new List(); + _ = ScanPackageAssetsAsync(ScannedFiles); + } + + var results = ScannedFiles?.ToList() + .Where(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))) + .ToArray() ?? Array.Empty(); + + return Task.FromResult(results); + } } diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs index 1c4d02f78baf..29746e3335e7 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.Collections.Generic; using System.IO; +using System.Linq; namespace Uno.UI.Toolkit; @@ -18,9 +19,18 @@ public partial class StorageFileHelper /// A task that will complete with a result of true if file exists, otherwise with a result of false. public static async Task ExistsInPackage(string fileName) => await FileExistsInPackage(fileName); + /// + /// Get all files in the package + /// + /// List of string with the Paths - Filtered by extensionsFilter list + public static async Task GetAllFilesPathInPackage(string[] extensionsFilter) => await GetFilesInPackage(extensionsFilter); + #if IS_UNIT_TESTS || __NETSTD_REFERENCE__ private static Task FileExistsInPackage(string fileName) => throw new NotImplementedException(); + + private static Task GetAllFilesPathInPackage(string[] extensionsFilter) + => throw new NotImplementedException(); #endif #if __SKIA__ || WINDOWS || WINAPPSDK || WINDOWS_UWP || WINUI @@ -39,5 +49,23 @@ private static Task FileExistsInPackage(string fileName) return Task.FromResult(false); } + + private static Task GetFilesInPackage(string[]? extensionsFilter) + { + List assetsFiles = new(); + string path = string.Empty; + var executingPath = Assembly.GetExecutingAssembly().Location; + if (!string.IsNullOrEmpty(executingPath)) + { + path = Path.GetDirectoryName(executingPath) + string.Empty; + if (!string.IsNullOrEmpty(path)) + { + assetsFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories) + .Where(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))) + .ToList(); + } + } + return Task.FromResult(assetsFiles.ToArray()); + } #endif } diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs index 6b45878d507a..7dbae6c7a5a3 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs @@ -22,4 +22,15 @@ private static Task FileExistsInPackage(string fileName) return Task.FromResult(resourcePathname != null); } + + private static Task GetFilesInPackage(string[]? extensionsFilter) + { + string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*", SearchOption.AllDirectories); + + var results = files?.ToList() + .Where(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))) + .ToArray() ?? Array.Empty(); + + return Task.FromResult(results); + } } diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs index 1afc322404e4..454d5a538e04 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs @@ -12,4 +12,12 @@ private static async Task FileExistsInPackage(string fileName) var assets = await AssetsManager.Assets.Value; return assets?.Contains(fileName) ?? false; } + + private static async Task GetFilesInPackage(string[]? extensionsFilter) + { + var assets = await AssetsManager.Assets.Value; + return assets?.ToList() + .Where(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))) + .ToArray() ?? Array.Empty(); + } } From ad65656ba7c7af75568bc8f692bf1b0081f16a4e Mon Sep 17 00:00:00 2001 From: Rafael Rosa Date: Wed, 17 Apr 2024 15:57:58 -0300 Subject: [PATCH 2/3] refactor: update GetFilesInDirectoryAsync --- .../Given_ApplicationStorage.cs | 10 +++++----- .../Storage/StorageFileHelper.Android.cs | 20 +++++++++++-------- .../Storage/StorageFileHelper.cs | 15 ++++++++------ .../Storage/StorageFileHelper.iOSmacOS.cs | 9 +++++---- .../Storage/StorageFileHelper.wasm.cs | 11 ++++++---- 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_ApplicationStorage.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_ApplicationStorage.cs index 2ae46f353cca..451882c15f8e 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_ApplicationStorage.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_ApplicationStorage.cs @@ -12,7 +12,7 @@ public class Given_ApplicationStorage [TestMethod] public async Task When_ExistFilesInPackage() { - var fileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage(null); + var fileExists = await Uno.UI.Toolkit.StorageFileHelper.GetFilesInDirectoryAsync(null); Assert.IsTrue(fileExists.Any()); } @@ -20,8 +20,8 @@ public async Task When_ExistFilesInPackage() [TestMethod] public async Task When_ExtensionsFilterCountDifferentFromAllInPackage() { - var filteredFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage([".png"]); - var allFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage(null); + var filteredFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetFilesInDirectoryAsync([".png"]); + var allFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetFilesInDirectoryAsync(null); Assert.IsFalse(allFileExists.Count() == filteredFileExists.Count()); } @@ -29,8 +29,8 @@ public async Task When_ExtensionsFilterCountDifferentFromAllInPackage() [TestMethod] public async Task When_ExtensionsFilterOnlyPathsForPngFiles() { - var filteredFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage([".png"]); - var allFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetAllFilesPathInPackage(null); + var filteredFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetFilesInDirectoryAsync([".png"]); + var allFileExists = await Uno.UI.Toolkit.StorageFileHelper.GetFilesInDirectoryAsync(null); Assert.IsTrue(filteredFileExists.Count() == filteredFileExists.Where(e => e.ToString().EndsWith(".png")).Count()); } diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs index e07d673d1d37..faed9bdfe7fa 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs @@ -5,6 +5,7 @@ using System.Threading; using Android.Content.Res; using System.Threading.Tasks; +using System; namespace Uno.UI.Toolkit; @@ -90,21 +91,24 @@ private static bool ScanPackageAssets(ICollection scannedFiles, string r } /// - /// This method will return all the assets within current package + /// Retrieves the paths of assets within the current Android application package based on the specified filter predicate. /// - /// Extensions list for filter files - private static Task GetFilesInPackage(string[]? extensionsFilter) + /// A predicate function determining whether a file should be included in the result. + /// + /// Returns an array of strings containing the paths of the filtered assets. + /// + private static Task GetFilesInDirectory(Func predicate) { var context = global::Android.App.Application.Context; - if (ScannedFiles is null) + if (_scannedFiles is null) { - ScannedFiles = new List(); - _ = ScanPackageAssetsAsync(ScannedFiles); + _scannedFiles = new List(); + _ = ScanPackageAssets(_scannedFiles); } - var results = ScannedFiles?.ToList() - .Where(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))) + var results = _scannedFiles?.Where(e => predicate(e)) + .Select(e => e.Replace('\\', '/')) .ToArray() ?? Array.Empty(); return Task.FromResult(results); diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs index 29746e3335e7..4c5635d87486 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs @@ -20,16 +20,18 @@ public partial class StorageFileHelper public static async Task ExistsInPackage(string fileName) => await FileExistsInPackage(fileName); /// - /// Get all files in the package + /// This asynchronous method retrieves the paths of files within a directory based on a specified filter for file extensions. /// - /// List of string with the Paths - Filtered by extensionsFilter list - public static async Task GetAllFilesPathInPackage(string[] extensionsFilter) => await GetFilesInPackage(extensionsFilter); + /// An array of strings representing the file extensions to filter the files.If null, all files in the directory are considered. + /// An array of strings containing the paths of the filtered files within the directory. + public static async Task GetFilesInDirectoryAsync(string[] extensionsFilter) + => await GetFilesInDirectory(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))); #if IS_UNIT_TESTS || __NETSTD_REFERENCE__ private static Task FileExistsInPackage(string fileName) => throw new NotImplementedException(); - private static Task GetAllFilesPathInPackage(string[] extensionsFilter) + private static Task GetFilesInDirectory(Func predicate) => throw new NotImplementedException(); #endif @@ -50,7 +52,7 @@ private static Task FileExistsInPackage(string fileName) return Task.FromResult(false); } - private static Task GetFilesInPackage(string[]? extensionsFilter) + private static Task GetFilesInDirectory(Func predicate) { List assetsFiles = new(); string path = string.Empty; @@ -61,7 +63,8 @@ private static Task GetFilesInPackage(string[]? extensionsFilter) if (!string.IsNullOrEmpty(path)) { assetsFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories) - .Where(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))) + .Where(e => predicate(e)) + .Select(e => e.Replace(path, string.Empty).Replace('\\', '/')) .ToList(); } } diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs index 7dbae6c7a5a3..75815fd389fb 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs @@ -23,12 +23,13 @@ private static Task FileExistsInPackage(string fileName) return Task.FromResult(resourcePathname != null); } - private static Task GetFilesInPackage(string[]? extensionsFilter) + private static Task GetFilesInDirectory(Func predicate) { - string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*", SearchOption.AllDirectories); + string rootPath = AppDomain.CurrentDomain.BaseDirectory; + string[] files = Directory.GetFiles(rootPath, "*", SearchOption.AllDirectories); - var results = files?.ToList() - .Where(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))) + var results = files?.Where(e => predicate(e)) + .Select(e => e.Replace(rootPath, string.Empty).Replace('\\', '/')) .ToArray() ?? Array.Empty(); return Task.FromResult(results); diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs index 454d5a538e04..95d4da51cbc9 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs @@ -1,4 +1,6 @@ #nullable enable +using System; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Windows.Storage.Helpers; @@ -13,11 +15,12 @@ private static async Task FileExistsInPackage(string fileName) return assets?.Contains(fileName) ?? false; } - private static async Task GetFilesInPackage(string[]? extensionsFilter) + private static async Task GetFilesInDirectory(Func predicate) { var assets = await AssetsManager.Assets.Value; - return assets?.ToList() - .Where(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))) - .ToArray() ?? Array.Empty(); + + return assets?.Where(e => predicate(e)) + .Select(e => e.Replace('\\', '/')) + .ToArray() ?? Array.Empty(); } } From 3547ce32d119d795a575decc4d5650bebfa5d1b1 Mon Sep 17 00:00:00 2001 From: Rafael Rosa Date: Wed, 17 Apr 2024 16:06:21 -0300 Subject: [PATCH 3/3] chore: add summary and trim --- src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs | 4 +--- src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs | 7 ++++++- src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs | 5 +++++ src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs | 5 +++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs index faed9bdfe7fa..671953a77aec 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.Android.cs @@ -94,9 +94,7 @@ private static bool ScanPackageAssets(ICollection scannedFiles, string r /// Retrieves the paths of assets within the current Android application package based on the specified filter predicate. /// /// A predicate function determining whether a file should be included in the result. - /// - /// Returns an array of strings containing the paths of the filtered assets. - /// + /// Returns an array of strings containing the paths of the filtered assets. private static Task GetFilesInDirectory(Func predicate) { var context = global::Android.App.Application.Context; diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs index 4c5635d87486..beceeeb0d7e7 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.cs @@ -52,6 +52,11 @@ private static Task FileExistsInPackage(string fileName) return Task.FromResult(false); } + /// + /// Retrieves the paths of assets within the current application based on the specified filter predicate. + /// + /// A predicate function determining whether a file should be included in the result. + /// Returns an array of strings containing the paths of the filtered assets. private static Task GetFilesInDirectory(Func predicate) { List assetsFiles = new(); @@ -64,7 +69,7 @@ private static Task GetFilesInDirectory(Func predicate) { assetsFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories) .Where(e => predicate(e)) - .Select(e => e.Replace(path, string.Empty).Replace('\\', '/')) + .Select(e => e.Replace(path, string.Empty).Replace('\\', '/').TrimStart('/')) .ToList(); } } diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs index 75815fd389fb..6bcc8723f1b2 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.iOSmacOS.cs @@ -23,6 +23,11 @@ private static Task FileExistsInPackage(string fileName) return Task.FromResult(resourcePathname != null); } + /// + /// Retrieves the paths of assets within the current application based on the specified filter predicate. + /// + /// A predicate function determining whether a file should be included in the result. + /// Returns an array of strings containing the paths of the filtered assets. private static Task GetFilesInDirectory(Func predicate) { string rootPath = AppDomain.CurrentDomain.BaseDirectory; diff --git a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs index 95d4da51cbc9..2d609aee99a4 100644 --- a/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs +++ b/src/Uno.UI.Toolkit/Storage/StorageFileHelper.wasm.cs @@ -15,6 +15,11 @@ private static async Task FileExistsInPackage(string fileName) return assets?.Contains(fileName) ?? false; } + /// + /// Retrieves the paths of assets within the current application based on the specified filter predicate. + /// + /// A predicate function determining whether a file should be included in the result. + /// Returns an array of strings containing the paths of the filtered assets. private static async Task GetFilesInDirectory(Func predicate) { var assets = await AssetsManager.Assets.Value;