Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IoTScCodeBlock.cs
4using System.Globalization;
5using System.Text;
6using System.Xml;
11
13{
18 {
19 private MarkdownDocument? document;
20
25 {
26 }
27
31 public MarkdownDocument? Document => this.document;
32
36 public bool EvaluatesScript => false;
37
41 public async Task<bool> RenderMauiXaml(MauiXamlRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
42 {
43 XmlWriter Output = Renderer.XmlOutput;
45
46 try
47 {
48 StringBuilder sb = new();
49
50 foreach (string Row in Rows)
51 sb.AppendLine(Row);
52
53 XmlDocument Doc = new()
54 {
55 PreserveWhitespace = true
56 };
57 Doc.LoadXml(sb.ToString());
58
59 ParsedContract Parsed = await Contract.Parse(Doc.DocumentElement, ServiceRef.XmppService.ContractsClient, false);
60 if (Parsed is null)
61 return false;
62
63 Contract = Parsed.Contract;
64 }
65 catch (Exception ex)
66 {
67 Output.WriteStartElement("Label");
68 Output.WriteAttributeString("Text", ex.Message);
69 Output.WriteAttributeString("FontFamily", "Courier New");
70 Output.WriteAttributeString("TextColor", "Red");
71 Output.WriteAttributeString("LineBreakMode", "WordWrap");
72 Output.WriteEndElement();
73
74 return false;
75 }
76
77 Output.WriteStartElement("VerticalStackLayout");
78 Output.WriteAttributeString("HorizontalOptions", "Center");
79
80 bool ImageShown = false;
81
82 if (Contract.Attachments is not null)
83 {
84 (string? FileName, int Width, int Height) = await PhotosLoader.LoadPhotoAsTemporaryFile(Contract.Attachments,
86
87 if (!string.IsNullOrEmpty(FileName))
88 {
89 Output.WriteStartElement("Image");
90 Output.WriteAttributeString("Source", FileName);
91 Output.WriteAttributeString("WidthRequest", Width.ToString(CultureInfo.InvariantCulture));
92 Output.WriteAttributeString("HeightRequest", Height.ToString(CultureInfo.InvariantCulture));
93 Output.WriteEndElement();
94
95 ImageShown = true;
96 }
97 }
98
99 if (!ImageShown)
100 {
101 Output.WriteStartElement("Path");
102 Output.WriteAttributeString("VerticalOptions", "Center");
103 Output.WriteAttributeString("HorizontalOptions", "Center");
104 Output.WriteAttributeString("HeightRequest", "16");
105 Output.WriteAttributeString("WidthRequest", "16");
106 Output.WriteAttributeString("Aspect", "Uniform");
107 Output.WriteAttributeString("Fill", "{AppThemeBinding Light={StaticResource PrimaryForegroundLight}, Dark={StaticResource PrimaryForegroundDark}}");
108 Output.WriteAttributeString("Data", "{x:Static ui:Geometries.ContractPath}");
109 Output.WriteEndElement();
110 }
111
112 string? FriendlyName = await ContractModel.GetCategory(Contract);
113
114 Output.WriteStartElement("Label");
115 Output.WriteAttributeString("LineBreakMode", "WordWrap");
116 Output.WriteAttributeString("FontSize", "Medium");
117 Output.WriteAttributeString("HorizontalOptions", "Center");
118 Output.WriteAttributeString("Text", FriendlyName);
119 Output.WriteEndElement();
120
121 Output.WriteStartElement("VerticalStackLayout.GestureRecognizers");
122
123 StringBuilder Xml = new();
124 Contract.Serialize(Xml, true, true, true, true, true, true, true);
125
126 Output.WriteStartElement("TapGestureRecognizer");
127 Output.WriteAttributeString("Command", "{Binding Path=IotScUriClicked}");
128 Output.WriteAttributeString("CommandParameter", Constants.UriSchemes.IotSc + ":" + Xml.ToString());
129 Output.WriteEndElement();
130
131 Output.WriteEndElement();
132 Output.WriteEndElement();
133
134 return true;
135 }
136
142 {
143 this.document = Document;
144 }
145
151 public Grade Supports(string Language)
152 {
153 return string.Equals(Language, Constants.UriSchemes.IotSc, StringComparison.OrdinalIgnoreCase) ? Grade.Excellent : Grade.NotAtAll;
154 }
155 }
156}
const int DefaultImageHeight
The default height to use when generating QR Code images.
Definition: Constants.cs:866
const int DefaultImageWidth
The default width to use when generating QR Code images.
Definition: Constants.cs:862
const string IotSc
The IoT Smart Contract URI Scheme (iotsc)
Definition: Constants.cs:109
A set of never changing property constants and helpful values.
Definition: Constants.cs:7
Base class that references services in the app.
Definition: ServiceRef.cs:31
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:67
static async Task< string?> GetCategory(Contract Contract)
Gets the category of a contract
async Task< bool > RenderMauiXaml(MauiXamlRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
Generates Maui XAML
MarkdownDocument? Document
Markdown document.
IoTScCodeBlock()
Handles embedded Smart Contracts.
void Register(MarkdownDocument Document)
Registers the Markdown document in which the construct resides.
bool EvaluatesScript
If script is evaluated for this type of code block.
Grade Supports(string Language)
How much the module supports code of a given language (i.e. type of content)
Renders XAML (Maui flavour) from a Markdown document.
Contains a markdown document. This markdown document class supports original markdown,...
Contains the definition of a contract
Definition: Contract.cs:22
static Task< ParsedContract > Parse(XmlDocument Xml)
Validates a contract XML Document, and returns the contract definition in it.
Definition: Contract.cs:397
Attachment[] Attachments
Attachments assigned to the legal identity.
Definition: Contract.cs:318
void Serialize(StringBuilder Xml, bool IncludeNamespace, bool IncludeIdAttribute, bool IncludeClientSignatures, bool IncludeAttachments, bool IncludeStatus, bool IncludeServerSignature, bool IncludeAttachmentReferences)
Serializes the Contract, in normalized form.
Definition: Contract.cs:1542
Contains information about a parsed contract.
Interface for all markdown handlers of code content.
Definition: ICodeContent.cs:9
Grade
Grade enumeration
Definition: Grade.cs:7