Skip to content

Commit d829031

Browse files
BornToBeRootclaude
andcommitted
Refactor: Update input histories in place
ListHelper.Modify works on the ObservableCollection itself: an existing entry is moved to the top, a new one inserted, and the oldest ones are removed. Replaces the pattern copy/clear/refill (with a workaround to restore the input text after the clear) and replacing the collection in 34 places in 22 view models. Unit tests for the helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7250925 commit d829031

25 files changed

Lines changed: 165 additions & 251 deletions
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using System.Collections.Specialized;
4+
using NETworkManager.Utilities;
5+
using Xunit;
6+
7+
namespace NETworkManager.Tests.Utilities;
8+
9+
public class ListHelperTests
10+
{
11+
[Fact]
12+
public void AddsNewEntryAtTheTop()
13+
{
14+
ObservableCollection<string> list = ["b", "c"];
15+
16+
ListHelper.Modify(list, "a", 5);
17+
18+
Assert.Equal(["a", "b", "c"], list);
19+
}
20+
21+
[Fact]
22+
public void MovesExistingEntryToTheTop()
23+
{
24+
ObservableCollection<string> list = ["a", "b", "c"];
25+
26+
ListHelper.Modify(list, "c", 5);
27+
28+
Assert.Equal(["c", "a", "b"], list);
29+
}
30+
31+
[Fact]
32+
public void RemovesOldestEntriesBeyondTheLength()
33+
{
34+
ObservableCollection<string> list = ["b", "c", "d"];
35+
36+
ListHelper.Modify(list, "a", 3);
37+
38+
Assert.Equal(["a", "b", "c"], list);
39+
}
40+
41+
[Fact]
42+
public void ShortensAListThatIsTooLong()
43+
{
44+
// E.g. after the history length was reduced in the settings.
45+
ObservableCollection<string> list = ["a", "b", "c", "d"];
46+
47+
ListHelper.Modify(list, "a", 2);
48+
49+
Assert.Equal(["a", "b"], list);
50+
}
51+
52+
[Fact]
53+
public void RemovesDuplicatesOfTheEntry()
54+
{
55+
ObservableCollection<int> list = [1, 2, 3, 2];
56+
57+
ListHelper.Modify(list, 2, 10);
58+
59+
Assert.Equal([2, 1, 3], list);
60+
}
61+
62+
[Fact]
63+
public void DoesNotChangeTheListIfTheEntryIsAlreadyTheNewest()
64+
{
65+
ObservableCollection<string> list = ["a", "b"];
66+
var changes = 0;
67+
list.CollectionChanged += (_, _) => changes++;
68+
69+
ListHelper.Modify(list, "a", 5);
70+
71+
Assert.Equal(0, changes);
72+
}
73+
74+
[Fact]
75+
public void MovesInsteadOfResetting()
76+
{
77+
// A reset (Clear) would remove the entry that a ComboBox shows, which can clear its text.
78+
ObservableCollection<string> list = ["a", "b", "c"];
79+
var actions = new System.Collections.Generic.List<NotifyCollectionChangedAction>();
80+
list.CollectionChanged += (_, e) => actions.Add(e.Action);
81+
82+
ListHelper.Modify(list, "b", 5);
83+
84+
Assert.Equal([NotifyCollectionChangedAction.Move], actions);
85+
}
86+
87+
[Fact]
88+
public void ThrowsIfTheLengthIsNotPositive()
89+
{
90+
Assert.Throws<ArgumentOutOfRangeException>(() => ListHelper.Modify(new ObservableCollection<string>(), "a", 0));
91+
}
92+
}
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34

