Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmppFormViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using CommunityToolkit.Mvvm.Input;
5using System.Collections.ObjectModel;
9
11{
15 public partial class XmppFormViewModel : XmppViewModel
16 {
17 private readonly DataForm? form;
18 private bool responseSent;
19
24 public XmppFormViewModel(XmppFormNavigationArgs? Args)
25 : base()
26 {
27 this.Pages = [];
28
29 if (Args is not null)
30 {
31 this.form = Args.Form;
32 this.responseSent = false;
33
34 // TODO: Post-back fields.
35
36 this.Title = this.form?.Title ?? string.Empty;
37 this.Instructions = this.form?.Instructions ?? [];
38
39 if (this.form?.HasPages ?? false)
40 {
41 foreach (Layout.Page P in this.form.Pages)
42 {
43 this.Pages.Add(new PageModel(this, P));
44 }
45 this.MultiplePages = this.form.Pages.Length > 1;
46 }
47 else
48 {
49 List<Layout.LayoutElement> Elements = [];
50
51 foreach (Field F in this.form?.Fields ?? [])
52 {
53 if (F is HiddenField)
54 continue;
55
56 Elements.Add(new Layout.FieldReference(this.form, F.Var));
57 }
58
59 this.Pages.Add(new PageModel(this, new Layout.Page(this.form, string.Empty, Elements.ToArray())));
60
61 this.MultiplePages = false;
62 }
63
64 this.ValidateForm();
65 }
66 }
67
69 protected override async Task OnDispose()
70 {
71 if (this.form is not null && this.form.CanCancel && !this.responseSent)
72 {
73 this.form.Cancel();
74 this.responseSent = true;
75 }
76
77 await base.OnDispose();
78 }
79
80 #region Properties
81
85 [ObservableProperty]
86 private string? title;
87
91 [ObservableProperty]
92 private string[]? instructions;
93
97 public ObservableCollection<PageModel> Pages { get; }
98
102 [ObservableProperty]
103 private bool isFormOk;
104
108 [ObservableProperty]
109 private bool multiplePages;
110
111 #endregion
112
113 #region Commands
114
118 [RelayCommand]
119 private async Task Submit()
120 {
121 if (!this.IsFormOk)
122 return;
123
124 try
125 {
126 if (this.form is not null && this.form.CanSubmit && !this.responseSent)
127 {
128 this.form.Submit();
129 this.responseSent = true;
130
131 await this.GoBack();
132 }
133 }
134 catch (Exception ex)
135 {
136 await ServiceRef.UiService.DisplayException(ex);
137 }
138 }
139
140 internal void ValidateForm()
141 {
142 try
143 {
144 foreach (Field F in this.form?.Fields ?? [])
145 {
146 if (F.HasError)
147 {
148 this.IsFormOk = false;
149 return;
150 }
151 }
152
153 this.IsFormOk = true;
154 }
155 catch (Exception)
156 {
157 this.IsFormOk = false;
158 }
159 }
160
161 #endregion
162 }
163}
Base class that references services in the app.
Definition: ServiceRef.cs:31
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
virtual async Task GoBack()
Method called when user wants to navigate to the previous screen.
The view model to bind to for when displaying the calculator.
ObservableCollection< PageModel > Pages
Holds the pages of the form
XmppFormViewModel(XmppFormNavigationArgs? Args)
Creates an instance of the XmppFormViewModel class.
override async Task OnDispose()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
A view model that holds the XMPP state.
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
Base class for form fields
Definition: Field.cs:16
Definition: App.xaml.cs:4