Skip to content

Commit 788c282

Browse files
bbonabyEricJohnson327
authored andcommitted
fix how we update the properties in environments page after an operation (#2950)
* update how we update the properties * fix ordering * update based on comments
1 parent 4e31a23 commit 788c282

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

common/Environments/Helpers/ComputeSystemHelpers.cs

+20-7
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public static async Task<List<CardProperty>> GetComputeSystemCardPropertiesAsync
8686
{
8787
try
8888
{
89-
var curentProperties = await computeSystem.GetComputeSystemPropertiesAsync(string.Empty);
90-
return GetComputeSystemCardProperties(curentProperties, packageFullName);
89+
var currentProperties = await computeSystem.GetComputeSystemPropertiesAsync(string.Empty);
90+
return GetComputeSystemCardProperties(currentProperties, packageFullName);
9191
}
9292
catch (Exception ex)
9393
{
@@ -131,7 +131,7 @@ public static EnvironmentsCallToActionData UpdateCallToActionText(int providerCo
131131
}
132132

133133
/// <summary>
134-
/// Safely remove all items from an observable collection.
134+
/// Safely removes all items from an observable collection and replaces them with new items.
135135
/// </summary>
136136
/// <remarks>
137137
/// There can be random COM exceptions due to using the "Clear()" method in an observable collection. This method
@@ -140,19 +140,32 @@ public static EnvironmentsCallToActionData UpdateCallToActionText(int providerCo
140140
/// this method is used to remove all items individually from the end of the collection to the beginning of the collection.
141141
/// </remarks>
142142
/// <typeparam name="T">Type of objects that the collection contains</typeparam>
143-
/// <param name="collection">An observable collection that contains zero to N elements</param>
144-
public static void RemoveAllItems<T>(ObservableCollection<T> collection)
143+
/// <param name="collectionToUpdate">An observable collection that contains zero to N elements that will have its contents replaced</param>
144+
/// <param name="listWithUpdates">A list that contains zero to N elements whose elements will be added to collectionToUpdate</param>
145+
/// <returns>
146+
/// True only if we successfully replaced all items in the collection. False otherwise.
147+
/// </returns>
148+
public static bool RemoveAllItemsAndReplace<T>(ObservableCollection<T> collectionToUpdate, List<T> listWithUpdates)
145149
{
146150
try
147151
{
148-
for (var i = collection.Count - 1; i >= 0; i--)
152+
for (var i = collectionToUpdate.Count - 1; i >= 0; i--)
149153
{
150-
collection.RemoveAt(i);
154+
collectionToUpdate.RemoveAt(i);
151155
}
156+
157+
for (var i = 0; i < listWithUpdates.Count; i++)
158+
{
159+
collectionToUpdate.Add(listWithUpdates[i]);
160+
}
161+
162+
return true;
152163
}
153164
catch (Exception ex)
154165
{
155166
_log.Error(ex, "Unable to remove items from the collection");
156167
}
168+
169+
return false;
157170
}
158171
}

tools/Environments/DevHome.Environments/ViewModels/ComputeSystemViewModel.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,9 @@ private async void SetPropertiesAsync()
193193
}
194194

195195
var properties = await ComputeSystemHelpers.GetComputeSystemCardPropertiesAsync(ComputeSystem!, PackageFullName);
196-
197-
ComputeSystemHelpers.RemoveAllItems(Properties);
198-
foreach (var property in properties)
196+
if (!ComputeSystemHelpers.RemoveAllItemsAndReplace(Properties, properties))
199197
{
200-
Properties.Add(property);
198+
Properties = new(properties);
201199
}
202200
}
203201

tools/SetupFlow/DevHome.SetupFlow/ViewModels/Environments/ComputeSystemCardViewModel.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ private async Task UpdatePropertiesAsync()
102102
var properties = await ComputeSystemHelpers.GetComputeSystemCardPropertiesAsync(ComputeSystem, _packageFullName);
103103
lock (_lock)
104104
{
105-
ComputeSystemHelpers.RemoveAllItems(ComputeSystemProperties);
106-
foreach (var property in properties)
105+
if (!ComputeSystemHelpers.RemoveAllItemsAndReplace(ComputeSystemProperties, properties))
107106
{
108-
ComputeSystemProperties.Add(property);
107+
ComputeSystemProperties = new(properties);
109108
}
110109
}
111110
}

0 commit comments

Comments
 (0)