Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObservablePart.cs
1using System;
3using System.Collections.ObjectModel;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using CommunityToolkit.Mvvm.ComponentModel;
8using CommunityToolkit.Mvvm.Input;
21
23{
24 public partial class ObservablePart : ObservableObject, IDisposable
25 {
26 public ObservablePart(Part part)
27 {
28 this.Part = part;
29 ServiceRef.XmppService.PetitionedIdentityResponseReceived += this.XmppService_OnPetitionedIdentityResponseReceived;
30 }
31
32 public ObservablePart(Part part, ClientSignature signature)
33 {
34 this.Part = part;
35 ServiceRef.XmppService.PetitionedIdentityResponseReceived += this.XmppService_OnPetitionedIdentityResponseReceived;
36 this.Signature = signature;
37
38 }
42 public async Task InitializeAsync(bool sendPetition = true)
43 {
44 try
45 {
46 // Set Friendly name before anything can go wrong
47 this.FriendlyName = await this.GetFriendlyNameAsync();
48 if (sendPetition)
49 await this.PetitionIdentityAsync();
50 else
51 {
52 try
53 {
54 LegalIdentity Identity = await ServiceRef.XmppService.GetLegalIdentity(this.LegalId);
55 this.identity = Identity;
56
57 string FriendlyName = await this.GetFriendlyNameAsync();
58
59 MainThread.BeginInvokeOnMainThread(() =>
60 {
61 this.OnPropertyChanged(nameof(this.HasIdentity));
62 this.FriendlyName = FriendlyName;
63 });
64 }
65 catch (Exception)
66 {
67 //Ignore, OK if forbidden or network error
68 }
69 }
70 }
71 catch (Exception E)
72 {
73 ServiceRef.LogService.LogException(E);
74 }
75 }
76
77 private async Task XmppService_OnPetitionedIdentityResponseReceived(object? Sender, LegalIdentityPetitionResponseEventArgs e)
78 {
79 if (e.RequestedIdentity is not null && e.RequestedIdentity.Id == this.LegalId)
80 {
81 this.identity = e.RequestedIdentity;
82 string FriendlyName = await this.GetFriendlyNameAsync();
83
84 MainThread.BeginInvokeOnMainThread(() =>
85 {
86 this.FriendlyName = FriendlyName;
87 this.HasSentPetition = false;
88 this.OnPropertyChanged(nameof(this.HasIdentity));
89 this.OnPropertyChanged(nameof(this.CanSendProposal));
90 this.OnPropertyChanged(nameof(this.HasSentPetition));
91 });
92 }
93 }
94
95 private async Task PetitionIdentityAsync()
96 {
97 this.identity = await ServiceRef.ContractOrchestratorService.TryGetLegalIdentity(this.LegalId,
99
100 string FriendlyName = await this.GetFriendlyNameAsync();
101
102 MainThread.BeginInvokeOnMainThread(() =>
103 {
104
105 if (this.identity is null)
106 this.HasSentPetition = true;
107 this.OnPropertyChanged(nameof(this.HasIdentity));
108 this.OnPropertyChanged(nameof(this.CanSendProposal));
109 this.OnPropertyChanged(nameof(this.HasSentPetition));
110 this.FriendlyName = FriendlyName;
111 });
112
113 }
114
115 private async Task<string> GetFriendlyNameAsync()
116 {
117 try
118 {
119 if (this.IsMe)
120 return ServiceRef.Localizer[nameof(AppResources.Me)];
121
122 if (this.Part.LegalId == ServiceRef.TagProfile.TrustProviderId && !string.IsNullOrEmpty(ServiceRef.TagProfile.Domain))
123 return ServiceRef.TagProfile.Domain;
124
125 ContactInfo Info = await Database.FindFirstIgnoreRest<ContactInfo>(new FilterFieldEqualTo("LegalId", this.Part.LegalId));
126 if (Info is not null && !string.IsNullOrEmpty(Info.FriendlyName))
127 return Info.FriendlyName;
128
129 if (this.identity is not null)
130 return ContactInfo.GetFriendlyName(this.identity);
131
132 if (Info is not null && !string.IsNullOrEmpty(Info.BareJid))
133 return Info.BareJid;
134
135 if (this.Signature is not null)
136 return this.Signature.BareJid;
137 }
138 catch (Exception E)
139 {
140 ServiceRef.LogService.LogException(E);
141 //log and use fallback
142 }
143
144 return this.LegalId;
145 }
146 #region Properties
147 private LegalIdentity? identity;
148
152 public Part Part { get; }
153
154 private ClientSignature? signature = null;
156 {
157 get => this.signature;
158 set
159 {
160 this.SetProperty(ref this.signature, value);
161 this.OnPropertyChanged(nameof(this.HasSigned));
162 this.OnPropertyChanged(nameof(this.CanSendProposal));
163 this.OpenSignatureCommand.NotifyCanExecuteChanged();
164 }
165 }
166
167 public string LegalId => this.Part.LegalId;
168
169
170 public string Role => this.Part.Role;
171
176 public string? FriendlyName
177 {
178 get => this.friendlyName;
179 private set
180 {
181 this.SetProperty(ref this.friendlyName, value);
182 }
183 }
184 private string? friendlyName = string.Empty;
185
186 public bool HasSentPetition
187 {
188 get => this.hasSentPetition && !this.HasIdentity;
189 private set
190 {
191 this.SetProperty(ref this.hasSentPetition, value);
192 }
193 }
194 private bool hasSentPetition = false;
195
196 public bool IsMe => this.Part.LegalId == ServiceRef.TagProfile.LegalIdentity?.Id;
197 public bool IsThirdParty => !this.IsMe;
198
199 public bool HasIdentity => this.identity is not null;
200
201 public bool HasSigned => this.Signature is not null;
202
203 public bool CanSendProposal => this.IsThirdParty && this.HasIdentity && !this.HasSigned && this.Role != "TrustProvider";
204
209 {
210 get => this.isPresetFromArgs;
211 set
212 {
213 if (this.SetProperty(ref this.isPresetFromArgs, value))
214 {
215 this.OnPropertyChanged(nameof(this.ShowRemoveAsMe));
216 this.OnPropertyChanged(nameof(this.ShowRemoveAsThirdParty));
217 }
218 }
219 }
220 private bool isPresetFromArgs = false;
221
225 public bool ShowRemoveAsMe => this.IsMe && !this.IsPresetFromArgs;
226
230 public bool ShowRemoveAsThirdParty => this.IsThirdParty && !this.IsPresetFromArgs;
231
232 #endregion
233
234
235 #region Commands
236 [RelayCommand(AllowConcurrentExecutions = false)]
237 private async Task OpenIdentityAsync()
238 {
239 if (this.identity is null)
240 {
241 try
242 {
243 await this.PetitionIdentityAsync();
245 }
246 catch (Exception)
247 {
249 }
250 }
251 else
252 {
253 await ServiceRef.NavigationService.GoToAsync(nameof(ViewIdentityPage), new ViewIdentityNavigationArgs(this.identity), Services.UI.BackMethod.Pop);
254 }
255 }
256
257 [RelayCommand(AllowConcurrentExecutions = false, CanExecute = nameof(HasSigned))]
258 private async Task OpenSignatureAsync()
259 {
260 if (this.Signature is not null)
261 {
262 await ServiceRef.NavigationService.GoToAsync(nameof(ClientSignaturePage),
263 new ClientSignatureNavigationArgs(this.Signature, this.identity),
264 Services.UI.BackMethod.Pop);
265 }
266 }
267 #endregion
268
269 private bool disposed = false;
270
271 #region IDisposable Support
272 public void Dispose()
273 {
274 this.Dispose(true);
275 GC.SuppressFinalize(this);
276 }
277
278 protected virtual void Dispose(bool disposing)
279 {
280 if (this.disposed)
281 return;
282
283 if (disposing)
284 {
285 // Unsubscribe from the event to prevent memory leaks
286 ServiceRef.XmppService.PetitionedIdentityResponseReceived -= this.XmppService_OnPetitionedIdentityResponseReceived;
287 }
288
289 this.disposed = true;
290 }
291 #endregion
292 }
293}
A strongly-typed resource class, for looking up localized strings, etc.
static string Petition
Looks up a localized string similar to Request.
static string Me
Looks up a localized string similar to (me).
static string PetitionIdentitySent
Looks up a localized string similar to A request to view ID has been sent.
static string Ok
Looks up a localized string similar to OK.
static string SomethingWentWrong
Looks up a localized string similar to Something went wrong.
static string Error
Looks up a localized string similar to Error.
static string ForInclusionInContract
Looks up a localized string similar to For inclusion as part in a contract..
Contains information about a contact.
Definition: ContactInfo.cs:22
static async Task< string > GetFriendlyName(CaseInsensitiveString RemoteId)
Gets the friendly name of a remote identity (Legal ID or Bare JID).
Definition: ContactInfo.cs:258
CaseInsensitiveString BareJid
Bare JID of contact.
Definition: ContactInfo.cs:63
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
static INavigationService NavigationService
The navigation service for navigating between pages.
Definition: ServiceRef.cs:178
static ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:202
static IContractOrchestratorService ContractOrchestratorService
Contract orchestrator service.
Definition: ServiceRef.cs:238
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:190
bool ShowRemoveAsThirdParty
Helper property for XAML: show the remove cross for third-party parts if not preset from args.
bool IsPresetFromArgs
True if this part was preselected via navigation args and therefore should not be removable in the Ro...
async Task InitializeAsync(bool sendPetition=true)
Initializes the part, setting properties which needs to be set asynchronosly
string? FriendlyName
The friendly name for the part Has to be initialized with InitializeAsync
bool ShowRemoveAsMe
Helper property for XAML: show the remove cross for "me" parts if not preset from args.
A page to display when the user wants to view an identity.
Holds navigation parameters specific to views displaying a client signature.
Represents a digital signature on a contract.
Class defining a part in a contract
Definition: Part.cs:30
string LegalId
Legal identity of part
Definition: Part.cs:38
string Role
Role of the part in the contract
Definition: Part.cs:57
Class defining a role
Definition: Role.cs:7
Abstract base class of signatures
Definition: Signature.cs:10
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
This filter selects objects that have a named field equal to a given value.