Skip to content

Commit

Permalink
Merging input and action data jsons for Adaptive Cards in Widgets (#3759
Browse files Browse the repository at this point in the history
)
  • Loading branch information
guimafelipe committed Sep 12, 2024
1 parent 92c6eba commit 9e0592b
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using AdaptiveCards.ObjectModel.WinUI3;
using AdaptiveCards.Rendering.WinUI3;
Expand All @@ -24,8 +25,8 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.Widgets;
using Microsoft.Windows.Widgets.Hosts;
using Newtonsoft.Json.Linq;
using Serilog;
using Windows.Data.Json;

namespace DevHome.Dashboard.ViewModels;

Expand Down Expand Up @@ -157,7 +158,7 @@ await Task.Run(async () =>
var hostData = new JsonObject
{
// TODO Add support to host theme in hostData
{ "widgetSize", JsonValue.CreateStringValue(WidgetSize.ToString().ToLowerInvariant()) }, // "small", "medium" or "large"
{ "widgetSize", WidgetSize.ToString().ToLowerInvariant() }, // "small", "medium" or "large"
}.ToString();
var context = new EvaluationContext(cardData, hostData);
Expand Down Expand Up @@ -310,6 +311,26 @@ private Grid GetErrorCard(string error, string subError = null)
return grid;
}

private string MergeJsonData(string jsonStringA, string jsonStringB)
{
if (string.IsNullOrEmpty(jsonStringA))
{
return jsonStringB;
}

if (string.IsNullOrEmpty(jsonStringB))
{
return jsonStringA;
}

var objA = JObject.Parse(jsonStringA);
var objB = JObject.Parse(jsonStringB);

objA.Merge(objB);

return objA.ToString();
}

private async void HandleAdaptiveAction(RenderedAdaptiveCard sender, AdaptiveActionEventArgs args)
{
_log.Information($"HandleInvokedAction {args.Action.ActionTypeString} for widget {Widget.Id}");
Expand All @@ -320,21 +341,23 @@ private async void HandleAdaptiveAction(RenderedAdaptiveCard sender, AdaptiveAct
}
else if (args.Action is AdaptiveExecuteAction executeAction)
{
var dataToSend = string.Empty;
var actionData = string.Empty;
var inputsData = string.Empty;

var dataType = executeAction.DataJson.ValueType;
if (dataType != Windows.Data.Json.JsonValueType.Null)
{
dataToSend = executeAction.DataJson.Stringify();
actionData = executeAction.DataJson.Stringify();
}
else

var inputType = args.Inputs.AsJson().ValueType;
if (inputType != Windows.Data.Json.JsonValueType.Null)
{
var inputType = args.Inputs.AsJson().ValueType;
if (inputType != Windows.Data.Json.JsonValueType.Null)
{
dataToSend = args.Inputs.AsJson().Stringify();
}
inputsData = args.Inputs.AsJson().Stringify();
}

var dataToSend = MergeJsonData(actionData, inputsData);

_log.Information($"Verb = {executeAction.Verb}, Data = {dataToSend}");
await Widget.NotifyActionInvokedAsync(executeAction.Verb, dataToSend);
}
Expand Down Expand Up @@ -406,7 +429,7 @@ private void AnnounceWarnings(AdaptiveCard card)
// we may add Caroussels, Tables and Facts to this list.
// We just need to add the other controls in this dictionary
// with the correct function to access its children.
private static readonly Dictionary<Type, string> ContainerTypes = new()
private static readonly Dictionary<Type, string> _containerTypes = new()
{
{ typeof(AdaptiveContainer), "get_Items" },
{ typeof(AdaptiveColumn), "get_Items" },
Expand All @@ -432,7 +455,7 @@ private void SearchForWarning(IAdaptiveCardElement element, bool isInsideWarning

var containerElement = element as IAdaptiveContainerBase;

foreach (var containerType in ContainerTypes.Where(containerType => containerType.Key == containerElement.GetType()))
foreach (var containerType in _containerTypes.Where(containerType => containerType.Key == containerElement.GetType()))
{
var itemsMethod = containerType.Key.GetMethod(containerType.Value, BindingFlags.Public | BindingFlags.Instance);

Expand Down

0 comments on commit 9e0592b

Please sign in to comment.