Form pages are all wired up

This commit is contained in:
Mike Griese
2024-12-12 12:29:51 -06:00
parent 6578549f98
commit 0884c8c247
9 changed files with 77 additions and 14 deletions

View File

@@ -201,7 +201,7 @@ public partial class MastodonPostForm : Form
{
"type": "Image",
"url": "${author_avatar_url}",
"size": "Small",
"size": "Medium",
"style": "Person"
}
]

View File

@@ -56,7 +56,7 @@ internal sealed partial class AddBookmarkForm : Form
public override string DataJson() => throw new NotImplementedException();
public override string StateJson() => throw new NotImplementedException();
public override string StateJson() => "{}";
public override CommandResult SubmitForm(string payload)
{

View File

@@ -69,7 +69,7 @@ internal sealed partial class BookmarkPlaceholderForm : Form
public override string DataJson() => throw new NotImplementedException();
public override string StateJson() => throw new NotImplementedException();
public override string StateJson() => "{}";
public override CommandResult SubmitForm(string payload)
{

View File

@@ -75,7 +75,7 @@ internal sealed partial class SettingsForm : Form
""";
}
public override string StateJson() => throw new NotImplementedException();
public override string StateJson() => "{}";
public override CommandResult SubmitForm(string payload)
{

View File

@@ -55,7 +55,7 @@ internal sealed partial class SampleForm : Form
return json;
}
public override string StateJson() => throw new NotImplementedException();
public override string StateJson() => "{}";
public override CommandResult SubmitForm(string payload)
{

View File

@@ -54,7 +54,7 @@ internal sealed partial class SpongeSettingsForm : Form
public override string DataJson() => throw new NotImplementedException();
public override string StateJson() => throw new NotImplementedException();
public override string StateJson() => "{}";
public override CommandResult SubmitForm(string payload)
{

View File

@@ -50,7 +50,7 @@ internal sealed partial class YouTubeAPIForm : Form
public override string DataJson() => throw new NotImplementedException();
public override string StateJson() => throw new NotImplementedException();
public override string StateJson() => "{}";
public override CommandResult SubmitForm(string payload)
{

View File

@@ -2,6 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text.Json;
using AdaptiveCards.ObjectModel.WinUI3;
using AdaptiveCards.Templating;
using Microsoft.CmdPal.Extensions;
@@ -33,15 +34,36 @@ public partial class FormViewModel(IForm _form, IPageContext context) : Extensio
return;
}
TemplateJson = model.TemplateJson();
StateJson = model.StateJson();
DataJson = model.DataJson();
try
{
TemplateJson = model.TemplateJson();
StateJson = model.StateJson();
DataJson = model.DataJson();
AdaptiveCardTemplate template = new(TemplateJson);
var cardJson = template.Expand(DataJson);
Card = AdaptiveCard.FromJsonString(cardJson);
AdaptiveCardTemplate template = new(TemplateJson);
var cardJson = template.Expand(DataJson);
Card = AdaptiveCard.FromJsonString(cardJson);
}
catch (Exception e)
{
// If we fail to parse the card JSON, then display _our own card_
// with the exception
AdaptiveCardTemplate template = new(ErrorCardJson);
// todo: we could probably stick Card.Errrors in there too
var dataJson = $$"""
{
"error_message": {{JsonSerializer.Serialize(e.Message)}},
"error_stack": {{JsonSerializer.Serialize(e.StackTrace)}},
"inner_exception": {{JsonSerializer.Serialize(e.InnerException?.Message)}},
"template_json": {{JsonSerializer.Serialize(TemplateJson)}},
"data_json": {{JsonSerializer.Serialize(DataJson)}}
}
""";
var cardJson = template.Expand(dataJson);
Card = AdaptiveCard.FromJsonString(cardJson);
}
// TODO catch and replace with our error card
UpdateProperty(nameof(Card));
}
@@ -78,4 +100,45 @@ public partial class FormViewModel(IForm _form, IPageContext context) : Extensio
}
}
}
private static readonly string ErrorCardJson = """
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.5",
"body": [
{
"type": "TextBlock",
"text": "Error parsing form from extension",
"wrap": true,
"style": "heading",
"size": "ExtraLarge",
"weight": "Bolder",
"color": "Attention"
},
{
"type": "TextBlock",
"wrap": true,
"text": "${error_message}",
"color": "Attention"
},
{
"type": "TextBlock",
"text": "${error_stack}",
"fontType": "Monospace"
},
{
"type": "TextBlock",
"wrap": true,
"text": "Inner exception:"
},
{
"type": "TextBlock",
"wrap": true,
"text": "${inner_exception}",
"color": "Attention"
}
]
}
""";
}