45
namespace NETworkManager.Utilities;
56
/// <summary>
6-
/// Helper class for modifying lists by adding new entries and removing old ones if the list exceeds a specified length.
7+
/// Helper class for history lists: add an entry as the newest one and remove the oldest ones if the list exceeds a
8+
/// specified length.
79
/// </summary>
810
public static class ListHelper
911
{
1012
/// <summary>
11-
/// Modify a list by adding the <paramref name="entry"/> and removing the oldest entry if the list is full.
12-
/// If an entry or multiple ones already exist in the list, they will be removed before adding the new entry.
13+
/// Adds the <paramref name="entry"/> as the newest (first) entry of the list. An existing entry is moved to the top
14+
/// (duplicates are removed) and the oldest entries are removed if the list exceeds the <paramref name="length"/>.
15+
/// The list is changed in place, so controls bound to it (e.g. the history of a ComboBox) keep their text and are
16+
/// notified only about the actual changes.
1317
/// </summary>
14-
/// <param name="list">List to modify. Used with <paramref name="entry"/> to add and remove entries.</param>
18+
/// <param name="list">List to modify (e.g. a history of the settings).</param>
1519
/// <param name="entry">Entry to add to the list.</param>
1620
/// <param name="length">Maximum length of the list. Oldest entries will be removed if the list exceeds this length.</param>
1721
/// <typeparam name="T">Type of the list entries. Currently <see cref="string"/> or <see cref="int"/>.</typeparam>
18-
/// <returns>Modified list with the new entry added and oldest entries removed if necessary.</returns>
19-
public static List<T> Modify<T>(List<T> list, T entry, int length)
22+
public static void Modify<T>(ObservableCollection<T> list, T entry, int length)
2023
{
21-
int index;
22-
23-
while ((index = list.IndexOf(entry)) != -1)
24-
{
25-
list.RemoveAt(index);
26-
}
27-
2824
if (length <= 0)
2925
throw new ArgumentOutOfRangeException(nameof(length), "Length must be greater than zero.");
3026

31-
while (list.Count >= length)
32-
list.RemoveAt(list.Count - 1);
27+
var index = list.IndexOf(entry);
3328

34-
list.Insert(0, entry);
29+
if (index > 0)
30+
list.Move(index, 0);
31+
else if (index < 0)
32+
list.Insert(0, entry);
3533

36-
return list;
34+
// Remove duplicates (e.g. from older settings files)
35+
for (var i = list.Count - 1; i > 0; i--)
36+
{
37+
if (EqualityComparer<T>.Default.Equals(list[i], entry))
38+
list.RemoveAt(i);
39+
}
40+
41+
while (list.Count > length)
42+
list.RemoveAt(list.Count - 1);
3743
}
3844
}

