Skip to content

Commit

Permalink
Change GetWidgetsAsync to always return a valid list (#2687)
Browse files Browse the repository at this point in the history
---------
Co-authored-by: Kristen Schau <47155823+krschau@users.noreply.github.com>
  • Loading branch information
dkbennett authored Apr 18, 2024
1 parent 867a83a commit c95bba2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,34 @@ namespace DevHome.Dashboard.Services;

public interface IWidgetHostingService
{
/// <summary>Get the list of current widgets from the WidgetService.</summary>
/// <returns>A list of widgets, or empty list if there were no widgets or the list could not be retrieved.</returns>
public Task<Widget[]> GetWidgetsAsync();

/// <summary>Gets the widget with the given ID.</summary>
/// <returns>The widget, or null if one could not be retrieved.</returns>
public Task<Widget> GetWidgetAsync(string widgetId);

/// <summary>Create and return a new widget.</summary>
/// <returns>The new widget, or null if one could not be created.</returns>
public Task<Widget> CreateWidgetAsync(string widgetDefinitionId, WidgetSize widgetSize);

/// <summary>Get the catalog of widgets from the WidgetService.</summary>
/// <returns>The catalog of widgets, or null if one could not be created.</returns>
public Task<WidgetCatalog> GetWidgetCatalogAsync();

/// <summary>Get the list of WidgetProviderDefinitions from the WidgetService.</summary>
/// <returns>A list of WidgetProviderDefinitions, or an empty list if there were no widgets
/// or the list could not be retrieved.</returns>
public Task<WidgetProviderDefinition[]> GetProviderDefinitionsAsync();

/// <summary>Get the list of WidgetDefinitions from the WidgetService.</summary>
/// <returns>A list of WidgetDefinitions, or an empty list if there were no widgets
/// or the list could not be retrieved.</returns>
public Task<WidgetDefinition[]> GetWidgetDefinitionsAsync();

/// <summary>Get the WidgetDefinition for the given WidgetDefinitionId from the WidgetService.</summary>
/// <returns>The WidgetDefinition, or null if the widget definition could not be found
/// or there was an error retrieving it.</returns>
public Task<WidgetDefinition> GetWidgetDefinitionAsync(string widgetDefinitionId);
}
40 changes: 9 additions & 31 deletions tools/Dashboard/DevHome.Dashboard/Services/WidgetHostingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public class WidgetHostingService : IWidgetHostingService

private const int MaxAttempts = 3;

/// <summary>
/// Get the list of current widgets from the WidgetService.
/// </summary>
/// <returns>A list of widgets, or null if there were no widgets or the list could not be retrieved.</returns>
/// <inheritdoc />
public async Task<Widget[]> GetWidgetsAsync()
{
var attempt = 0;
Expand All @@ -35,7 +32,7 @@ public async Task<Widget[]> GetWidgetsAsync()
try
{
_widgetHost ??= await Task.Run(() => WidgetHost.Register(new WidgetHostContext("BAA93438-9B07-4554-AD09-7ACCD7D4F031")));
return await Task.Run(() => _widgetHost.GetWidgets());
return await Task.Run(() => _widgetHost.GetWidgets()) ?? [];
}
catch (COMException ex) when (ex.HResult == RpcServerUnavailable || ex.HResult == RpcCallFailed)
{
Expand All @@ -52,11 +49,10 @@ public async Task<Widget[]> GetWidgetsAsync()
}
}

return null;
return [];
}

/// <summary>Gets the widget with the given ID.</summary>
/// <returns>The widget, or null if one could not be retrieved.</returns>
/// <inheritdoc />
public async Task<Widget> GetWidgetAsync(string widgetId)
{
var attempt = 0;
Expand All @@ -83,10 +79,7 @@ public async Task<Widget> GetWidgetAsync(string widgetId)
return null;
}

