Skip to content

Commit f4deae3

Browse files
committed
chore: Add Storage Extensions from my other Repository
Since the Storage PR seems to take more time to get merged to Uno.Extensions, this way other Beginners could still lookup this
1 parent 1cf7170 commit f4deae3

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net9.0</TargetFramework>
4+
<OutputType>Library</OutputType>
5+
</PropertyGroup>
6+
<PropertyGroup>
7+
<AssemblyName>DevTKSS.Extensions.Uno</AssemblyName>
8+
<RootNamespace>DevTKSS.Extensions.Uno</RootNamespace>
9+
<VersionPrefix>1.0.0</VersionPrefix>
10+
<VersionSuffix>preview.1.0.0</VersionSuffix>
11+
<PackageVersion>$(Version)</PackageVersion>
12+
<PackageTags>Extensions</PackageTags>
13+
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
14+
<Description>Extensions that are extending Uno Platform functionallities</Description>
15+
</PropertyGroup>
16+
<ItemGroup>
17+
<PackageReference Include="Uno.Core" />
18+
<PackageReference Include="Uno.Core.Extensions" />
19+
<PackageReference Include="Uno.Core.Extensions.Collections" />
20+
<PackageReference Include="Uno.Extensions.Storage" />
21+
</ItemGroup>
22+
</Project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace DevTKSS.Extensions.Uno.Storage.Enumerable;
2+
3+
/// <summary>
4+
/// Provides <see cref="IEnumerable{TResult}"/> extension methods for working with <see cref="IStorage"/>
5+
/// </summary>
6+
public static class EnumerableExtensions
7+
{
8+
/// <summary>
9+
/// Selects string typed items from the input collection based on the specified ranges.
10+
/// </summary>
11+
/// <param name="source">The collection of string typed items to select from.</param>
12+
/// <param name="ranges">
13+
/// A collection of ranges, where each range is a tuple containing a start and end index.
14+
/// The start index specifies the first item to include, and the end index specifies the last item to include.
15+
/// </param>
16+
/// <param name="isNullBased">
17+
/// Indicates whether the range indices are 0-based (<c>true</c>) or 1-based (<c>false</c>).
18+
/// If <c>true</c>, the start and end indices are treated as 0-based; otherwise, they are treated as 1-based.
19+
/// </param>
20+
/// <returns>
21+
/// An enumerable collection of strings, where each string represents the items within a specified range.
22+
/// If a range is invalid (e.g. start is greater than the last one), an empty string is returned for that range.
23+
/// </returns>
24+
public static IEnumerable<string> SelectItemsByRanges(this IEnumerable<string> source, IEnumerable<(int Start, int End)> ranges, bool isNullBased = true)
25+
{
26+
if (source is null || ranges is null)
27+
{
28+
yield return string.Empty;
29+
yield break;
30+
}
31+
32+
if (!ranges.Any())
33+
{
34+
yield return source.JoinBy(Environment.NewLine);
35+
yield break;
36+
}
37+
38+
foreach (var range in ranges)
39+
{
40+
yield return source.GetItemsWithinRange(range, isNullBased);
41+
}
42+
}
43+
44+
/// <summary>
45+
/// Retrieves the concatenated items within the specified range as one single <see langword="string"/> joined by <see cref="Environment.NewLine"/> character.
46+
/// </summary>
47+
/// <param name="source">The <see cref="IEnumerable{TData}"/> to select from.</param>
48+
/// <param name="range">
49+
/// A tuple containing the start and end indices of the range as <see langword="int"/>.
50+
/// The start index specifies the first item to include, and the end index specifies the last item to include.
51+
/// </param>
52+
/// <param name="isNullBased">
53+
/// Indicates whether the range indices are 0-based (<c>true</c>) or 1-based (<c>false</c>).
54+
/// If <c>true</c>, the start and end indices are treated as 0-based; otherwise, they are treated as 1-based.
55+
/// </param>
56+
/// <returns>
57+
/// A string containing the string typed items of <paramref name="source"/> within the specified range, joined by the system's newline character.
58+
/// </returns>
59+
public static string GetItemsWithinRange(this IEnumerable<string> source, (int Start, int End) range, bool isNullBased = true) // TODO: Consider to limit int to min 0 value instead of implicit allowing negative.
60+
{
61+
if (source is null)
62+
{
63+
return string.Empty;
64+
}
65+
66+
var list = source as IList<string> ?? [.. source];
67+
if (list.Count == 0)
68+
{
69+
return string.Empty;
70+
}
71+
72+
var startIndex = Math.Clamp(
73+
value: range.Start - (isNullBased ? 0 : 1),
74+
min: 0,
75+
max: list.Count);
76+
77+
var endIndex = Math.Clamp(
78+
value: range.End - (isNullBased ? 0 : 1),
79+
min: startIndex,
80+
max: list.Count); // Ensure 'End' does not exceed available lines
81+
82+
return list.Skip(startIndex)
83+
.Take(endIndex - startIndex)
84+
.JoinBy(Environment.NewLine);
85+
}
86+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace DevTKSS.Extensions.Uno.Storage;
2+
public static class StorageExtensions
3+
{
4+
/// <summary>
5+
/// Reads specific lines from a file asynchronously based on the provided line ranges.
6+
/// </summary>
7+
/// <param name="storage">The storage interface used to access the file.</param>
8+
/// <param name="filePath">The path of the file to read from.</param>
9+
/// <param name="lineRanges">
10+
/// A collection of tuples representing the line ranges to extract.
11+
/// Each tuple contains a start line (inclusive) and an end line (inclusive).
12+
/// </param>
13+
/// <returns>
14+
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation.
15+
/// The result contains the extracted lines joined by the system's newline character,
16+
/// or the entire file content if no line ranges are specified.
17+
/// </returns>
18+
public static async ValueTask<string> ReadLinesFromPackageFile(this IStorage storage, string filePath, IEnumerable<(int, int)> lineRanges)
19+
{
20+
string fileContent = await storage.ReadPackageFileAsync(filePath) ?? string.Empty;
21+
if (!(lineRanges.Any()))
22+
{
23+
return fileContent;
24+
}
25+
26+
return fileContent.Split(Environment.NewLine)
27+
.SelectItemsByRanges(lineRanges,false)
28+
.JoinBy(Environment.NewLine);
29+
}
30+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
global using DevTKSS.Extensions.Uno.Storage.Enumerable;
2+
global using DevTKSS.Extensions.Uno.Storage;

src/DevTKSS.Uno.Samples.MvuxGallery/DevTKSS.Uno.Samples.MvuxGallery.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
</PropertyGroup>
5050
<ItemGroup>
5151
<PackageReference Include="CommunityToolkit.WinUI.Converters" />
52-
<PackageReference Include="DevTKSS.Extensions.Uno" />
52+
<!--<PackageReference Include="DevTKSS.Extensions.Uno" /> -->
53+
<ProjectReference Include="..\DevTKSS.Extensions.Uno\DevTKSS.Extensions.Uno.csproj" />
5354
</ItemGroup>
5455
<ItemGroup>
5556
<UnoImage Include="Assets\Images\*" />

0 commit comments

Comments
 (0)