Skip to content

Commit f5a0b1e

Browse files
BornToBeRootclaude
andcommitted
Refactor: Inline the AddXToHistory methods
The 34 AddXToHistory methods only called ListHelper.Modify and were each called once. ListHelper.Modify now ignores default values (null, 0) and empty strings, which the remote tools checked in their methods, so the call sites call it directly. PuTTY keeps its check for port and baud, which are converted to strings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d829031 commit f5a0b1e

24 files changed

Lines changed: 100 additions & 303 deletions

‎Source/NETworkManager.Tests/Utilities/ListHelperTests.cs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ public void MovesInsteadOfResetting()
8484
Assert.Equal([NotifyCollectionChangedAction.Move], actions);
8585
}
8686

87+
[Theory]
88+
[InlineData(null)]
89+
[InlineData("")]
90+
public void IgnoresEmptyEntries(string entry)
91+
{
92+
ObservableCollection<string> list = ["a"];
93+
94+
ListHelper.Modify(list, entry, 5);
95+
96+
Assert.Equal(["a"], list);
97+
}
98+
99+
[Fact]
100+
public void IgnoresDefaultValue()
101+
{
102+
// E.g. a port of 0 if the port is not used.
103+
ObservableCollection<int> list = [22];
104+
105+
ListHelper.Modify(list, 0, 5);
106+
107+
Assert.Equal([22], list);
108+
}
109+
87110
[Fact]
88111
public void ThrowsIfTheLengthIsNotPositive()
89112
{

‎Source/NETworkManager.Utilities/ListHelper.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public static class ListHelper
1313
/// Adds the <paramref name="entry"/> as the newest (first) entry of the list. An existing entry is moved to the top
1414
/// (duplicates are removed) and the oldest entries are removed if the list exceeds the <paramref name="length"/>.
1515
/// 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.
16+
/// notified only about the actual changes. Default values (null, 0) and empty strings are not added (e.g. optional
17+
/// inputs).
1718
/// </summary>
1819
/// <param name="list">List to modify (e.g. a history of the settings).</param>
1920
/// <param name="entry">Entry to add to the list.</param>
@@ -24,6 +25,9 @@ public static void Modify<T>(ObservableCollection<T> list, T entry, int length)
2425
if (length <= 0)
2526
throw new ArgumentOutOfRangeException(nameof(length), "Length must be greater than zero.");
2627

28+
if (EqualityComparer<T>.Default.Equals(entry, default) || entry is string text && string.IsNullOrEmpty(text))
29+
return;
30+
2731
var index = list.IndexOf(entry);
2832

2933
if (index > 0)

‎Source/NETworkManager/ViewModels/BitCalculatorViewModel.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,21 +264,12 @@ private async void Calculate()
264264

265265
IsResultVisible = true;
266266

267-
AddInputToHistory(Input);
267+
ListHelper.Modify(SettingsManager.Current.BitCalculator_InputHistory, Input,
268+
SettingsManager.Current.General_HistoryListEntries);
268269

269270
IsRunning = false;
270271
}
271272

272-
/// <summary>
273-
/// Adds the input to the history.
274-
/// </summary>
275-
/// <param name="input">The input string.</param>
276-
private void AddInputToHistory(string input)
277-
{
278-
ListHelper.Modify(SettingsManager.Current.BitCalculator_InputHistory, input,
279-
SettingsManager.Current.General_HistoryListEntries);
280-
}
281-
282273
/// <summary>
283274
/// Called when the view becomes visible.
284275
/// </summary>

‎Source/NETworkManager/ViewModels/DNSLookupViewModel.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ private async Task QueryAsync()
385385

386386
DragablzTabItem.SetTabHeader(_tabId, Host);
387387

388-
AddHostToHistory(Host);
388+
ListHelper.Modify(SettingsManager.Current.DNSLookup_HostHistory, Host,
389+
SettingsManager.Current.General_HistoryListEntries);
389390

390391
DNSLookupSettings dnsSettings = new()
391392
{
@@ -517,16 +518,6 @@ private void AppendStatusMessage(string message)
517518
}
518519

519520
// Modify history list
520-
/// <summary>
521-
/// Adds the host to the history.
522-
/// </summary>
523-
/// <param name="host">The host to add.</param>
524-
private void AddHostToHistory(string host)
525-
{
526-
ListHelper.Modify(SettingsManager.Current.DNSLookup_HostHistory, host,
527-
SettingsManager.Current.General_HistoryListEntries);
528-
}
529-
530521

531522
/// <summary>
532523
/// Exports the results.

‎Source/NETworkManager/ViewModels/IPGeolocationViewModel.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ private async Task Query()
262262
Result = result.Info;
263263
IsResultVisible = true;
264264

265-
AddHostToHistory(Host);
265+
ListHelper.Modify(SettingsManager.Current.IPGeolocation_HostHistory, Host,
266+
SettingsManager.Current.General_HistoryListEntries);
266267
}
267268
catch (Exception ex)
268269
{
@@ -335,15 +336,5 @@ public void OnClose()
335336
ConfigurationManager.Current.IPGeolocationTabCount--;
336337
}
337338

338-
/// <summary>
339-
/// Adds the host to the history.
340-
/// </summary>
341-
/// <param name="host">The host to add.</param>
342-
private void AddHostToHistory(string host)
343-
{
344-
ListHelper.Modify(SettingsManager.Current.IPGeolocation_HostHistory, host,
345-
SettingsManager.Current.General_HistoryListEntries);
346-
}
347-
348339
#endregion
349340
}

‎Source/NETworkManager/ViewModels/IPScannerViewModel.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ private async Task Start()
552552
PreparingScan = false;
553553

