Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TransactionPopupViewModel.cs
1using System;
2using System.Threading.Tasks;
3using CommunityToolkit.Mvvm.ComponentModel;
4using CommunityToolkit.Mvvm.Input;
11using NeuroAccessMaui.Services.Contacts; // Added for ContactInfo lookup
14using System.Globalization; // Added for identity navigation
15
17{
18 public partial class TransactionPopupViewModel(TransactionEventItem Event) : BasePopupViewModel
19 {
20 [ObservableProperty]
21 private TransactionEventItem transactionEevent = Event;
22
23 [ObservableProperty]
24 private bool isContact;
25
26 public decimal Change => this.TransactionEevent.Change < 0 ? -this.TransactionEevent.Change : this.TransactionEevent.Change;
27
28 public bool IsSender => this.TransactionEevent.Change < 0;
29
30 public bool HasMessage => this.TransactionEevent.HasMessage;
31
32 public bool MessageIsUrl => this.HasMessage && Uri.IsWellFormedUriString(this.TransactionEevent.Message, UriKind.Absolute);
33
34 public override async Task OnInitializeAsync()
35 {
36 await base.OnInitializeAsync();
37
38 await this.EvaluateIsContact();
39
40 this.OnPropertyChanged(nameof(this.IsContact));
41 }
42
43 public string? Message
44 {
45 get
46 {
47 if (this.HasMessage)
48 return this.TransactionEevent.Message;
49 else
50 return null;
51 }
52 }
53
54 public string DestinationString
55 {
56 get
57 {
58 if (this.IsSender)
59 return ServiceRef.Localizer[nameof(AppResources.To)];
60 else
61 return ServiceRef.Localizer[nameof(AppResources.From)];
62 }
63 }
64
65 #region Commands
66
67 [RelayCommand]
68 private async Task Close()
69 {
70 await ServiceRef.PopupService.PopAsync();
71 }
72
73 [RelayCommand]
74 private async Task OpenChat()
75 {
76 try
77 {
78 string Remote = this.TransactionEevent.Remote;
79 if (string.IsNullOrEmpty(Remote))
80 return;
81
82 string? BareJid = null;
83 string? LegalId = null;
84 int AtIndex = Remote.IndexOf('@');
85 if (AtIndex > 0)
86 {
87 BareJid = Remote;
88 string AccountPart = Remote.Substring(0, AtIndex);
89 if (Guid.TryParse(AccountPart, out Guid _))
90 LegalId = AccountPart;
91 }
92
93 ChatNavigationArgs Args = new(LegalId, BareJid ?? Remote, this.TransactionEevent.FriendlyName);
94 await ServiceRef.NavigationService.GoToAsync(nameof(ChatPage), Args);
95 await ServiceRef.PopupService.PopAsync();
96 }
97 catch (Exception Ex)
98 {
99 ServiceRef.LogService.LogException(Ex);
100 }
101 }
102
106 [RelayCommand]
107 private async Task ViewId()
108 {
109 try
110 {
111 string Remote = this.TransactionEevent.Remote;
112 if (string.IsNullOrEmpty(Remote))
113 return;
114
115 ContactInfo? Contact = await ContactInfo.FindByBareJid(Remote);
116 Contact ??= await ContactInfo.FindByLegalId(Remote);
117
118 if (Contact is null)
119 return;
120
121 LegalIdentity? Identity = Contact.LegalIdentity;
122 if (Identity is not null)
123 {
124 ViewIdentityNavigationArgs ViewIdentityArgs = new(Identity);
125 await ServiceRef.NavigationService.GoToAsync(nameof(ViewIdentityPage), ViewIdentityArgs);
126 await ServiceRef.PopupService.PopAsync();
127 }
128 else
129 {
130 string LegalId = Contact.LegalId;
131 if (!string.IsNullOrEmpty(LegalId))
132 {
133 await ServiceRef.ContractOrchestratorService.OpenLegalIdentity(LegalId, ServiceRef.Localizer[nameof(AppResources.ScannedQrCode)]);
134 await ServiceRef.PopupService.PopAsync();
135 }
136 }
137 }
138 catch (Exception Ex)
139 {
140 ServiceRef.LogService.LogException(Ex);
141 }
142 }
143
144 protected override Task OnPopAsync()
145 {
146 return base.OnPopAsync();
147 }
148
149 [RelayCommand]
150 private async Task OpenMessageUri()
151 {
152 if (this.MessageIsUrl && this.Message is not null)
153 {
154 try
155 {
156 await App.OpenUrlAsync(this.Message);
157 await ServiceRef.PopupService.PopAsync();
158 }
159 catch (Exception Ex)
160 {
161 ServiceRef.LogService.LogException(Ex);
162 }
163 }
164 }
165 #endregion
166
167 private async Task EvaluateIsContact()
168 {
169 string Remote = this.TransactionEevent.Remote;
170 if (string.IsNullOrEmpty(Remote))
171 {
172 this.IsContact = false;
173 return;
174 }
175
176 // Count as contact if self
177 if (string.Equals(Remote, ServiceRef.XmppService.BareJid, StringComparison.OrdinalIgnoreCase) || string.Equals(Remote, ServiceRef.TagProfile.LegalJid, StringComparison.OrdinalIgnoreCase))
178 {
179 this.IsContact = true;
180 return;
181 }
182
183 try
184 {
185 ContactInfo? Contact2 = await ContactInfo.FindByLegalId(Remote);
186 if (Contact2 is not null)
187 {
188 this.IsContact = true;
189 return;
190 }
191
192 ContactInfo Contact = await ContactInfo.FindByBareJid(Remote);
193 if (Contact is not null)
194 {
195 this.IsContact = true;
196 return;
197 }
198
199 RosterItem? Item = ServiceRef.XmppService.GetRosterItem(Remote);
200 if (Item is not null)
201 {
202 this.IsContact = true;
203 return;
204 }
205 }
206 catch
207 {
208 // Do nothing
209 }
210 }
211 }
212}
Represents an instance of the Neuro-Access app.
Definition: App.xaml.cs:125
A strongly-typed resource class, for looking up localized strings, etc.
static string From
Looks up a localized string similar to From.
static string ScannedQrCode
Looks up a localized string similar to Scanned QR Code.
static string To
Looks up a localized string similar to To.
Contains information about a contact.
Definition: ContactInfo.cs:22
static async Task< ContactInfo?> FindByLegalId(string LegalId)
Finds information about a contact, given its Legal ID.
Definition: ContactInfo.cs:248
LegalIdentity? LegalIdentity
Legal Identity object.
Definition: ContactInfo.cs:82
static Task< ContactInfo > FindByBareJid(string BareJid)
Finds information about a contact, given its Bare JID.
Definition: ContactInfo.cs:221
CaseInsensitiveString LegalId
Legal ID of contact.
Definition: ContactInfo.cs:72
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
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
Holds navigation parameters specific to views displaying a list of contacts.
A page that displays a list of the current user's contacts.
A page to display when the user wants to view an identity.
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
Definition: ImplTypes.g.cs:58
partial class TransactionEventItem(AccountEventModel accountEvent, string friendlyName, string currency, string? transactionType)
Presentation model for a transaction event in history (no notification support).