Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IsFriendViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using CommunityToolkit.Mvvm.Input;
8using System.Collections.ObjectModel;
13
15{
19 public partial class IsFriendViewModel : XmppViewModel
20 {
21 private readonly NotificationEvent? @event;
22 private readonly IsFriendNavigationArgs? navigationArguments;
23
29 : base()
30 {
31 this.navigationArguments = Args;
32
33 this.Tags = [];
34 this.CallerTags = [];
35 this.RuleRanges = [];
36
37 if (Args is not null)
38 {
39 this.@event = Args.Event;
40 this.BareJid = Args.BareJid;
41 this.FriendlyName = Args.FriendlyName;
42 this.RemoteJid = Args.RemoteJid;
43 this.RemoteFriendlyName = Args.RemoteFriendlyName;
44 this.Key = Args.Key;
45 this.ProvisioningService = Args.ProvisioningService;
46
47 if (this.FriendlyName == this.BareJid)
48 this.FriendlyName = ServiceRef.Localizer[nameof(AppResources.NotAvailable)];
49
50 this.RemoteFriendlyNameAvailable = this.RemoteFriendlyName != this.RemoteJid;
51 if (!this.RemoteFriendlyNameAvailable)
52 this.RemoteFriendlyName = ServiceRef.Localizer[nameof(AppResources.NotAvailable)];
53 }
54 }
55
57 protected override async Task OnInitialize()
58 {
59 await base.OnInitialize();
60
61 if (this.navigationArguments is not null)
62 {
63 this.Tags.Clear();
64 this.CallerTags.Clear();
65
66 ContactInfo Thing = await ContactInfo.FindByBareJid(this.BareJid ?? string.Empty);
67 if (Thing?.MetaData is not null)
68 {
69 foreach (Property Tag in Thing.MetaData)
70 this.Tags.Add(new HumanReadableTag(Tag));
71 }
72
73 ContactInfo Caller = await ContactInfo.FindByBareJid(this.RemoteJid ?? string.Empty);
74 this.CallerInContactsList = Caller is not null;
75 if (Caller?.MetaData is not null)
76 {
77 foreach (Property Tag in Caller.MetaData)
78 this.CallerTags.Add(new HumanReadableTag(Tag));
79 }
80
81 this.RuleRanges.Clear();
82 this.RuleRanges.Add(new RuleRangeModel(RuleRange.Caller, ServiceRef.Localizer[nameof(AppResources.CallerOnly)]));
83 this.RuleRanges.Add(new RuleRangeModel(RuleRange.Domain, ServiceRef.Localizer[nameof(AppResources.EntireDomain), XmppClient.GetDomain(this.RemoteJid)]));
84 this.RuleRanges.Add(new RuleRangeModel(RuleRange.All, ServiceRef.Localizer[nameof(AppResources.Everyone)]));
85
86 this.SelectedRuleRangeIndex = 0;
87 }
88 }
89
90 #region Properties
91
95 public ObservableCollection<HumanReadableTag> Tags { get; }
96
100 public ObservableCollection<HumanReadableTag> CallerTags { get; }
101
105 public ObservableCollection<RuleRangeModel> RuleRanges { get; }
106
110 [ObservableProperty]
111 private string? bareJid;
112
116 [ObservableProperty]
117 private string? friendlyName;
118
122 [ObservableProperty]
123 private string? remoteJid;
124
128 [ObservableProperty]
129 private string? remoteFriendlyName;
130
134 [ObservableProperty]
135 private bool remoteFriendlyNameAvailable;
136
140 [ObservableProperty]
141 private string? key;
142
146 [ObservableProperty]
147 private string? provisioningService;
148
152 [ObservableProperty]
153 private bool callerInContactsList;
154
158 [ObservableProperty]
159 private int selectedRuleRangeIndex;
160
161 #endregion
162
166 [RelayCommand]
167 private static Task Click(object obj)
168 {
169 if (obj is HumanReadableTag Tag)
170 return ViewClaimThingViewModel.LabelClicked(Tag.Name, Tag.Value, Tag.LocalizedValue);
171 else if (obj is string s)
172 return ViewClaimThingViewModel.LabelClicked(string.Empty, s, s);
173 else
174 return Task.CompletedTask;
175 }
176
180 [RelayCommand]
181 private async Task AddContact()
182 {
183 if (!this.CallerInContactsList)
184 {
185 ContactInfo Info = new()
186 {
187 BareJid = this.RemoteJid,
188 FriendlyName = (this.RemoteFriendlyNameAvailable ? this.RemoteFriendlyName : this.RemoteJid) ?? string.Empty
189 };
190
191 await Database.Insert(Info);
192
193 this.CallerInContactsList = true;
194 }
195 }
196
200 [RelayCommand]
201 private async Task RemoveContact()
202 {
203 if (this.CallerInContactsList)
204 {
205 ContactInfo Info = await ContactInfo.FindByBareJid(this.RemoteJid ?? string.Empty);
206 if (Info is not null)
207 await Database.Delete(Info);
208
209 this.CallerInContactsList = false;
210 }
211 }
212
213 private RuleRange GetRuleRange()
214 {
215 return this.SelectedRuleRangeIndex switch
216 {
217 1 => RuleRange.Domain,
218 2 => RuleRange.All,
219 _ => RuleRange.Caller,
220 };
221 }
222
226 [RelayCommand]
227 private void Accept()
228 {
229 this.Respond(true);
230 }
231
235 [RelayCommand]
236 private void Reject()
237 {
238 this.Respond(false);
239 }
240
241 private void Respond(bool Accepts)
242 {
243 RuleRange Range = this.GetRuleRange();
244 FriendshipResolver Resolver = new(this.BareJid ?? string.Empty, this.RemoteJid ?? string.Empty, Range);
245
246 ServiceRef.XmppService.IsFriendResponse(this.ProvisioningService ?? ServiceRef.TagProfile.ProvisioningJid ?? string.Empty,
247 this.BareJid ?? string.Empty, this.RemoteJid ?? string.Empty, this.Key ?? string.Empty, Accepts, Range,
248 this.ResponseHandler, Resolver);
249 }
250
251 private async Task ResponseHandler(object? Sender, IqResultEventArgs e)
252 {
253 if (e.Ok)
254 {
255 if (this.@event is not null)
257
259
260 MainThread.BeginInvokeOnMainThread(async () =>
261 {
262 await this.GoBack();
263 });
264 }
265 else
266 {
267 MainThread.BeginInvokeOnMainThread(async () => await ServiceRef.UiService.DisplayException(e.StanzaError ??
268 new Exception(ServiceRef.Localizer[nameof(AppResources.UnableToRespond)])));
269 }
270 }
271
275 [RelayCommand]
276 private async Task Ignore()
277 {
278 if (this.@event is not null)
280
281 await this.GoBack();
282 }
283
284 }
285}
Contains information about a contact.
Definition: ContactInfo.cs:21
Property?[] MetaData
Meta-data related to a thing.
Definition: ContactInfo.cs:210
static Task< ContactInfo > FindByBareJid(string BareJid)
Finds information about a contact, given its Bare JID.
Definition: ContactInfo.cs:220
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
static INotificationService NotificationService
Service for managing notifications for the user.
Definition: ServiceRef.cs:211
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
virtual async Task GoBack()
Method called when user wants to navigate to the previous screen.
Class used to present a meta-data tag in a human interface.
Holds navigation parameters specific to displaying the is-friend provisioning question.
The view model to bind to when displaying a thing.
IsFriendViewModel(IsFriendNavigationArgs? Args)
Creates an instance of the IsFriendViewModel class.
override async Task OnInitialize()
Method called when view is initialized for the first time. Use this method to implement registration ...
ObservableCollection< RuleRangeModel > RuleRanges
Available Rule Ranges
ObservableCollection< HumanReadableTag > CallerTags
Holds a list of meta-data tags associated with the caller.
ObservableCollection< HumanReadableTag > Tags
Holds a list of meta-data tags associated with a thing.
ProvisioningNotificationEvent? Event
Notification event object.
string? RemoteJid
Bare JID of remote entity trying to connect to device.
The view model to bind to for when displaying thing claim information.
static async Task LabelClicked(string Name, string Value, string LocalizedValue)
Processes the click of a localized meta-data label.
A view model that holds the XMPP state.
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
static string GetDomain(string JID)
Gets the domain part of a JID.
Definition: XmppClient.cs:6929
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Delete(object Object)
Deletes an object in the database.
Definition: Database.cs:717
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
Interface for event resolvers. Such can be used to resolve multiple pending notifications at once.
Task DeleteEvents(NotificationEventType Type, CaseInsensitiveString Category)
Deletes events for a given button and category.
Task DeleteResolvedEvents(IEventResolver Resolver)
Deletes pending events that have already been resolved.
abstract class NotificationEvent()
Abstract base class of notification events.
class RuleRangeModel(object RuleRange, string Label)
Rule Range model
RuleRange
Range of a rule change
Definition: RuleRange.cs:7