From 23deea8230908e63519948a169ecb8120784f7bc Mon Sep 17 00:00:00 2001 From: hansmbakker Date: Tue, 6 Oct 2020 22:32:30 +0200 Subject: [PATCH 1/5] Mutable and subclassable version of ObservableGroup --- .../Collections/ObservableGroup.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Microsoft.Toolkit/Collections/ObservableGroup.cs b/Microsoft.Toolkit/Collections/ObservableGroup.cs index 304be89e496..542f943d7ef 100644 --- a/Microsoft.Toolkit/Collections/ObservableGroup.cs +++ b/Microsoft.Toolkit/Collections/ObservableGroup.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Diagnostics; using System.Linq; @@ -16,8 +17,10 @@ namespace Microsoft.Toolkit.Collections /// The type of the group key. /// The type of the items in the collection. [DebuggerDisplay("Key = {Key}, Count = {Count}")] - public sealed class ObservableGroup : ObservableCollection, IGrouping, IReadOnlyObservableGroup + public class ObservableGroup : ObservableCollection, IGrouping, IReadOnlyObservableGroup { + private TKey _key; + /// /// Initializes a new instance of the class. /// @@ -49,9 +52,21 @@ public ObservableGroup(TKey key, IEnumerable collection) } /// - /// Gets the key of the group. + /// Gets or sets the key of the group. /// - public TKey Key { get; } + public TKey Key + { + get + { + return _key; + } + + set + { + _key = value; + OnPropertyChanged(new PropertyChangedEventArgs(nameof(Key))); + } + } /// object IReadOnlyObservableGroup.Key => Key; From f95595d06b130161220c8c83040fae9c27ae582f Mon Sep 17 00:00:00 2001 From: Hans Bakker Date: Wed, 7 Oct 2020 09:48:31 +0200 Subject: [PATCH 2/5] Prevent unnecessary OnPropertyChanged Co-authored-by: Sergio Pedri --- Microsoft.Toolkit/Collections/ObservableGroup.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Microsoft.Toolkit/Collections/ObservableGroup.cs b/Microsoft.Toolkit/Collections/ObservableGroup.cs index 542f943d7ef..996a90ecc9b 100644 --- a/Microsoft.Toolkit/Collections/ObservableGroup.cs +++ b/Microsoft.Toolkit/Collections/ObservableGroup.cs @@ -60,11 +60,14 @@ public TKey Key { return _key; } - set { - _key = value; - OnPropertyChanged(new PropertyChangedEventArgs(nameof(Key))); + if (!EqualityComparer.Default.Equals(this.key, value)) + { + this.key = value; + + OnPropertyChanged(new PropertyChangedEventArgs(nameof(Key))); + } } } From 46d9d343dfef339bc74b55a4e51986f64aebb8bd Mon Sep 17 00:00:00 2001 From: Hans Bakker Date: Wed, 7 Oct 2020 09:48:48 +0200 Subject: [PATCH 3/5] Code style suggestion Co-authored-by: Sergio Pedri --- Microsoft.Toolkit/Collections/ObservableGroup.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Microsoft.Toolkit/Collections/ObservableGroup.cs b/Microsoft.Toolkit/Collections/ObservableGroup.cs index 996a90ecc9b..78a5a8bbcaf 100644 --- a/Microsoft.Toolkit/Collections/ObservableGroup.cs +++ b/Microsoft.Toolkit/Collections/ObservableGroup.cs @@ -56,10 +56,7 @@ public ObservableGroup(TKey key, IEnumerable collection) /// public TKey Key { - get - { - return _key; - } + get => this.key; set { if (!EqualityComparer.Default.Equals(this.key, value)) From eeb1ff98019f4ddee1919295030abd522065c0e7 Mon Sep 17 00:00:00 2001 From: Hans Bakker Date: Wed, 7 Oct 2020 09:48:57 +0200 Subject: [PATCH 4/5] Code style suggestion Co-authored-by: Sergio Pedri --- Microsoft.Toolkit/Collections/ObservableGroup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Collections/ObservableGroup.cs b/Microsoft.Toolkit/Collections/ObservableGroup.cs index 78a5a8bbcaf..a247fe6895f 100644 --- a/Microsoft.Toolkit/Collections/ObservableGroup.cs +++ b/Microsoft.Toolkit/Collections/ObservableGroup.cs @@ -19,7 +19,7 @@ namespace Microsoft.Toolkit.Collections [DebuggerDisplay("Key = {Key}, Count = {Count}")] public class ObservableGroup : ObservableCollection, IGrouping, IReadOnlyObservableGroup { - private TKey _key; + private TKey key; /// /// Initializes a new instance of the class. From b601e57d89f665ac0211cf64ebcde504bac16aa7 Mon Sep 17 00:00:00 2001 From: hansmbakker Date: Wed, 7 Oct 2020 14:34:09 +0200 Subject: [PATCH 5/5] code styling and perf improvement --- Microsoft.Toolkit/Collections/ObservableGroup.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Microsoft.Toolkit/Collections/ObservableGroup.cs b/Microsoft.Toolkit/Collections/ObservableGroup.cs index a247fe6895f..ce828934917 100644 --- a/Microsoft.Toolkit/Collections/ObservableGroup.cs +++ b/Microsoft.Toolkit/Collections/ObservableGroup.cs @@ -19,7 +19,10 @@ namespace Microsoft.Toolkit.Collections [DebuggerDisplay("Key = {Key}, Count = {Count}")] public class ObservableGroup : ObservableCollection, IGrouping, IReadOnlyObservableGroup { - private TKey key; + /// + /// The cached for + /// + private static readonly PropertyChangedEventArgs KeyChangedEventArgs = new PropertyChangedEventArgs(nameof(Key)); /// /// Initializes a new instance of the class. @@ -51,6 +54,8 @@ public ObservableGroup(TKey key, IEnumerable collection) Key = key; } + private TKey key; + /// /// Gets or sets the key of the group. /// @@ -62,8 +67,8 @@ public TKey Key if (!EqualityComparer.Default.Equals(this.key, value)) { this.key = value; - - OnPropertyChanged(new PropertyChangedEventArgs(nameof(Key))); + + OnPropertyChanged(KeyChangedEventArgs); } } }