-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathExtensionAdaptiveCard.cs
68 lines (52 loc) · 2.55 KB
/
ExtensionAdaptiveCard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Text.Json.Nodes;
using AdaptiveCards.ObjectModel.WinUI3;
using AdaptiveCards.Templating;
using Microsoft.Windows.DevHome.SDK;
using Serilog;
namespace DevHome.Common.Models;
public class ExtensionAdaptiveCard : IExtensionAdaptiveCard
{
private readonly AdaptiveElementParserRegistration? _elementParserRegistration;
private readonly AdaptiveActionParserRegistration? _actionParserRegistration;
public event EventHandler<AdaptiveCard>? UiUpdate;
public string DataJson { get; private set; }
public string State { get; private set; }
public string TemplateJson { get; private set; }
public ExtensionAdaptiveCard(
AdaptiveElementParserRegistration? elementParserRegistration = null,
AdaptiveActionParserRegistration? actionParserRegistration = null)
{
TemplateJson = new JsonObject().ToJsonString();
DataJson = new JsonObject().ToJsonString();
State = string.Empty;
_elementParserRegistration = elementParserRegistration ?? new AdaptiveElementParserRegistration();
_actionParserRegistration = actionParserRegistration ?? new AdaptiveActionParserRegistration();
}
public ProviderOperationResult Update(string templateJson, string dataJson, string state)
{
var template = new AdaptiveCardTemplate(templateJson ?? TemplateJson);
var adaptiveCardString = template.Expand(dataJson ?? DataJson);
var parseResult = AdaptiveCard.FromJsonString(adaptiveCardString, _elementParserRegistration, _actionParserRegistration);
if (parseResult.AdaptiveCard is null)
{
Log.Error($"ExtensionAdaptiveCard.Update(): AdaptiveCard is null - templateJson: {templateJson} dataJson: {dataJson} state: {state}");
return new ProviderOperationResult(
ProviderOperationStatus.Failure,
new ArgumentNullException(null),
"AdaptiveCard is null",
$"templateJson: {templateJson} dataJson: {dataJson} state: {state}");
}
TemplateJson = templateJson ?? TemplateJson;
DataJson = dataJson ?? DataJson;
State = state ?? State;
UiUpdate?.Invoke(this, parseResult.AdaptiveCard);
return new ProviderOperationResult(
ProviderOperationStatus.Success,
null,
"IExtensionAdaptiveCard.Update succeeds",
"IExtensionAdaptiveCard.Update succeeds");
}
}