Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
KycPage.cs
1using System;
3using System.Collections.ObjectModel;
4using System.ComponentModel;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using CommunityToolkit.Mvvm.ComponentModel;
9using Microsoft.Maui.ApplicationModel;
13
15{
19 public partial class KycPage : ObservableObject, IDisposable
20 {
21 private bool disposed;
22 private readonly List<IDisposable> subscriptions = new();
23 private IDictionary<string, string?> values = null!;
24
25 public KycPage()
26 {
27 this.VisibleFieldsCollection = new FilteredObservableCollection<ObservableKycField>(this.AllFields, Field => Field.IsVisible);
28 this.VisibleSectionsCollection = new FilteredObservableCollection<KycSection>(this.AllSections, Section => Section.IsVisible);
29 }
30
34 public string Id { get; init; } = string.Empty;
38 public KycLocalizedText? Title { get; set; }
42 public KycLocalizedText? Description { get; set; }
46 public KycCondition? Condition { get; set; }
47
51 public ObservableCollection<ObservableKycField> AllFields { get; } = new();
59 public ReadOnlyObservableCollection<ObservableKycField> VisibleFields => this.VisibleFieldsCollection;
60
64 public ObservableCollection<KycSection> AllSections { get; } = new();
72 public ReadOnlyObservableCollection<KycSection> VisibleSections => this.VisibleSectionsCollection;
73
78 public void UpdateVisibilities(IDictionary<string, string?> Values)
79 {
80 foreach (ObservableKycField Field in this.AllFields)
81 {
82 Field.IsVisible = Field.Condition?.Evaluate(Values) ?? true;
83 }
84 foreach (KycSection Section in this.AllSections)
85 {
86 Section.UpdateVisibilities(Values);
87 }
88
89 this.VisibleFieldsCollection.Refresh();
90 this.VisibleSectionsCollection.Refresh();
91 }
92
98 public bool IsVisible(IDictionary<string, string?> Values)
99 {
100 return this.Condition?.Evaluate(Values) ?? true;
101 }
102
103 protected virtual void Dispose(bool Disposing)
104 {
105 if (this.disposed)
106 {
107 return;
108 }
109 if (Disposing)
110 {
111 this.VisibleFieldsCollection.Dispose();
112 this.VisibleSectionsCollection.Dispose();
113 this.subscriptions.ForEach(Sub => Sub.Dispose());
114 this.subscriptions.Clear();
115 }
116 this.disposed = true;
117 }
118
119 public void Dispose()
120 {
121 this.Dispose(true);
122 GC.SuppressFinalize(this);
123 }
124
129 internal void InitFieldValueNotifications(IDictionary<string, string?> Values)
130 {
131 this.values = Values;
132 foreach (ObservableKycField Field in this.AllFields)
133 {
134 Field.PropertyChanged += this.Field_ValueChanged;
135 }
136 foreach (KycSection Section in this.AllSections)
137 {
138 foreach (ObservableKycField Field in Section.AllFields)
139 {
140 Field.PropertyChanged += this.Field_ValueChanged;
141 }
142 }
143 }
144
145 private void Field_ValueChanged(object? Sender, PropertyChangedEventArgs E)
146 {
147 if (E.PropertyName == nameof(ObservableKycField.RawValue) && Sender is ObservableKycField Field)
148 {
149 this.values[Field.Id] = Field.StringValue;
150 this.UpdateVisibilities(this.values);
151 if (Field.FieldType == FieldType.Country && string.Equals(Field.Id, "country", StringComparison.OrdinalIgnoreCase))
152 this.HandleCountryFieldChanged(Field);
153 }
154 }
155
156 private void HandleCountryFieldChanged(ObservableKycField CountryField)
157 {
158 if (CountryField is null)
159 return;
160
161 string CountryCode = CountryField.StringValue ?? string.Empty;
162 if (CountryField.TryGetOwnerProcess(out KycProcess? Process))
163 {
164 Process.Values[CountryField.Id] = CountryCode;
165 string PlaceholderExample = string.IsNullOrEmpty(CountryCode) ? string.Empty : PersonalNumberSchemes.DisplayStringForCountry(CountryCode) ?? string.Empty;
166
167 MainThread.BeginInvokeOnMainThread(() =>
168 {
169 foreach (KycPage Page in Process.Pages)
170 {
171 this.UpdatePersonalNumberFields(Page.VisibleFields, CountryCode, PlaceholderExample);
172 foreach (KycSection Section in Page.VisibleSections)
173 this.UpdatePersonalNumberFields(Section.VisibleFields, CountryCode, PlaceholderExample);
174 }
175 });
176 }
177 }
178
179 private void UpdatePersonalNumberFields(IEnumerable<ObservableKycField> Fields, string CountryCode, string PlaceholderExample)
180 {
181 foreach (ObservableKycField Field in Fields)
182 {
183 if (!Field.IsVisible)
184 continue;
185
186 if (!MapsToPersonalNumber(Field))
187 continue;
188
189 this.ApplyPersonalNumberPlaceholder(Field, CountryCode, PlaceholderExample);
191 Field.ValidationTask.Run();
192 }
193 }
194
195 private void ApplyPersonalNumberPlaceholder(ObservableKycField Field, string CountryCode, string PlaceholderExample)
196 {
197 if (Field.Placeholder is null)
198 Field.Placeholder = new KycLocalizedText();
199
200 string PlaceholderKey = string.IsNullOrEmpty(CountryCode) ? "en" : CountryCode;
201 string PlaceholderValue = PlaceholderExample ?? string.Empty;
202 Field.Placeholder.Add(PlaceholderKey, PlaceholderValue);
203 }
204
205 private static bool MapsToPersonalNumber(ObservableKycField Field)
206 {
207 foreach (KycMapping Mapping in Field.Mappings)
208 {
209 if (string.Equals(Mapping.Key, Constants.XmppProperties.PersonalNumber, StringComparison.OrdinalIgnoreCase))
210 return true;
211 }
212
213 return string.Equals(Field.Id, "personalNumber", StringComparison.OrdinalIgnoreCase);
214 }
215 }
216}
const string PersonalNumber
Personal number
Definition: Constants.cs:394
A set of never changing property constants and helpful values.
Definition: Constants.cs:24
Personal Number Schemes available in different countries.
static ? string DisplayStringForCountry(string CountryCode)
Gets the expected personal number format for the given country.
Represents a set of localized strings, addressable by language code (e.g. "en", "sv").
Represents a page in a KYC process containing fields and sections.
Definition: KycPage.cs:20
ReadOnlyObservableCollection< ObservableKycField > VisibleFields
Gets the read-only collection of visible fields in the page.
Definition: KycPage.cs:59
ObservableCollection< KycSection > AllSections
Gets all sections contained in the page.
Definition: KycPage.cs:64
FilteredObservableCollection< KycSection > VisibleSectionsCollection
Backing collection tracking sections whose KycSection.IsVisible is true.
Definition: KycPage.cs:68
string Id
Gets the page identifier.
Definition: KycPage.cs:34
void UpdateVisibilities(IDictionary< string, string?> Values)
Updates visibility flags for fields and sections based on current values.
Definition: KycPage.cs:78
FilteredObservableCollection< ObservableKycField > VisibleFieldsCollection
Backing collection tracking fields whose ObservableKycField.IsVisible is true.
Definition: KycPage.cs:55
KycLocalizedText? Description
Gets or sets the localized page description.
Definition: KycPage.cs:42
ReadOnlyObservableCollection< KycSection > VisibleSections
Gets the read-only collection of visible sections in the page.
Definition: KycPage.cs:72
KycCondition? Condition
Gets or sets an optional page condition controlling visibility.
Definition: KycPage.cs:46
KycLocalizedText? Title
Gets or sets the localized page title.
Definition: KycPage.cs:38
bool IsVisible(IDictionary< string, string?> Values)
Gets a value indicating whether the page is visible based on current values and its condition.
Definition: KycPage.cs:98
ObservableCollection< ObservableKycField > AllFields
Gets all fields directly contained in the page (excluding sections).
Definition: KycPage.cs:51
The base KYC field model. Use as-is for generic fields, or subclass for custom logic.
string Id
The unique identifier for the field.
ObservableCollection< KycMapping > Mappings
Mappings to apply output.
KycCondition? Condition
Conditional display of this field.
void ForceSynchronousValidation()
Forces immediate synchronous validation (used when advancing pages or explicit full validation is req...
KycLocalizedText? Placeholder
Placeholder text for the field input.
Provides a live, read-only filtered view of an ObservableCollection<T> that stays in sync and maintai...
void Run()
Start the configured task now (no CanExecute gating).
FieldType
Enumeration of all supported field types in the KYC process.