‎Source/NETworkManager/ViewModels/BitCalculatorViewModel.cs‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,8 @@ private async void Calculate()
275275
/// <param name="input">The input string.</param>
276276
private void AddInputToHistory(string input)
277277
{
278-
// Create the new list
279-
var list = ListHelper.Modify(SettingsManager.Current.BitCalculator_InputHistory.ToList(), input,
278+
ListHelper.Modify(SettingsManager.Current.BitCalculator_InputHistory, input,
280279
SettingsManager.Current.General_HistoryListEntries);
281-
282-
// Clear the old items
283-
SettingsManager.Current.BitCalculator_InputHistory.Clear();
284-
OnPropertyChanged(nameof(Input)); // Raise property changed again, after the collection has been cleared
285-
286-
// Fill with the new items
287-
list.ForEach(x => SettingsManager.Current.BitCalculator_InputHistory.Add(x));
288280
}
289281

290282
/// <summary>

‎Source/NETworkManager/ViewModels/DNSLookupViewModel.cs‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,8 @@ private void AppendStatusMessage(string message)
523523
/// <param name="host">The host to add.</param>
524524
private void AddHostToHistory(string host)
525525
{
526-
// Create the new list
527-
var list = ListHelper.Modify([.. SettingsManager.Current.DNSLookup_HostHistory], host,
526+
ListHelper.Modify(SettingsManager.Current.DNSLookup_HostHistory, host,
528527
SettingsManager.Current.General_HistoryListEntries);
529-
530-
// Clear the old items
531-
SettingsManager.Current.DNSLookup_HostHistory.Clear();
532-
OnPropertyChanged(nameof(Host)); // Raise property changed again, after the collection has been cleared
533-
534-
// Fill with the new items
535-
list.ForEach(x => SettingsManager.Current.DNSLookup_HostHistory.Add(x));
536528
}
537529

538530

‎Source/NETworkManager/ViewModels/IPGeolocationViewModel.cs‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,8 @@ public void OnClose()
341341
/// <param name="host">The host to add.</param>
342342
private void AddHostToHistory(string host)
343343
{
344-
// Create the new list
345-
var list = ListHelper.Modify(SettingsManager.Current.IPGeolocation_HostHistory.ToList(), host,
344+
ListHelper.Modify(SettingsManager.Current.IPGeolocation_HostHistory, host,
346345
SettingsManager.Current.General_HistoryListEntries);
347-
348-
// Clear the old items
349-
SettingsManager.Current.IPGeolocation_HostHistory.Clear();
350-
OnPropertyChanged(nameof(Host)); // Raise property changed again, after the collection has been cleared
351-
352-
// Fill with the new items
353-
list.ForEach(x => SettingsManager.Current.IPGeolocation_HostHistory.Add(x));
354346
}
355347

356348
#endregion

‎Source/NETworkManager/ViewModels/IPScannerViewModel.cs‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -705,16 +705,8 @@ private async Task CustomCommand(object guid)
705705
/// <param name="ipRange">The host or IP range to add.</param>
706706
private void AddHostToHistory(string ipRange)
707707
{
708-
// Create the new list
709-
var list = ListHelper.Modify([.. SettingsManager.Current.IPScanner_HostHistory], ipRange,
708+
ListHelper.Modify(SettingsManager.Current.IPScanner_HostHistory, ipRange,
710709
SettingsManager.Current.General_HistoryListEntries);
711-
712-
// Clear the old items
713-
SettingsManager.Current.IPScanner_HostHistory.Clear();
714-
OnPropertyChanged(nameof(Host)); // Raise property changed again, after the collection has been cleared
715-
716-
// Fill with the new items
717-
list.ForEach(SettingsManager.Current.IPScanner_HostHistory.Add);
718710
}
719711

720712
/// <summary>

‎Source/NETworkManager/ViewModels/LookupOUILookupViewModel.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,8 @@ public LookupOUILookupViewModel()
5151
/// <param name="macAddressOrVendor">The MAC address or vendor.</param>
5252
private void AddSearchToHistory(string macAddressOrVendor)
5353
{
54-
// Create the new list
55-
var list = ListHelper.Modify(SettingsManager.Current.Lookup_OUI_SearchHistory.ToList(),
56-
macAddressOrVendor, SettingsManager.Current.General_HistoryListEntries);
57-
58-
// Clear the old items
59-
SettingsManager.Current.Lookup_OUI_SearchHistory.Clear();
60-
OnPropertyChanged(
61-
nameof(Search)); // Raise property changed again, after the collection has been cleared
62-
63-
// Fill with the new items
64-
list.ForEach(x => SettingsManager.Current.Lookup_OUI_SearchHistory.Add(x));
54+
ListHelper.Modify(SettingsManager.Current.Lookup_OUI_SearchHistory, macAddressOrVendor,
55+
SettingsManager.Current.General_HistoryListEntries);
6556
}
6657

6758
#endregion

‎Source/NETworkManager/ViewModels/LookupPortViewModel.cs‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,8 @@ public LookupPortLookupViewModel()
4848
/// <param name="portOrService">The port or service.</param>
4949
private void AddSearchToHistory(string portOrService)
5050
{
51-
// Create the new list
52-
var list = ListHelper.Modify(SettingsManager.Current.Lookup_Port_SearchHistory.ToList(), portOrService,
51+
ListHelper.Modify(SettingsManager.Current.Lookup_Port_SearchHistory, portOrService,
5352
SettingsManager.Current.General_HistoryListEntries);
54-
55-
// Clear the old items
56-
SettingsManager.Current.Lookup_Port_SearchHistory.Clear();
57-
OnPropertyChanged(nameof(Search)); // Raise property changed again, after the collection has been cleared
58-
59-
// Fill with the new items
60-
list.ForEach(x => SettingsManager.Current.Lookup_Port_SearchHistory.Add(x));
6153
}
6254

6355
#endregion

‎Source/NETworkManager/ViewModels/PingMonitorHostViewModel.cs‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,16 +411,8 @@ private void RemoveHostByGuid(Guid hostId)
411411

412412
private void AddHostToHistory(string host)
413413
{
414-
// Create the new list
415-
var list = ListHelper.Modify([.. SettingsManager.Current.PingMonitor_HostHistory], host,
414+
ListHelper.Modify(SettingsManager.Current.PingMonitor_HostHistory, host,
416415
SettingsManager.Current.General_HistoryListEntries);
417-
418-
// Clear the old items
419-
SettingsManager.Current.PingMonitor_HostHistory.Clear();
420-
OnPropertyChanged(nameof(Host)); // Raise property changed again, after the collection has been cleared
421-
422-
// Fill with the new items
423-
list.ForEach(SettingsManager.Current.PingMonitor_HostHistory.Add);
424416
}
425417

426418
#endregion

‎Source/NETworkManager/ViewModels/PortScannerViewModel.cs‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -573,30 +573,14 @@ await DialogHelper.ShowMessageAsync(window, Strings.Error,
573573

574574
private void AddHostToHistory(string host)
575575
{
576-
// Create the new list
577-
var list = ListHelper.Modify(SettingsManager.Current.PortScanner_HostHistory.ToList(), host,
576+
ListHelper.Modify(SettingsManager.Current.PortScanner_HostHistory, host,
578577
SettingsManager.Current.General_HistoryListEntries);
579-
580-
// Clear the old items
581-
SettingsManager.Current.PortScanner_HostHistory.Clear();
582-
OnPropertyChanged(nameof(Host)); // Raise property changed again, after the collection has been cleared
583-
584-
// Fill with the new items
585-
list.ForEach(x => SettingsManager.Current.PortScanner_HostHistory.Add(x));
586578
}
587579

588580
private void AddPortToHistory(string port)
589581
{
590-
// Create the new list
591-
var list = ListHelper.Modify(SettingsManager.Current.PortScanner_PortHistory.ToList(), port,
582+
ListHelper.Modify(SettingsManager.Current.PortScanner_PortHistory, port,
592583
SettingsManager.Current.General_HistoryListEntries);
593-
594-
// Clear the old items
595-
SettingsManager.Current.PortScanner_PortHistory.Clear();
596-
OnPropertyChanged(nameof(Ports)); // Raise property changed again, after the collection has been cleared
597-
598-
// Fill with the new items
599-
list.ForEach(x => SettingsManager.Current.PortScanner_PortHistory.Add(x));
600584
}
601585

602586
#endregion

0 commit comments

Comments
 (0)