Repository navigation
Expand file tree
/
Copy pathListHelper.cs
More file actions
83 lines (71 loc) · 3.12 KB
/
Copy pathListHelper.cs
File metadata and controls
83 lines (71 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace NETworkManager.Utilities;
/// <summary>
/// Helper class for history lists: add an entry as the newest one and remove the oldest ones if the list exceeds a
/// specified length.
/// </summary>
public static class ListHelper
{
/// <summary>
/// Adds the <paramref name="entry"/> as the newest (first) entry of the list. An existing entry is moved to the top
/// (duplicates are removed) and the oldest entries are removed if the list exceeds the <paramref name="length"/>.
/// The list is changed in place, so controls bound to it (e.g. the history of a ComboBox) keep their text and are
/// notified only about the actual changes. Default values (null, 0) and empty strings are not added (e.g. optional
/// inputs).
/// </summary>
/// <param name="list">List to modify (e.g. a history of the settings).</param>
/// <param name="entry">Entry to add to the list.</param>
/// <param name="length">
/// Maximum length of the list. Oldest entries will be removed if the list exceeds this length. 0 stores no
/// entries (e.g. "Number of stored entries" is set to 0), the list is emptied.
/// </param>
/// <param name="comparer">Comparer for existing entries (e.g. case-insensitive). If null, the default comparer is used.</param>
/// <typeparam name="T">Type of the list entries. Currently <see cref="string"/> or <see cref="int"/>.</typeparam>
public static void Modify<T>(ObservableCollection<T> list, T entry, int length,
IEqualityComparer<T> comparer = null)
{
comparer ??= EqualityComparer<T>.Default;
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), "Length must not be negative.");
// No history: all entries are removed, one by one like the oldest entries below. An editable ComboBox that shows
// the list keeps its text (ComboBoxHistoryBehavior in the HistoryComboBox style).
if (length == 0)
{
while (list.Count > 0)
list.RemoveAt(list.Count - 1);
return;
}
if (EqualityComparer<T>.Default.Equals(entry, default) || entry is string text && string.IsNullOrEmpty(text))
return;
var index = -1;
for (var i = 0; i < list.Count; i++)
{
if (!comparer.Equals(list[i], entry))
continue;
index = i;
break;
}
if (index < 0)
{
list.Insert(0, entry);
}
else
{
if (index > 0)
list.Move(index, 0);
// Keep the latest spelling (e.g. with a case-insensitive comparer)
if (!EqualityComparer<T>.Default.Equals(list[0], entry))
list[0] = entry;
}
// Remove duplicates (e.g. from older settings files)
for (var i = list.Count - 1; i > 0; i--)
{
if (comparer.Equals(list[i], entry))
list.RemoveAt(i);
}
while (list.Count > length)
list.RemoveAt(list.Count - 1);
}
}