Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ItemGroupCollection.cs
1using System.Collections.ObjectModel;
3
5{
9 public interface IUniqueItem
10 {
14 public string UniqueName { get; }
15
19 public string LocalisedName => this.UniqueName;
20 }
21
27 public class ObservableItemGroup<T>(string Name, IEnumerable<T> Items) : ObservableCollection<T>(Items), IUniqueItem
28 where T : IUniqueItem
29 {
31 public string UniqueName { get; } = Name;
32
34 public string LocalisedName => ServiceRef.Localizer[this.UniqueName] ?? this.UniqueName;
35
39 public override string ToString()
40 {
41 return this.UniqueName;
42 }
43
47 public static void UpdateGroupsItems(ObservableItemGroup<IUniqueItem> OldCollection, ObservableItemGroup<IUniqueItem> NewCollection)
48 {
49 // First, remove items which are no longer in the new collection
50
51 Dictionary<string, IUniqueItem> ToRemove = [];
52
53 foreach (IUniqueItem Item in OldCollection)
54 ToRemove[Item.UniqueName] = Item;
55
56 foreach (IUniqueItem Item in NewCollection)
57 ToRemove.Remove(Item.UniqueName);
58
59 foreach (IUniqueItem Item in ToRemove.Values)
60 OldCollection.Remove(Item);
61
62 // Then recursivelly update every item.
63 // An old item might move or a new item might be inserted in the middle or appended to the end.
64 for (int i = 0; i < NewCollection.Count; i++)
65 {
66 IUniqueItem NewItem = NewCollection[i];
67
68 if (i >= OldCollection.Count)
69 {
70 // appended to the end
71 OldCollection.Add(NewItem);
72 }
73 else
74 {
75 IUniqueItem OldItem = OldCollection[i];
76
77 if (OldItem.UniqueName.Equals(NewItem.UniqueName, StringComparison.Ordinal))
78 {
79 // The item is in its right place.
80 // If it's a collection, do the update recursivelly
81
82 if (OldItem is ObservableItemGroup<IUniqueItem> OldItems && NewItem is ObservableItemGroup<IUniqueItem> NewItems)
83 UpdateGroupsItems(OldItems, NewItems);
84 }
85 else
86 {
87 // We removed the missing items, so this item is moved or has to be inserted
88 int OldIndex = -1;
89
90 for (int j = i + 1; j < OldCollection.Count; j++)
91 {
92 if (OldCollection[j].UniqueName.Equals(NewItem.UniqueName, StringComparison.Ordinal))
93 {
94 OldIndex = j;
95 break;
96 }
97 }
98
99 if (OldIndex == -1)
100 {
101 // The item isn't found in the old collection
102 OldCollection.Insert(i, NewItem);
103 }
104 else
105 {
106 // Move the item to it's new position
107 OldCollection.Move(OldIndex, i);
108
109 // If it's a collection, do the update recursivelly
110 if (OldItem is ObservableItemGroup<IUniqueItem> OldItems && NewItem is ObservableItemGroup<IUniqueItem> NewItems)
111 UpdateGroupsItems(OldItems, NewItems);
112 }
113 }
114 }
115 }
116 }
117 }
118}
Base class that references services in the app.
Definition: ServiceRef.cs:31
static IStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:235
string UniqueName
Unique name used to compare items.
string LocalisedName
Get the groups's localised name