/// <summary>
/// Create and return a new widget.
/// </summary>
/// <returns>The new widget, or null if one could not be created.</returns>
/// <inheritdoc />
public async Task<Widget> CreateWidgetAsync(string widgetDefinitionId, WidgetSize widgetSize)
{
var attempt = 0;
Expand All @@ -113,10 +106,7 @@ public async Task<Widget> CreateWidgetAsync(string widgetDefinitionId, WidgetSiz
return null;
}

/// <summary>
/// Get the catalog of widgets from the WidgetService.
/// </summary>
/// <returns>The catalog of widgets, or null if one could not be created.</returns>
/// <inheritdoc />
public async Task<WidgetCatalog> GetWidgetCatalogAsync()
{
var attempt = 0;
Expand Down Expand Up @@ -149,11 +139,7 @@ public async Task<WidgetCatalog> GetWidgetCatalogAsync()
return _widgetCatalog;
}

/// <summary>
/// Get the list of WidgetProviderDefinitions from the WidgetService.
/// </summary>
/// <returns>A list of WidgetProviderDefinitions, or an empty list if there were no widgets
/// or the list could not be retrieved.</returns>
/// <inheritdoc />
public async Task<WidgetProviderDefinition[]> GetProviderDefinitionsAsync()
{
var attempt = 0;
Expand Down Expand Up @@ -182,11 +168,7 @@ public async Task<WidgetProviderDefinition[]> GetProviderDefinitionsAsync()
return [];
}

/// <summary>
/// Get the list of WidgetDefinitions from the WidgetService.
/// </summary>
/// <returns>A list of WidgetDefinitions, or an empty list if there were no widgets
/// or the list could not be retrieved.</returns>
/// <inheritdoc />
public async Task<WidgetDefinition[]> GetWidgetDefinitionsAsync()
{
var attempt = 0;
Expand Down Expand Up @@ -215,11 +197,7 @@ public async Task<WidgetDefinition[]> GetWidgetDefinitionsAsync()
return [];
}

/// <summary>
/// Get the WidgetDefinition for the given WidgetDefinitionId from the WidgetService.
/// </summary>
/// <returns>The WidgetDefinition, or null if the widget definition could not be found
/// or there was an error retrieving it.</returns>
/// <inheritdoc />
public async Task<WidgetDefinition> GetWidgetDefinitionAsync(string widgetDefinitionId)
{
var attempt = 0;
Expand Down
12 changes: 5 additions & 7 deletions tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,14 @@ private async Task InitializeDashboard()
private async Task InitializePinnedWidgetListAsync(bool isFirstDashboardRun)
{
var hostWidgets = await GetPreviouslyPinnedWidgets();

if ((hostWidgets == null || hostWidgets.Length == 0) && isFirstDashboardRun)
if ((hostWidgets.Length == 0) && isFirstDashboardRun)
{
// If it's the first time the Dashboard has been displayed and we have no other widgets pinned to a
// different version of Dev Home, pin some default widgets.
_log.Information($"Pin default widgets");
await PinDefaultWidgetsAsync();
}
else if (hostWidgets != null)
else
{
await RestorePinnedWidgetsAsync(hostWidgets);
}
Expand All @@ -235,11 +234,10 @@ private async Task<ComSafeWidget[]> GetPreviouslyPinnedWidgets()
{
_log.Information("Get widgets for current host");
var unsafeHostWidgets = await ViewModel.WidgetHostingService.GetWidgetsAsync();

if (unsafeHostWidgets == null)
if (unsafeHostWidgets.Length == 0)
{
_log.Information($"Found 0 widgets for this host");
return null;
return [];
}

var comSafeHostWidgets = new List<ComSafeWidget>();
Expand Down Expand Up @@ -380,7 +378,7 @@ private async Task DeleteAbandonedWidgetAsync(ComSafeWidget widget)
await widget.DeleteAsync();

var newWidgetList = await ViewModel.WidgetHostingService.GetWidgetsAsync();
length = (newWidgetList == null) ? 0 : newWidgetList.Length;
length = newWidgetList.Length;
_log.Information($"After delete, {length} widgets for this host");
}

Expand Down

0 comments on commit c95bba2

Please sign in to comment.