This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 457
Merge Grouping
classes from Refractored.MvvmHelpers
#957
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/ObjectModel/Grouping_Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Linq; | ||
using Xamarin.CommunityToolkit.ObjectModel; | ||
using Xunit; | ||
|
||
namespace Xamarin.CommunityToolkit.UnitTests.ObjectModel | ||
{ | ||
public sealed class Grouping_Tests | ||
{ | ||
readonly Person[] people; | ||
|
||
public Grouping_Tests() | ||
{ | ||
people = new[] | ||
{ | ||
new Person { FirstName = "Joseph", LastName = "Hill" }, | ||
new Person { FirstName = "James", LastName = "Montemagno" }, | ||
new Person { FirstName = "Pierce", LastName = "Boggan" }, | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void Grouping() | ||
{ | ||
var sorted = from person in people | ||
orderby person.FirstName | ||
group person by person.Group | ||
into personGroup | ||
select new Grouping<string, Person>(personGroup.Key, personGroup); | ||
var grouped = new ObservableRangeCollection<Grouping<string, Person>>(); | ||
grouped.AddRange(sorted); | ||
|
||
Assert.Equal(2, grouped.Count); | ||
Assert.Equal("J", grouped[0].Key); | ||
Assert.Equal(2, grouped[0].Count); | ||
Assert.Single(grouped[1]); | ||
Assert.Equal(2, grouped[0].Items.Count); | ||
Assert.Single(grouped[1].Items); | ||
} | ||
|
||
[Fact] | ||
public void GroupingSubKey() | ||
{ | ||
var sorted = from person in people | ||
orderby person.FirstName | ||
group person by person.Group | ||
into personGroup | ||
select new Grouping<string, string, Person>(personGroup.Key, personGroup.Key, personGroup); | ||
var grouped = new ObservableRangeCollection<Grouping<string, string, Person>>(); | ||
grouped.AddRange(sorted); | ||
|
||
Assert.Equal(2, grouped.Count); | ||
Assert.Equal("J", grouped[0].SubKey); | ||
Assert.Equal("J", grouped[0].Key); | ||
Assert.Equal(2, grouped[0].Count); | ||
Assert.Single(grouped[1]); | ||
Assert.Equal(2, grouped[0].Items.Count); | ||
Assert.Single(grouped[1].Items); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/ObjectModel/Person.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using Xamarin.CommunityToolkit.ObjectModel; | ||
|
||
namespace Xamarin.CommunityToolkit.UnitTests.ObjectModel | ||
{ | ||
class Person : ObservableObject | ||
{ | ||
string firstName; | ||
string lastName; | ||
|
||
public Action Changed { get; set; } | ||
|
||
public Action Changing { get; set; } | ||
|
||
public Func<string, string, bool> Validate { get; set; } | ||
|
||
public string FirstName | ||
{ | ||
get => firstName; | ||
set => SetProperty(ref firstName, value, onChanged: Changed, onChanging: Changing, validateValue: Validate); | ||
} | ||
|
||
public string LastName | ||
{ | ||
get => lastName; | ||
set => SetProperty(ref lastName, value, onChanged: Changed, onChanging: Changing, validateValue: Validate); | ||
} | ||
|
||
public string Group => FirstName[0].ToString().ToUpperInvariant(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Grouping.shared.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Collections.Generic; | ||
|
||
#nullable enable | ||
|
||
namespace Xamarin.CommunityToolkit.ObjectModel | ||
{ | ||
/// <summary> | ||
/// Grouping of items by key into ObservableRange | ||
/// </summary> | ||
public class Grouping<TKey, TItem> : ObservableRangeCollection<TItem> | ||
{ | ||
/// <summary> | ||
/// Gets the key. | ||
/// </summary> | ||
/// <value>The key.</value> | ||
public TKey Key { get; } | ||
|
||
/// <summary> | ||
/// Returns list of items in the grouping. | ||
/// </summary> | ||
public new IList<TItem> Items => base.Items; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the Grouping class. | ||
/// </summary> | ||
/// <param name="key">Key.</param> | ||
/// <param name="items">Items.</param> | ||
public Grouping(TKey key, IEnumerable<TItem> items) | ||
{ | ||
Key = key; | ||
AddRange(items); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Grouping of items by key and subkey into ObservableRange | ||
/// </summary> | ||
public class Grouping<TKey, TSubKey, TItem> : ObservableRangeCollection<TItem> | ||
{ | ||
/// <summary> | ||
/// Gets the key. | ||
/// </summary> | ||
/// <value>The key.</value> | ||
public TKey Key { get; } | ||
|
||
/// <summary> | ||
/// Gets the subkey of the grouping | ||
/// </summary> | ||
public TSubKey SubKey { get; } | ||
|
||
/// <summary> | ||
/// Returns list of items in the grouping. | ||
/// </summary> | ||
public new IList<TItem> Items => base.Items; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the Grouping class. | ||
/// </summary> | ||
/// <param name="key">Key.</param> | ||
/// <param name="subkey">Subkey</param> | ||
/// <param name="items">Items.</param> | ||
public Grouping(TKey key, TSubKey subkey, IEnumerable<TItem> items) | ||
{ | ||
Key = key; | ||
SubKey = subkey; | ||
AddRange(items); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<3