554554
// Add host(s) to the history
555-
AddHostToHistory(Host);
555+
ListHelper.Modify(SettingsManager.Current.IPScanner_HostHistory, Host,
556+
SettingsManager.Current.General_HistoryListEntries);
556557

557558
var ipScanner = new IPScanner(new IPScannerOptions(
558559
SettingsManager.Current.IPScanner_MaxHostThreads,
@@ -699,16 +700,6 @@ private async Task CustomCommand(object guid)
699700
}
700701
}
701702

702-
/// <summary>
703-
/// Adds the scanned host/range to the history.
704-
/// </summary>
705-
/// <param name="ipRange">The host or IP range to add.</param>
706-
private void AddHostToHistory(string ipRange)
707-
{
708-
ListHelper.Modify(SettingsManager.Current.IPScanner_HostHistory, ipRange,
709-
SettingsManager.Current.General_HistoryListEntries);
710-
}
711-
712703
/// <summary>
713704
/// Exports the scan results.
714705
/// </summary>

‎Source/NETworkManager/ViewModels/LookupOUILookupViewModel.cs‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,6 @@ public LookupOUILookupViewModel()
4343

4444
#endregion
4545

46-
#region Methods
47-
48-
/// <summary>
49-
/// Adds the search term to the history.
50-
/// </summary>
51-
/// <param name="macAddressOrVendor">The MAC address or vendor.</param>
52-
private void AddSearchToHistory(string macAddressOrVendor)
53-
{
54-
ListHelper.Modify(SettingsManager.Current.Lookup_OUI_SearchHistory, macAddressOrVendor,
55-
SettingsManager.Current.General_HistoryListEntries);
56-
}
57-
58-
#endregion
5946

6047
#region Variables
6148

@@ -251,7 +238,8 @@ private async void OUILookupAction()
251238
NothingFound = Results.Count == 0;
252239

253240
// Add the MAC-Address or Vendor to the history
254-
AddSearchToHistory(Search);
241+
ListHelper.Modify(SettingsManager.Current.Lookup_OUI_SearchHistory, Search,
242+
SettingsManager.Current.General_HistoryListEntries);
255243

256244
IsRunning = false;
257245
}

‎Source/NETworkManager/ViewModels/LookupPortViewModel.cs‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,6 @@ public LookupPortLookupViewModel()
4040

4141
#endregion
4242

43-
#region Methods
44-
45-
/// <summary>
46-
/// Adds the search term to the history.
47-
/// </summary>
48-
/// <param name="portOrService">The port or service.</param>
49-
private void AddSearchToHistory(string portOrService)
50-
{
51-
ListHelper.Modify(SettingsManager.Current.Lookup_Port_SearchHistory, portOrService,
52-
SettingsManager.Current.General_HistoryListEntries);
53-
}
54-
55-
#endregion
5643

5744
#region Variables
5845

@@ -312,7 +299,8 @@ await PortLookup.LookupByPortAndProtocolAsync(
312299
NothingFound = Results.Count == 0;
313300

314301
// Add the MAC-Address or Vendor to the history
315-
AddSearchToHistory(Search);
302+
ListHelper.Modify(SettingsManager.Current.Lookup_Port_SearchHistory, Search,
303+
SettingsManager.Current.General_HistoryListEntries);
316304

317305
IsRunning = false;
318306
}

‎Source/NETworkManager/ViewModels/PingMonitorHostViewModel.cs‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ public async Task Start()
344344
}
345345

346346
// Add host(s) to history
347-
AddHostToHistory(Host);
347+
ListHelper.Modify(SettingsManager.Current.PingMonitor_HostHistory, Host,
348+
SettingsManager.Current.General_HistoryListEntries);
348349

349350
// Add host(s) to list and start the ping
350351
foreach (var hostView in hosts.hosts.Select(currentHost =>
@@ -409,12 +410,6 @@ private void RemoveHostByGuid(Guid hostId)
409410
Hosts.RemoveAt(i);
410411
}
411412

412-
private void AddHostToHistory(string host)
413-
{
414-
ListHelper.Modify(SettingsManager.Current.PingMonitor_HostHistory, host,
415-
SettingsManager.Current.General_HistoryListEntries);
416-
}
417-
418413
#endregion
419414

420415
#region Event

‎Source/NETworkManager/ViewModels/PortScannerViewModel.cs‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,10 @@ private async Task Start()
487487
PreparingScan = false;
488488

489489
// Add host(s) to the history
490-
AddHostToHistory(Host);
491-
AddPortToHistory(Ports);
490+
ListHelper.Modify(SettingsManager.Current.PortScanner_HostHistory, Host,
491+
SettingsManager.Current.General_HistoryListEntries);
492+
ListHelper.Modify(SettingsManager.Current.PortScanner_PortHistory, Ports,
493+
SettingsManager.Current.General_HistoryListEntries);
492494

493495
var portScanner = new PortScanner(new PortScannerOptions(
494496
SettingsManager.Current.PortScanner_MaxHostThreads,
@@ -571,18 +573,6 @@ await DialogHelper.ShowMessageAsync(window, Strings.Error,
571573
return window.ShowChildWindowAsync(childWindow);
572574
}
573575

574-
private void AddHostToHistory(string host)
575-
{
576-
ListHelper.Modify(SettingsManager.Current.PortScanner_HostHistory, host,
577-
SettingsManager.Current.General_HistoryListEntries);
578-
}
579-
580-
private void AddPortToHistory(string port)
581-
{
582-
ListHelper.Modify(SettingsManager.Current.PortScanner_PortHistory, port,
583-
SettingsManager.Current.General_HistoryListEntries);
584-
}
585-
586576
#endregion
587577

588578
#region Events

0 commit comments

Comments
 (0)