Skip to content

Commit 7f8f512

Browse files
committed
Updated logic to ensure platform specific samples are inserted in order
1 parent 6fc488f commit 7f8f512

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

samples/MADE.Samples/MADE.Samples.Shared/Features/Home/ViewModels/MainPageViewModel.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ namespace MADE.Samples.Features.Home.ViewModels
55
using System.Windows.Input;
66
using CommunityToolkit.Mvvm.Input;
77
using CommunityToolkit.Mvvm.Messaging;
8-
using Foundation.Platform;
8+
using MADE.Collections;
9+
using MADE.Data.Validation.Extensions;
10+
using MADE.Foundation.Platform;
911
using MADE.Samples.Features.Samples.Data;
1012
using MADE.Samples.Features.Samples.Pages;
13+
using MADE.UI.ViewManagement;
1114
using MADE.UI.Views.Navigation;
1215
using MADE.UI.Views.Navigation.ViewModels;
13-
using UI.ViewManagement;
1416

1517
public class MainPageViewModel : PageViewModel
1618
{
@@ -49,24 +51,21 @@ private static ICollection<SampleGroup> GetSampleGroups()
4951
new Sample(
5052
"AppDialog",
5153
typeof(AppDialogPage),
52-
"/Features/Samples/Assets/AppDialog/AppDialog.png"),
54+
"/Features/Samples/Assets/AppDialog/AppDialog.png")
5355
}
5456
};
5557

5658
if (PlatformApiHelper.IsTypeSupported(typeof(WindowManager)))
5759
{
58-
helpers.Samples.Add(
60+
helpers.Samples.InsertAtPotentialIndex(
5961
new Sample(
6062
"WindowManager",
6163
typeof(WindowManagerPage),
62-
"/Features/Samples/Assets/WindowManager/WindowManager.png"));
64+
"/Features/Samples/Assets/WindowManager/WindowManager.png"),
65+
(item, compare) => compare.Name.IsLessThanOrEqualTo(item.Name));
6366
}
6467

65-
var list = new List<SampleGroup>
66-
{
67-
controls,
68-
helpers
69-
};
68+
var list = new List<SampleGroup> { controls, helpers };
7069

7170
return list;
7271
}

samples/MADE.Samples/MADE.Samples.Shared/Features/Samples/Data/SampleGroup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public class SampleGroup
66
{
77
public string Name { get; set; }
88

9-
public ICollection<Sample> Samples { get; } = new List<Sample>();
9+
public IList<Sample> Samples { get; } = new List<Sample>();
1010
}
1111
}

src/MADE.Collections/CollectionExtensions.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace MADE.Collections
66
using System;
77
using System.Collections.Generic;
88
using System.Linq;
9-
using System.Runtime.CompilerServices;
109

1110
/// <summary>
1211
/// Defines a collection of extensions for enumerables, lists, and collections.
@@ -242,5 +241,43 @@ public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, i
242241
.GroupBy(x => x.Index / chunkSize)
243242
.Select(x => x.Select(v => v.Value));
244243
}
244+
245+
/// <summary>Inserts an item to the specified <paramref name="source"/> at the potential index determined by the <paramref name="predicate"/>.</summary>
246+
/// <param name="source">The source where the <paramref name="value" /> should be inserted.</param>
247+
/// <param name="value">The object to insert into the <paramref name="source"/>.</param>
248+
/// <param name="predicate">The action to run to determine the position of the item based on the provided <paramref name="value"/> and an item in the collection.</param>
249+
/// <typeparam name="T">The type of items in the collection.</typeparam>
250+
/// <returns>The inserted index of the item.</returns>
251+
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1" /> is read-only.</exception>
252+
public static int InsertAtPotentialIndex<T>(this IList<T> source, T value, Func<T, T, bool> predicate)
253+
{
254+
var potentialIndex = source.PotentialIndexOf(value, predicate);
255+
source.Insert(potentialIndex, value);
256+
return potentialIndex;
257+
}
258+
259+
/// <summary>Gets the potential index of an item that does not currently exist within a collection based on the specified criteria.</summary>
260+
/// <param name="source">The collection to get the index from.</param>
261+
/// <param name="value">The object to determine an index for in the <paramref name="source"/>.</param>
262+
/// <param name="predicate">The action to run to determine the position of the item based on the provided <paramref name="value"/> and an item in the collection.</param>
263+
/// <typeparam name="T">The type of items in the collection.</typeparam>
264+
/// <returns>The potential index of the item.</returns>
265+
public static int PotentialIndexOf<T>(this IList<T> source, T value, Func<T, T, bool> predicate)
266+
{
267+
var result = 0;
268+
269+
foreach (var item in source)
270+
{
271+
if (predicate(value, item))
272+
{
273+
result = source.IndexOf(item) + 1;
274+
continue;
275+
}
276+
277+
break;
278+
}
279+
280+
return result;
281+
}
245282
}
246283
}

src/MADE.Data.Validation/Extensions/ComparableExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ public static bool IsGreaterThan<T>(this T value, T other)
2020
return value.CompareTo(other) > 0;
2121
}
2222

23+
/// <summary>
24+
/// Determines whether the value is greater than or equal to the <paramref name="other"/> value.
25+
/// </summary>
26+
/// <typeparam name="T">The <see cref="IComparable"/> type.</typeparam>
27+
/// <param name="value">The value to compare.</param>
28+
/// <param name="other">The value to compare against.</param>
29+
/// <returns>True if the <paramref name="value"/> is greater than or equal to the <paramref name="other"/> value.</returns>
30+
public static bool IsGreaterThanOrEqualTo<T>(this T value, T other)
31+
where T : IComparable
32+
{
33+
return value.CompareTo(other) >= 0;
34+
}
35+
2336
/// <summary>
2437
/// Determines whether the value is less than the <paramref name="other"/> value.
2538
/// </summary>
@@ -32,5 +45,18 @@ public static bool IsLessThan<T>(this T value, T other)
3245
{
3346
return value.CompareTo(other) < 0;
3447
}
48+
49+
/// <summary>
50+
/// Determines whether the value is less than or equal to the <paramref name="other"/> value.
51+
/// </summary>
52+
/// <typeparam name="T">The <see cref="IComparable"/> type.</typeparam>
53+
/// <param name="value">The value to compare.</param>
54+
/// <param name="other">The value to compare against.</param>
55+
/// <returns>True if the <paramref name="value"/> is less than or equal to the <paramref name="other"/> value.</returns>
56+
public static bool IsLessThanOrEqualTo<T>(this T value, T other)
57+
where T : IComparable
58+
{
59+
return value.CompareTo(other) <= 0;
60+
}
3561
}
3662
}

0 commit comments

Comments
 (0)