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 Mopups.Services;
14
16{
20 public class ContactInfoModel : INotifyPropertyChanged, IUniqueItem
21 {
22 private readonly ContactInfo? contact;
23 private NotificationEvent[] events;
24
25 private readonly ICommand toggleSubscriptionCommand;
26
33 {
34 this.contact = Contact;
35 this.events = Events;
36
37 this.toggleSubscriptionCommand = new Command(async () => await this.ToggleSubscription(), () => this.CanToggleSubscription());
38 }
39
41 public string UniqueName => this.contact?.ThingNotificationCategoryKey ?? string.Empty;
42
46 public ContactInfo? Contact => this.contact;
47
51 public CaseInsensitiveString? BareJid => this.contact?.BareJid;
52
56 public CaseInsensitiveString? LegalId => this.contact?.LegalId;
57
61 public LegalIdentity? LegalIdentity => this.contact?.LegalIdentity;
62
66 public string? FriendlyName => this.contact?.FriendlyName;
67
71 public string? SourceId => this.contact?.SourceId;
72
76 public string? Partition => this.contact?.Partition;
77
81 public string? NodeId => this.contact?.NodeId;
82
86 public CaseInsensitiveString? RegistryJid => this.contact?.RegistryJid;
87
91 public bool? SubcribeTo => this.contact?.SubscribeTo;
92
96 public bool? AllowSubscriptionFrom => this.contact?.AllowSubscriptionFrom;
97
101 public bool? IsThing => this.contact?.IsThing;
102
106 public bool? Owner => this.contact?.Owner;
107
111 public Property[]? MetaData => this.contact?.MetaData;
112
116 public NotificationEvent[] Events => this.events;
117
121 public bool HasEvents => this.events is not null && this.events.Length > 0;
122
126 public int NrEvents => this.events?.Length ?? 0;
127
131 public Color ConnectionColor
132 {
133 get
134 {
135 if (string.IsNullOrEmpty(this.contact?.BareJid))
136 return Colors.Transparent;
137
138 RosterItem? Item = null;
139 try
140 {
141 Item = ServiceRef.XmppService.GetRosterItem(this.contact?.BareJid);
142 }
143 catch (Exception)
144 {
145 return Colors.Transparent;
146 }
147 if (Item is null)
148 return Colors.Transparent;
149
150 if (Item.State != SubscriptionState.To && Item.State != SubscriptionState.Both)
151 return Colors.Transparent;
152
153 if (!Item.HasLastPresence)
154 return Colors.LightSalmon;
155
156 return Item.LastPresence.Availability switch
157 {
158 Availability.Online or Availability.Chat => Colors.LightGreen,
159 Availability.Away or Availability.ExtendedAway => Colors.LightYellow,
160 _ => Colors.LightSalmon,
161 };
162 }
163 }
164
168 public ICommand ToggleSubscriptionCommand => this.toggleSubscriptionCommand;
169
175 {
176 return !string.IsNullOrEmpty(this.contact?.BareJid);
177 }
178
182 public async Task ToggleSubscription()
183 {
184 if (string.IsNullOrEmpty(this.contact?.BareJid))
185 return;
186
187 RosterItem? Item = null;
188 try
189 {
190 Item = ServiceRef.XmppService.GetRosterItem(this.contact?.BareJid);
191 }
192 catch
193 {
194 await ServiceRef.UiService.DisplayAlert(ServiceRef.Localizer[nameof(AppResources.Error)], ServiceRef.Localizer[nameof(AppResources.NetworkSeemsToBeMissing)], ServiceRef.Localizer[nameof(AppResources.Ok)]);
195 return;
196 }
197
198 bool Subscribed;
199
200 if (Item is null)
201 Subscribed = false;
202 else
203 Subscribed = Item.State == SubscriptionState.To || Item.State == SubscriptionState.Both;
204
205 if (Subscribed)
206 {
207 if (!await ServiceRef.UiService.DisplayAlert(ServiceRef.Localizer[nameof(AppResources.Question)],
208 ServiceRef.Localizer[nameof(AppResources.RemoveSubscriptionFrom), this.FriendlyName ?? string.Empty],
209 ServiceRef.Localizer[nameof(AppResources.Yes)], ServiceRef.Localizer[nameof(AppResources.Cancel)]))
210 {
211 return;
212 }
213
214 ServiceRef.XmppService.RequestPresenceUnsubscription(this.BareJid);
215
216 if (Item is not null && (Item.State == SubscriptionState.From || Item.State == SubscriptionState.Both))
217 {
218 RemoveSubscriptionViewModel ViewModel = new(this.BareJid);
219 RemoveSubscriptionPopup Page = new(ViewModel);
220
221 await MopupService.Instance.PushAsync(Page);
222 bool? Remove = await ViewModel.Result;
223
224 if (Remove.HasValue && Remove.Value)
225 {
226 ServiceRef.XmppService.RequestRevokePresenceSubscription(this.BareJid);
227
228 if ((this.contact?.AllowSubscriptionFrom.HasValue ?? false) && this.contact.AllowSubscriptionFrom.Value)
229 {
230 this.contact.AllowSubscriptionFrom = null;
231 await Database.Update(this.contact);
232 }
233 }
234 }
235 }
236 else
237 {
238 SubscribeToViewModel ViewModel = new(this.BareJid);
239 SubscribeToPopup Page = new(ViewModel);
240
241 await MopupService.Instance.PushAsync(Page);
242 bool? SubscribeTo = await ViewModel.Result;
243
244 if (SubscribeTo.HasValue && SubscribeTo.Value)
245 {
246 string IdXml;
247
248 if (ServiceRef.TagProfile.LegalIdentity is null)
249 IdXml = string.Empty;
250 else
251 {
252 StringBuilder Xml = new();
253 ServiceRef.TagProfile.LegalIdentity.Serialize(Xml, true, true, true, true, true, true, true);
254 IdXml = Xml.ToString();
255 }
256
257 ServiceRef.XmppService.RequestPresenceSubscription(this.BareJid, IdXml);
258 }
259 }
260 }
261
265 public void PresenceUpdated()
266 {
267 this.OnPropertyChanged(nameof(this.ConnectionColor));
268 }
269
274 public void OnPropertyChanged(string PropertyName)
275 {
276 try
277 {
278 this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
279 }
280 catch (Exception ex)
281 {
282 ServiceRef.LogService.LogException(ex);
283 }
284 }
285
289 public event PropertyChangedEventHandler? PropertyChanged;
290
296 {
297 this.events = Events;
298
299 this.OnPropertyChanged(nameof(this.Events));
300 this.OnPropertyChanged(nameof(this.HasEvents));
301 this.OnPropertyChanged(nameof(this.NrEvents));
302 }
303
308 public void AddEvent(NotificationEvent Event)
309 {
310 if (this.events is null)
311 this.NotificationsUpdated([Event]);
312 else
313 {
314 foreach (NotificationEvent Event2 in this.events)
315 {
316 if (Event2.ObjectId == Event.ObjectId)
317 return;
318 }
319
320 int c = this.events.Length;
321 NotificationEvent[] NewArray = new NotificationEvent[c + 1];
322 Array.Copy(this.events, 0, NewArray, 0, c);
323 NewArray[c] = Event;
324
325 this.NotificationsUpdated(NewArray);
326 }
327 }
328
334 {
335 if (this.events is not null)
336 {
337 int i, c = this.events.Length;
338
339 for (i = 0; i < c; i++)
340 {
341 NotificationEvent Event2 = this.events[i];
342
343 if (Event2.ObjectId == Event.ObjectId)
344 {
345 NotificationEvent[] NewArray = new NotificationEvent[c - 1];
346
347 if (i > 0)
348 Array.Copy(this.events, 0, NewArray, 0, i);
349
350 if (i < c - 1)
351 Array.Copy(this.events, i + 1, NewArray, i, c - i - 1);
352
353 this.NotificationsUpdated(NewArray);
354
355 return;
356 }
357 }
358
359 }
360 }
361 }
362}
Contains information about a contact.
Definition: ContactInfo.cs:21
Base class that references services in the app.
Definition: ServiceRef.cs:31
static ILogService LogService
Log service.
Definition: ServiceRef.cs:91
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
static ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:79
static IStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:235
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:67
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.
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:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
abstract class NotificationEvent()
Abstract base class of notification events.
SubscriptionState
State of a presence subscription.
Definition: RosterItem.cs:16