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

Change GetWidgetsAsync to always return a valid list #2687

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
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
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
Loading