Skip to content

Commit

Permalink
fix how we update the properties in environments page after an operat…
Browse files Browse the repository at this point in the history
…ion (#2950)

* update how we update the properties

* fix ordering

* update based on comments
  • Loading branch information
bbonaby authored and EricJohnson327 committed May 17, 2024
1 parent 4e31a23 commit 788c282
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
27 changes: 20 additions & 7 deletions common/Environments/Helpers/ComputeSystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public static async Task<List<CardProperty>> GetComputeSystemCardPropertiesAsync
{
try
{
var curentProperties = await computeSystem.GetComputeSystemPropertiesAsync(string.Empty);
return GetComputeSystemCardProperties(curentProperties, packageFullName);
var currentProperties = await computeSystem.GetComputeSystemPropertiesAsync(string.Empty);
return GetComputeSystemCardProperties(currentProperties, packageFullName);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -131,7 +131,7 @@ public static EnvironmentsCallToActionData UpdateCallToActionText(int providerCo
}

/// <summary>
/// Safely remove all items from an observable collection.
/// Safely removes all items from an observable collection and replaces them with new items.
/// </summary>
/// <remarks>
/// There can be random COM exceptions due to using the "Clear()" method in an observable collection. This method
Expand All @@ -140,19 +140,32 @@ public static EnvironmentsCallToActionData UpdateCallToActionText(int providerCo
/// this method is used to remove all items individually from the end of the collection to the beginning of the collection.
/// </remarks>
/// <typeparam name="T">Type of objects that the collection contains</typeparam>
/// <param name="collection">An observable collection that contains zero to N elements</param>
public static void RemoveAllItems<T>(ObservableCollection<T> collection)
/// <param name="collectionToUpdate">An observable collection that contains zero to N elements that will have its contents replaced</param>
/// <param name="listWithUpdates">A list that contains zero to N elements whose elements will be added to collectionToUpdate</param>
/// <returns>
/// True only if we successfully replaced all items in the collection. False otherwise.
/// </returns>
public static bool RemoveAllItemsAndReplace<T>(ObservableCollection<T> collectionToUpdate, List<T> listWithUpdates)
{
try
{
for (var i = collection.Count - 1; i >= 0; i--)
for (var i = collectionToUpdate.Count - 1; i >= 0; i--)
{
collection.RemoveAt(i);
collectionToUpdate.RemoveAt(i);
}

for (var i = 0; i < listWithUpdates.Count; i++)
{
collectionToUpdate.Add(listWithUpdates[i]);
}

return true;
}
catch (Exception ex)
{
_log.Error(ex, "Unable to remove items from the collection");
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,9 @@ private async void SetPropertiesAsync()
}

var properties = await ComputeSystemHelpers.GetComputeSystemCardPropertiesAsync(ComputeSystem!, PackageFullName);

ComputeSystemHelpers.RemoveAllItems(Properties);
foreach (var property in properties)
if (!ComputeSystemHelpers.RemoveAllItemsAndReplace(Properties, properties))
{
Properties.Add(property);
Properties = new(properties);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ private async Task UpdatePropertiesAsync()
var properties = await ComputeSystemHelpers.GetComputeSystemCardPropertiesAsync(ComputeSystem, _packageFullName);
lock (_lock)
{
ComputeSystemHelpers.RemoveAllItems(ComputeSystemProperties);
foreach (var property in properties)
if (!ComputeSystemHelpers.RemoveAllItemsAndReplace(ComputeSystemProperties, properties))
{
ComputeSystemProperties.Add(property);
ComputeSystemProperties = new(properties);
}
}
}
Expand Down

0 comments on commit 788c282

Please sign in to comment.