Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ContactInfoModel.cs
1using System.ComponentModel;
2using System.Text;
3using System.Windows.Input;
4using CommunityToolkit.Mvvm.ComponentModel;
5using CommunityToolkit.Mvvm.Input;
19
21{
25 public partial class ContactInfoModel : INotifyPropertyChanged, IUniqueItem
26 {
27 private readonly ContactInfo? contact;
28 private NotificationEvent[] events;
29
30 private readonly ICommand toggleSubscriptionCommand;
31
38 {
39 this.contact = Contact;
40 this.events = Events;
41
42 this.toggleSubscriptionCommand = new Command(async () => await this.ToggleSubscription(), () => this.CanToggleSubscription());
43
44 this.OnPropertyChanged(nameof(this.IsFriend));
45 }
46
48 public string UniqueName => this.contact?.ThingNotificationCategoryKey ?? string.Empty;
49
53 public ContactInfo? Contact => this.contact;
54
58 public CaseInsensitiveString? BareJid => this.contact?.BareJid;
59
63 public CaseInsensitiveString? LegalId => this.contact?.LegalId;
64
68 public LegalIdentity? LegalIdentity => this.contact?.LegalIdentity;
69
73 public string? FriendlyName => this.contact?.FriendlyName;
74
78 public string? SourceId => this.contact?.SourceId;
79
83 public string? Partition => this.contact?.Partition;
84
88 public string? NodeId => this.contact?.NodeId;
89
93 public CaseInsensitiveString? RegistryJid => this.contact?.RegistryJid;
94
98 public bool? SubcribeTo => this.contact?.SubscribeTo;
99
103 public bool? AllowSubscriptionFrom => this.contact?.AllowSubscriptionFrom;
104
108 public bool? IsThing => this.contact?.IsThing;
109
113 public bool? Owner => this.contact?.Owner;
114
118 public Property[]? MetaData => this.contact?.MetaData;
119
123 public NotificationEvent[] Events => this.events;
124
128 public bool HasEvents => this.events is not null && this.events.Length > 0;
129
133 public int NrEvents => this.events?.Length ?? 0;
134
138 public Color ConnectionColor
139 {
140 get
141 {
142 if (string.IsNullOrEmpty(this.contact?.BareJid))
143 return AppColors.ContentSecondary;
144
145 RosterItem? Item = null;
146 try
147 {
148 Item = ServiceRef.XmppService.GetRosterItem(this.contact?.BareJid);
149 }
150 catch (Exception)
151 {
152 return AppColors.ContentSecondary;
153 }
154 if (Item is null)
155 return AppColors.ContentSecondary;
156
157 if (Item.State != SubscriptionState.To && Item.State != SubscriptionState.Both)
158 return AppColors.ContentSecondary;
159
160 if (!Item.HasLastPresence)
161 return AppColors.ContentSecondary;
162
163 return Item.LastPresence.Availability switch
164 {
165 Availability.Online or Availability.Chat => AppColors.TnPSuccessFigure,
166 Availability.Away or Availability.ExtendedAway => AppColors.TnPWarningContent,
167 Availability.DoNotDisturb => AppColors.TnPDangerContent,
168 _ => AppColors.ContentSecondary,
169 };
170 }
171 }
172
177 {
178 get
179 {
180 if (string.IsNullOrEmpty(this.contact?.BareJid))
181 return AppColors.ButtonNeutralNavButtonsOnContainerbgActive;
182
183 RosterItem? Item = null;
184 try
185 {
186 Item = ServiceRef.XmppService.GetRosterItem(this.contact?.BareJid);
187 }
188 catch (Exception)
189 {
190 return AppColors.ButtonNeutralNavButtonsOnContainerbgActive;
191 }
192 if (Item is null)
193 return AppColors.ButtonNeutralNavButtonsOnContainerbgActive;
194 if (Item.State != SubscriptionState.To && Item.State != SubscriptionState.Both)
195 return AppColors.ButtonNeutralNavButtonsOnContainerbgActive;
196 if (!Item.HasLastPresence)
197 return AppColors.ButtonNeutralNavButtonsOnContainerbgActive;
198
199 return Item.LastPresence.Availability switch
200 {
201 Availability.Online or Availability.Chat => AppColors.TnPSuccessBg,
202 Availability.Away or Availability.ExtendedAway => AppColors.TnPWarningBg,
203 Availability.DoNotDisturb => AppColors.TnPDangerContent,
204 _ => AppColors.ButtonNeutralNavButtonsOnContainerbgActive,
205 };
206 }
207 }
208
209 public bool IsFriend
210 {
211 get
212 {
213 if (string.IsNullOrEmpty(this.contact?.BareJid))
214 return false;
215 RosterItem? Item;
216 try
217 {
218 Item = ServiceRef.XmppService.GetRosterItem(this.contact?.BareJid);
219 }
220 catch (Exception)
221 {
222 return false;
223 }
224 if (Item is null)
225 return false;
226 return Item.State == SubscriptionState.To || Item.State == SubscriptionState.Both;
227 }
228 }
229
233 public ICommand ToggleSubscriptionCommand => this.toggleSubscriptionCommand;
234
240 {
241 return !string.IsNullOrEmpty(this.contact?.BareJid);
242 }
243
247 public async Task ToggleSubscription()
248 {
249 if (string.IsNullOrEmpty(this.contact?.BareJid))
250 return;
251
252 RosterItem? Item = null;
253 try
254 {
255 Item = ServiceRef.XmppService.GetRosterItem(this.contact?.BareJid);
256 }
257 catch
258 {
260 return;
261 }
262
263 bool Subscribed;
264
265 if (Item is null)
266 Subscribed = false;
267 else
268 Subscribed = Item.State == SubscriptionState.To || Item.State == SubscriptionState.Both;
269
270 if (Subscribed)
271 {
273 ServiceRef.Localizer[nameof(AppResources.RemoveSubscriptionFrom), this.FriendlyName ?? string.Empty],
275 {
276 return;
277 }
278
279 ServiceRef.XmppService.RequestPresenceUnsubscription(this.BareJid);
280
281 if (Item is not null && (Item.State == SubscriptionState.From || Item.State == SubscriptionState.Both))
282 {
283 RemoveSubscriptionViewModel ViewModel = new(this.BareJid);
284 RemoveSubscriptionPopup Page = new(ViewModel);
285
286 await ServiceRef.PopupService.PushAsync(Page);
287 bool? Remove = await ViewModel.Result;
288
289 if (Remove.HasValue && Remove.Value)
290 {
291 ServiceRef.XmppService.RequestRevokePresenceSubscription(this.BareJid);
292
293 if ((this.contact?.AllowSubscriptionFrom.HasValue ?? false) && this.contact.AllowSubscriptionFrom.Value)
294 {
295 this.contact.AllowSubscriptionFrom = null;
296 await Database.Update(this.contact);
297 }
298 }
299 }
300 }
301 else
302 {
303 SubscribeToViewModel ViewModel = new(this.BareJid);
304 SubscribeToPopup Page = new(ViewModel);
305
306 await ServiceRef.PopupService.PushAsync(Page);
307 bool? SubscribeTo = await ViewModel.Result;
308
309 if (SubscribeTo.HasValue && SubscribeTo.Value)
310 {
311 string IdXml;
312
313 if (ServiceRef.TagProfile.LegalIdentity is null)
314 IdXml = string.Empty;
315 else
316 {
317 StringBuilder Xml = new();
318 ServiceRef.TagProfile.LegalIdentity.Serialize(Xml, true, true, true, true, true, true, true);
319 IdXml = Xml.ToString();
320 }
321
322 ServiceRef.XmppService.RequestPresenceSubscription(this.BareJid, IdXml);
323 }
324 }
325
326 this.OnPropertyChanged(nameof(this.IsFriend));
327 }
328
332 public void PresenceUpdated()
333 {
334 this.OnPropertyChanged(nameof(this.ConnectionColor));
336 this.OnPropertyChanged(nameof(this.IsFriend));
337 }
338
343 public void OnPropertyChanged(string PropertyName)
344 {
345 try
346 {
347 this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
348 }
349 catch (Exception ex)
350 {
351 ServiceRef.LogService.LogException(ex);
352 }
353 }
354
358 public event PropertyChangedEventHandler? PropertyChanged;
359
365 {
366 this.events = Events;
367
368 this.OnPropertyChanged(nameof(this.Events));
369 this.OnPropertyChanged(nameof(this.HasEvents));
370 this.OnPropertyChanged(nameof(this.NrEvents));
371 }
372
377 public void AddEvent(NotificationEvent Event)
378 {
379 if (this.events is null)
380 this.NotificationsUpdated([Event]);
381 else
382 {
383 foreach (NotificationEvent Event2 in this.events)
384 {
385 if (Event2.ObjectId == Event.ObjectId)
386 return;
387 }
388
389 int c = this.events.Length;
390 NotificationEvent[] NewArray = new NotificationEvent[c + 1];
391 Array.Copy(this.events, 0, NewArray, 0, c);
392 NewArray[c] = Event;
393
394 this.NotificationsUpdated(NewArray);
395 }
396 }
397
403 {
404 if (this.events is not null)
405 {
406 int i, c = this.events.Length;
407
408 for (i = 0; i < c; i++)
409 {
410 NotificationEvent Event2 = this.events[i];
411
412 if (Event2.ObjectId == Event.ObjectId)
413 {
414 NotificationEvent[] NewArray = new NotificationEvent[c - 1];
415
416 if (i > 0)
417 Array.Copy(this.events, 0, NewArray, 0, i);
418
419 if (i < c - 1)
420 Array.Copy(this.events, i + 1, NewArray, i, c - i - 1);
421
422 this.NotificationsUpdated(NewArray);
423
424 return;
425 }
426 }
427
428 }
429 }
430
431 [RelayCommand]
432 public async Task ViewContact()
433 {
434 if (this.LegalIdentity is not null)
435 {
436 ViewIdentityNavigationArgs ViewIdentityArgs = new(this.LegalIdentity);
437
438 await ServiceRef.NavigationService.GoToAsync(nameof(ViewIdentityPage), ViewIdentityArgs);
439 }
440 else if (!string.IsNullOrEmpty(this.LegalId))
441 {
442 await ServiceRef.ContractOrchestratorService.OpenLegalIdentity(this.LegalId,
444 }
445 }
446
447 [RelayCommand]
448 public async Task ViewChat()
449 {
450 if (!string.IsNullOrEmpty(this.BareJid) && this.Contact is not null)
451 {
452 ChatNavigationArgs ChatArgs = new(this.Contact);
453 await ServiceRef.NavigationService.GoToAsync(nameof(ChatPage), ChatArgs, BackMethod.Inherited, this.BareJid);
454 }
455 }
456 }
457}
A strongly-typed resource class, for looking up localized strings, etc.
static string Yes
Looks up a localized string similar to Yes.
static string Cancel
Looks up a localized string similar to Cancel.
static string Question
Looks up a localized string similar to Question.
static string NetworkSeemsToBeMissing
Looks up a localized string similar to Network seems to be missing. Please check your configuration a...
static string Ok
Looks up a localized string similar to OK.
static string RemoveSubscriptionFrom
Looks up a localized string similar to Do you want to remove the presence subscription from {0}?...
static string ScannedQrCode
Looks up a localized string similar to Scanned QR Code.
static string Error
Looks up a localized string similar to Error.
Contains information about a contact.
Definition: ContactInfo.cs:22
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 IPopupService PopupService
Popup service for presenting application popups.
Definition: ServiceRef.cs:142
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
Static class that gives access to app-specific themed colors. All colors are fetched directly from th...
Definition: AppColors.cs:8
Holds navigation parameters specific to views displaying a list of contacts.
A page that displays a list of the current user's contacts.
Contact Information model, including related notification information.
bool CanToggleSubscription()
If toggle subscription can be performed on the contact.
ContactInfoModel(ContactInfo? Contact, params NotificationEvent[] Events)
Contact Information model, including related notification information.
CaseInsensitiveString? LegalId
Legal ID of contact.
bool? Owner
If the account is registered as the owner of the thing.
int NrEvents
Number of events associated with contact.
async Task ToggleSubscription()
Subscribes to an unsubscribed contact; unsubscribes from a subscribed one, with user permission.
void RemoveEvent(NotificationEvent Event)
Removes a notification event.
string UniqueName
Unique name used to compare items.
void PresenceUpdated()
Method called when presence for contact has been updated.
bool HasEvents
If the contact has associated events.
Color ConnectionColor
A color representing the current connection state of the contact.
ContactInfo? Contact
Contact Information object in database.
PropertyChangedEventHandler? PropertyChanged
Occurs when a property value changes.
ICommand ToggleSubscriptionCommand
Command to execute when user wants to toggle XMPP subcription.
void AddEvent(NotificationEvent Event)
Adds a notification event.
bool? AllowSubscriptionFrom
Allow subscriptions from this contact
void OnPropertyChanged(string PropertyName)
Called when a property has changed.
void NotificationsUpdated(NotificationEvent[] Events)
Method called when notifications for the item have been updated.
CaseInsensitiveString? BareJid
Bare JID of contact.
Color ConnectionSecondaryColor
Secondary Color representing the current connection state of the contact.
A page to display when the user wants to view an identity.
Asks the user if it wants to remove an existing presence subscription request as well.
Task< bool?> Result
Result will be provided here. If dialog is cancelled, null is returned.
Asks the user if it wants to remove an existing presence subscription request as well.
Task< bool?> Result
Result will be provided here. If dialog is cancelled, null is returned.
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
SubscriptionState State
roup Current subscription state.
Definition: RosterItem.cs:268
bool HasLastPresence
If the roster item has received presence from an online resource having the given bare JID.
Definition: RosterItem.cs:425
Represents a case-insensitive string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
Task GoToAsync(string Route)
Navigates to the specified route and pushes the page onto the navigation stack.
Task< bool > DisplayAlert(string Title, string Message, string? Accept=null, string? Cancel=null)
Displays an alert/message box to the user.
Definition: ImplTypes.g.cs:58
abstract class NotificationEvent()
Abstract base class of notification events.
BackMethod
Navigation Back Method
Definition: BackMethod.cs:7
SubscriptionState
State of a presence subscription.
Definition: RosterItem.cs:16