|
8 | 8 | using DynamicData.Cache; |
9 | 9 | using DynamicData.Cache.Internal; |
10 | 10 |
|
11 | | -namespace DynamicData.Binding |
| 11 | +namespace DynamicData.Binding; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Adaptor to reflect a change set into a binding list. |
| 15 | +/// </summary> |
| 16 | +/// <typeparam name="T">The type of items.</typeparam> |
| 17 | +/// <remarks> |
| 18 | +/// Initializes a new instance of the <see cref="BindingListAdaptor{T}"/> class. |
| 19 | +/// </remarks> |
| 20 | +/// <param name="list">The list of items to add to the adapter.</param> |
| 21 | +/// <param name="refreshThreshold">The threshold before a reset is issued.</param> |
| 22 | +public class BindingListAdaptor<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(BindingList<T> list, int refreshThreshold = BindingOptions.DefaultResetThreshold) : IChangeSetAdaptor<T> |
| 23 | + where T : notnull |
12 | 24 | { |
13 | | - /// <summary> |
14 | | - /// Adaptor to reflect a change set into a binding list. |
15 | | - /// </summary> |
16 | | - /// <typeparam name="T">The type of items.</typeparam> |
17 | | - /// <remarks> |
18 | | - /// Initializes a new instance of the <see cref="BindingListAdaptor{T}"/> class. |
19 | | - /// </remarks> |
20 | | - /// <param name="list">The list of items to add to the adapter.</param> |
21 | | - /// <param name="refreshThreshold">The threshold before a reset is issued.</param> |
22 | | - public class BindingListAdaptor<T>(BindingList<T> list, int refreshThreshold = BindingOptions.DefaultResetThreshold) : IChangeSetAdaptor<T> |
23 | | - where T : notnull |
| 25 | + private readonly BindingList<T> _list = list ?? throw new ArgumentNullException(nameof(list)); |
| 26 | + private bool _loaded; |
| 27 | + |
| 28 | + /// <inheritdoc /> |
| 29 | + public void Adapt(IChangeSet<T> changes) |
24 | 30 | { |
25 | | - private readonly BindingList<T> _list = list ?? throw new ArgumentNullException(nameof(list)); |
26 | | - private bool _loaded; |
| 31 | + changes.ThrowArgumentNullExceptionIfNull(nameof(changes)); |
27 | 32 |
|
28 | | - /// <inheritdoc /> |
29 | | - public void Adapt(IChangeSet<T> changes) |
| 33 | + if (changes.TotalChanges - changes.Refreshes > refreshThreshold || !_loaded) |
30 | 34 | { |
31 | | - changes.ThrowArgumentNullExceptionIfNull(nameof(changes)); |
32 | | - |
33 | | - if (changes.TotalChanges - changes.Refreshes > refreshThreshold || !_loaded) |
34 | | - { |
35 | | - using (new BindingListEventsSuspender<T>(_list)) |
36 | | - { |
37 | | - _list.Clone(changes); |
38 | | - _loaded = true; |
39 | | - } |
40 | | - } |
41 | | - else |
| 35 | + using (new BindingListEventsSuspender<T>(_list)) |
42 | 36 | { |
43 | 37 | _list.Clone(changes); |
| 38 | + _loaded = true; |
44 | 39 | } |
45 | 40 | } |
| 41 | + else |
| 42 | + { |
| 43 | + _list.Clone(changes); |
| 44 | + } |
46 | 45 | } |
| 46 | +} |
47 | 47 |
|
48 | | - /// <summary> |
49 | | - /// Adaptor to reflect a change set into a binding list. |
50 | | - /// </summary> |
51 | | - /// <typeparam name="TObject">The type of the object.</typeparam> |
52 | | - /// <typeparam name="TKey">The type of the key.</typeparam> |
53 | | - /// <remarks> |
54 | | - /// Initializes a new instance of the <see cref="BindingListAdaptor{TObject, TKey}"/> class. |
55 | | - /// </remarks> |
56 | | - /// <param name="list">The list of items to adapt.</param> |
57 | | - /// <param name="refreshThreshold">The threshold before the refresh is triggered.</param> |
58 | | - [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Same class name, different generics")] |
59 | | - public class BindingListAdaptor<TObject, TKey>(BindingList<TObject> list, int refreshThreshold = BindingOptions.DefaultResetThreshold) : IChangeSetAdaptor<TObject, TKey> |
60 | | - where TObject : notnull |
61 | | - where TKey : notnull |
62 | | - { |
63 | | - private readonly Cache<TObject, TKey> _cache = new(); |
| 48 | +/// <summary> |
| 49 | +/// Adaptor to reflect a change set into a binding list. |
| 50 | +/// </summary> |
| 51 | +/// <typeparam name="TObject">The type of the object.</typeparam> |
| 52 | +/// <typeparam name="TKey">The type of the key.</typeparam> |
| 53 | +/// <remarks> |
| 54 | +/// Initializes a new instance of the <see cref="BindingListAdaptor{TObject, TKey}"/> class. |
| 55 | +/// </remarks> |
| 56 | +/// <param name="list">The list of items to adapt.</param> |
| 57 | +/// <param name="refreshThreshold">The threshold before the refresh is triggered.</param> |
| 58 | +[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Same class name, different generics")] |
| 59 | +public class BindingListAdaptor<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TObject, TKey>(BindingList<TObject> list, int refreshThreshold = BindingOptions.DefaultResetThreshold) : IChangeSetAdaptor<TObject, TKey> |
| 60 | + where TObject : notnull |
| 61 | + where TKey : notnull |
| 62 | +{ |
| 63 | + private readonly Cache<TObject, TKey> _cache = new(); |
64 | 64 |
|
65 | | - private readonly BindingList<TObject> _list = list ?? throw new ArgumentNullException(nameof(list)); |
66 | | - private bool _loaded; |
| 65 | + private readonly BindingList<TObject> _list = list ?? throw new ArgumentNullException(nameof(list)); |
| 66 | + private bool _loaded; |
67 | 67 |
|
68 | | - /// <inheritdoc /> |
69 | | - public void Adapt(IChangeSet<TObject, TKey> changes) |
70 | | - { |
71 | | - changes.ThrowArgumentNullExceptionIfNull(nameof(changes)); |
72 | | - _cache.Clone(changes); |
| 68 | + /// <inheritdoc /> |
| 69 | + public void Adapt(IChangeSet<TObject, TKey> changes) |
| 70 | + { |
| 71 | + changes.ThrowArgumentNullExceptionIfNull(nameof(changes)); |
| 72 | + _cache.Clone(changes); |
73 | 73 |
|
74 | | - if (changes.Count - changes.Refreshes > refreshThreshold || !_loaded) |
75 | | - { |
76 | | - using (new BindingListEventsSuspender<TObject>(_list)) |
77 | | - { |
78 | | - _list.Clear(); |
79 | | - _list.AddRange(_cache.Items); |
80 | | - _loaded = true; |
81 | | - } |
82 | | - } |
83 | | - else |
| 74 | + if (changes.Count - changes.Refreshes > refreshThreshold || !_loaded) |
| 75 | + { |
| 76 | + using (new BindingListEventsSuspender<TObject>(_list)) |
84 | 77 | { |
85 | | - DoUpdate(changes, _list); |
| 78 | + _list.Clear(); |
| 79 | + _list.AddRange(_cache.Items); |
| 80 | + _loaded = true; |
86 | 81 | } |
87 | 82 | } |
| 83 | + else |
| 84 | + { |
| 85 | + DoUpdate(changes, _list); |
| 86 | + } |
| 87 | + } |
88 | 88 |
|
89 | | - private static void DoUpdate(IChangeSet<TObject, TKey> changes, BindingList<TObject> list) |
| 89 | + private static void DoUpdate(IChangeSet<TObject, TKey> changes, BindingList<TObject> list) |
| 90 | + { |
| 91 | + foreach (var update in changes.ToConcreteType()) |
90 | 92 | { |
91 | | - foreach (var update in changes.ToConcreteType()) |
| 93 | + switch (update.Reason) |
92 | 94 | { |
93 | | - switch (update.Reason) |
94 | | - { |
95 | | - case ChangeReason.Add: |
| 95 | + case ChangeReason.Add: |
| 96 | + list.Add(update.Current); |
| 97 | + break; |
| 98 | + |
| 99 | + case ChangeReason.Remove: |
| 100 | + list.Remove(update.Current); |
| 101 | + break; |
| 102 | + |
| 103 | + case ChangeReason.Update: |
| 104 | + var previousIndex = list.IndexOf(update.Previous.Value); |
| 105 | + if (previousIndex >= 0) |
| 106 | + { |
| 107 | + list[previousIndex] = update.Current; |
| 108 | + } |
| 109 | + else |
| 110 | + { |
96 | 111 | list.Add(update.Current); |
97 | | - break; |
98 | | - |
99 | | - case ChangeReason.Remove: |
100 | | - list.Remove(update.Current); |
101 | | - break; |
102 | | - |
103 | | - case ChangeReason.Update: |
104 | | - var previousIndex = list.IndexOf(update.Previous.Value); |
105 | | - if (previousIndex >= 0) |
106 | | - { |
107 | | - list[previousIndex] = update.Current; |
108 | | - } |
109 | | - else |
110 | | - { |
111 | | - list.Add(update.Current); |
112 | | - } |
113 | | - |
114 | | - break; |
115 | | - |
116 | | - case ChangeReason.Refresh: |
117 | | - var index = list.IndexOf(update.Current); |
118 | | - if (index != -1) |
119 | | - { |
120 | | - list.ResetItem(index); |
121 | | - } |
122 | | - |
123 | | - break; |
124 | | - } |
| 112 | + } |
| 113 | + |
| 114 | + break; |
| 115 | + |
| 116 | + case ChangeReason.Refresh: |
| 117 | + var index = list.IndexOf(update.Current); |
| 118 | + if (index != -1) |
| 119 | + { |
| 120 | + list.ResetItem(index); |
| 121 | + } |
| 122 | + |
| 123 | + break; |
125 | 124 | } |
126 | 125 | } |
127 | 126 | } |
|
0 commit comments