Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete abandoned widgets #1810

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ private async Task<bool> InitializeDashboard()
private async Task RestorePinnedWidgetsAsync()
{
Log.Logger()?.ReportInfo("DashboardView", "Get widgets for current host");
var pinnedWidgets = ViewModel.WidgetHostingService.GetWidgetHost()?.GetWidgets();
if (pinnedWidgets != null)
var hostWidgets = ViewModel.WidgetHostingService.GetWidgetHost()?.GetWidgets();
if (hostWidgets != null)
{
Log.Logger()?.ReportInfo("DashboardView", $"Found {pinnedWidgets.Length} widgets for this host");
Log.Logger()?.ReportInfo("DashboardView", $"Found {hostWidgets.Length} widgets for this host");
var restoredWidgetsWithPosition = new SortedDictionary<int, Widget>();
var restoredWidgetsWithoutPosition = new SortedDictionary<int, Widget>();
var numUnorderedWidgets = 0;

// Widgets do not come from the host in a deterministic order, so save their order in each widget's CustomState.
// Iterate through all the widgets and put them in order. If a widget does not have a position assigned to it,
// append it at the end. If a position is missing, just show the next widget in order.
foreach (var widget in pinnedWidgets)
foreach (var widget in hostWidgets)
{
try
{
Expand Down Expand Up @@ -179,6 +179,17 @@ private async Task RestorePinnedWidgetsAsync()
restoredWidgetsWithoutPosition.Add(numUnorderedWidgets++, widget);
}
}
else
{
// This shouldn't be able to be reached
Log.Logger()?.ReportError("DashboardView", $"Widget has custom state but no HostName.");
}
}
else
{
// If we have a widget with no state, Dev Home does not consider it a valid widget
// and should delete it, rather than letting it run invisibly in the background.
await DeleteAbandonedWidgetAsync(widget);
}
}
catch (Exception ex)
Expand All @@ -205,6 +216,18 @@ private async Task RestorePinnedWidgetsAsync()
}
}

private async Task DeleteAbandonedWidgetAsync(Widget widget)
{
var length = ViewModel.WidgetHostingService.GetWidgetHost()!.GetWidgets().Length;
Log.Logger()?.ReportInfo("DashboardView", $"Found abandoned widget, try to delete it...");
Log.Logger()?.ReportInfo("DashboardView", $"Before delete, {length} widgets for this host");

await widget.DeleteAsync();

length = ViewModel.WidgetHostingService.GetWidgetHost()!.GetWidgets().Length;
Log.Logger()?.ReportInfo("DashboardView", $"After delete, {length} widgets for this host");
}

private async Task PlaceWidget(KeyValuePair<int, Widget> orderedWidget, int finalPlace)
{
var widget = orderedWidget.Value;
Expand Down