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;
14
16{
20 public partial class IsFriendViewModel : XmppViewModel
21 {
22 private readonly NotificationEvent? @event;
23 private readonly IsFriendNavigationArgs? navigationArguments;
24
30 : base()
31 {
32 this.navigationArguments = Args;
33
34 this.Tags = [];
35 this.CallerTags = [];
36 this.RuleRanges = [];
37
38 if (Args is not null)
39 {
40 this.@event = Args.Event;
41 this.BareJid = Args.BareJid;
42 this.FriendlyName = Args.FriendlyName;
43 this.RemoteJid = Args.RemoteJid;
44 this.RemoteFriendlyName = Args.RemoteFriendlyName;
45 this.Key = Args.Key;
46 this.ProvisioningService = Args.ProvisioningService;
47
48 if (this.FriendlyName == this.BareJid)
49 this.FriendlyName = ServiceRef.Localizer[nameof(AppResources.NotAvailable)];
50
51 this.RemoteFriendlyNameAvailable = this.RemoteFriendlyName != this.RemoteJid;
52 if (!this.RemoteFriendlyNameAvailable)
53 this.RemoteFriendlyName = ServiceRef.Localizer[nameof(AppResources.NotAvailable)];
54 }
55 }
56
58 public override async Task OnInitializeAsync()
59 {
60 await base.OnInitializeAsync();
61
62 if (this.navigationArguments is not null)
63 {
64 this.Tags.Clear();
65 this.CallerTags.Clear();
66
67 ContactInfo Thing = await ContactInfo.FindByBareJid(this.BareJid ?? string.Empty);
68 if (Thing?.MetaData is not null)
69 {
70 foreach (Property Tag in Thing.MetaData)
71 this.Tags.Add(new HumanReadableTag(Tag));
72 }
73
74 ContactInfo Caller = await ContactInfo.FindByBareJid(this.RemoteJid ?? string.Empty);
75 this.CallerInContactsList = Caller is not null;
76 if (Caller?.MetaData is not null)
77 {
78 foreach (Property Tag in Caller.MetaData)
79 this.CallerTags.Add(new HumanReadableTag(Tag));
80 }
81
82 this.RuleRanges.Clear();
86
87 this.SelectedRuleRangeIndex = 0;
88 }
89 }
90
91 #region Properties
92
96 public ObservableCollection<HumanReadableTag> Tags { get; }
97
101 public ObservableCollection<HumanReadableTag> CallerTags { get; }
102
106 public ObservableCollection<RuleRangeModel> RuleRanges { get; }
107
111 [ObservableProperty]
112 private string? bareJid;
113
117 [ObservableProperty]
118 private string? friendlyName;
119
123 [ObservableProperty]
124 private string? remoteJid;
125
129 [ObservableProperty]
130 private string? remoteFriendlyName;
131
135 [ObservableProperty]
136 private bool remoteFriendlyNameAvailable;
137
141 [ObservableProperty]
142 private string? key;
143
147 [ObservableProperty]
148 private string? provisioningService;
149
153 [ObservableProperty]
154 private bool callerInContactsList;
155
159 [ObservableProperty]
160 private int selectedRuleRangeIndex;
161
162 #endregion
163
167 [RelayCommand]
168 private static Task Click(object obj)
169 {
170 if (obj is HumanReadableTag Tag)
171 return ViewClaimThingViewModel.LabelClicked(Tag.Name, Tag.Value, Tag.LocalizedValue);
172 else if (obj is string s)
173 return ViewClaimThingViewModel.LabelClicked(string.Empty, s, s);
174 else
175 return Task.CompletedTask;
176 }
177
181 [RelayCommand]
182 private async Task AddContact()
183 {
184 if (!this.CallerInContactsList)
185 {
186 ContactInfo Info = new()
187 {
188 BareJid = this.RemoteJid,
189 FriendlyName = (this.RemoteFriendlyNameAvailable ? this.RemoteFriendlyName : this.RemoteJid) ?? string.Empty
190 };
191
192 await Database.Insert(Info);
193
194 this.CallerInContactsList = true;
195 }
196 }
197
201 [RelayCommand]
202 private async Task RemoveContact()
203 {
204 if (this.CallerInContactsList)
205 {
206 ContactInfo Info = await ContactInfo.FindByBareJid(this.RemoteJid ?? string.Empty);
207 if (Info is not null)
208 await Database.Delete(Info);
209
210 this.CallerInContactsList = false;
211 }
212 }
213
214 private RuleRange GetRuleRange()
215 {
216 return this.SelectedRuleRangeIndex switch
217 {
218 1 => RuleRange.Domain,
219 2 => RuleRange.All,
220 _ => RuleRange.Caller,
221 };
222 }
223
227 [RelayCommand]
228 private void Accept()
229 {
230 this.Respond(true);
231 }
232
236 [RelayCommand]
237 private void Reject()
238 {
239 this.Respond(false);
240 }
241
242 private void Respond(bool Accepts)
243 {
244 RuleRange Range = this.GetRuleRange();
245 FriendshipResolver Resolver = new(this.BareJid ?? string.Empty, this.RemoteJid ?? string.Empty, Range);
246
247 ServiceRef.XmppService.IsFriendResponse(this.ProvisioningService ?? ServiceRef.TagProfile.ProvisioningJid ?? string.Empty,
248 this.BareJid ?? string.Empty, this.RemoteJid ?? string.Empty, this.Key ?? string.Empty, Accepts, Range,
249 this.ResponseHandler, Resolver);
250 }
251
252 private async Task ResponseHandler(object? Sender, IqResultEventArgs e)
253 {
254 if (e.Ok)
255 {
256 if (this.@event is not null)
258
260
261 MainThread.BeginInvokeOnMainThread(async () =>
262 {
263 await this.GoBack();
264 });
265 }
266 else
267 {
268 MainThread.BeginInvokeOnMainThread(async () => await ServiceRef.UiService.DisplayException(e.StanzaError ??
269 new Exception(ServiceRef.Localizer[nameof(AppResources.UnableToRespond)])));
270 }
271 }
272
276 [RelayCommand]
277 private async Task Ignore()
278 {
279 if (this.@event is not null)
281
282 await this.GoBack();
283 }
284
285 }
286}
A strongly-typed resource class, for looking up localized strings, etc.
static string NotAvailable
Looks up a localized string similar to Not Available.
static string UnableToRespond
Looks up a localized string similar to Unable to respond to request..
static string CallerOnly
Looks up a localized string similar to Caller Only.
static string EntireDomain
Looks up a localized string similar to Everyone from {0}.
static string Everyone
Looks up a localized string similar to Everyone.
Contains information about a contact.
Definition: ContactInfo.cs:22
Property?[] MetaData
Meta-data related to a thing.
Definition: ContactInfo.cs:211
static Task< ContactInfo > FindByBareJid(string BareJid)
Finds information about a contact, given its Bare JID.
Definition: ContactInfo.cs:221
Base class that references services in the app.
Definition: ServiceRef.cs:43
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
static INotificationService NotificationService
Service for managing notifications for the user.
Definition: ServiceRef.cs:334
static ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:202
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:190
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 OnInitializeAsync()
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.
Event arguments for responses to IQ queries.
bool Ok
If the response is an OK result response (true), or an error response (false).
object State
State object passed to the original request.
XmppException StanzaError
Any stanza error returned.
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:58
static string GetDomain(string JID)
Gets the domain part of a JID.
Definition: XmppClient.cs:6986
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Delete(object Object)
Deletes an object in the database.
Definition: Database.cs:1291
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
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.
Definition: ImplTypes.g.cs:58
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