Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RequestPaymentViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using CommunityToolkit.Mvvm.Input;
9using System.ComponentModel;
10using System.Globalization;
11using System.Text;
12using Waher.Content;
13
15{
20 {
21 private readonly RequestPaymentPage page;
22
29 : base()
30 {
31 this.page = Page;
32
33 this.Currency = Args?.Balance?.Currency;
34
35 this.Amount = 0;
36 this.AmountText = string.Empty;
37 this.AmountOk = false;
38
39 this.AmountExtra = 0;
40 this.AmountExtraText = string.Empty;
41 this.AmountExtraOk = false;
42
43 this.RemoveQrCode();
44 }
45
46 #region Properties
47
51 [ObservableProperty]
52 private decimal amount;
53
57 [ObservableProperty]
58 private bool amountOk;
59
63 [ObservableProperty]
64 private string? amountText;
65
66 protected override void OnPropertyChanged(PropertyChangedEventArgs e)
67 {
68 base.OnPropertyChanged(e);
69
70 switch (e.PropertyName)
71 {
72 case nameof(this.AmountText):
73 if (CommonTypes.TryParse(this.AmountText, out decimal d) && d > 0)
74 {
75 this.Amount = d;
76 this.AmountOk = true;
77 }
78 else
79 this.AmountOk = false;
80 break;
81
82 case nameof(this.AmountExtraText):
83 if (string.IsNullOrEmpty(this.AmountExtraText))
84 {
85 this.AmountExtra = null;
86 this.AmountExtraOk = true;
87 }
88 else if (CommonTypes.TryParse(this.AmountExtraText, out d) && d >= 0)
89 {
90 this.AmountExtra = d;
91 this.AmountExtraOk = true;
92 }
93 else
94 this.AmountExtraOk = false;
95 break;
96 }
97 }
98
102 [ObservableProperty]
103 private decimal? amountExtra;
104
108 [ObservableProperty]
109 private bool amountExtraOk;
110
114 [ObservableProperty]
115 private string? amountExtraText;
116
120 [ObservableProperty]
121 private string? currency;
122
126 [ObservableProperty]
127 private string? message;
128
132 [ObservableProperty]
133 private bool encryptMessage;
134
135 #endregion
136
140 [RelayCommand(CanExecute = nameof(AmountOk))]
141 private Task GenerateQrCode()
142 {
143 string Uri;
144
145 if (this.EncryptMessage && ServiceRef.TagProfile.LegalIdentity is not null)
146 {
147 Uri = ServiceRef.XmppService.CreateIncompleteEDalerPayMeUri(ServiceRef.TagProfile.LegalIdentity, this.Amount, this.AmountExtra,
148 this.Currency ?? string.Empty, this.Message ?? string.Empty);
149 }
150 else
151 {
152 Uri = ServiceRef.XmppService.CreateIncompleteEDalerPayMeUri(ServiceRef.XmppService.BareJid, this.Amount, this.AmountExtra,
153 this.Currency ?? string.Empty, this.Message ?? string.Empty);
154 }
155
156 if (this.IsAppearing)
157 {
158 MainThread.BeginInvokeOnMainThread(async () =>
159 {
160 this.GenerateQrCode(Uri);
161
162 await this.page.ShowQrCode();
163 });
164 }
165
166 return Task.CompletedTask;
167 }
168
172 [RelayCommand(CanExecute = nameof(HasQrCode))]
173 private async Task ShareContact()
174 {
175 try
176 {
177 TaskCompletionSource<ContactInfoModel?> Selected = new();
178 ContactListNavigationArgs ContactListArgs = new(ServiceRef.Localizer[nameof(AppResources.SelectFromWhomToRequestPayment)], Selected)
179 {
180 CanScanQrCode = true
181 };
182
183 await ServiceRef.UiService.GoToAsync(nameof(MyContactsPage), ContactListArgs, BackMethod.Pop);
184
185 ContactInfoModel? Contact = await Selected.Task;
186 if (Contact is null)
187 return;
188
189 if (string.IsNullOrEmpty(Contact.BareJid))
190 {
191 await ServiceRef.UiService.DisplayAlert(ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
192 ServiceRef.Localizer[nameof(AppResources.NetworkAddressOfContactUnknown)]);
193 return;
194 }
195
196 StringBuilder Markdown = new();
197
198 Markdown.Append("![");
199 Markdown.Append(ServiceRef.Localizer[nameof(AppResources.RequestPayment)]);
200 Markdown.Append("](");
201 Markdown.Append(this.QrCodeUri);
202 Markdown.Append(')');
203
204 await ChatViewModel.ExecuteSendMessage(string.Empty, Markdown.ToString(), Contact.BareJid);
205
206 if (Contact.Contact is not null)
207 {
208 await Task.Delay(100); // Otherwise, page doesn't show properly. (Underlying timing issue. TODO: Find better solution.)
209
210 ChatNavigationArgs ChatArgs = new(Contact.Contact);
211 await ServiceRef.UiService.GoToAsync(nameof(ChatPage), ChatArgs, BackMethod.Inherited, Contact.BareJid);
212 }
213 }
214 catch (Exception ex)
215 {
216 ServiceRef.LogService.LogException(ex);
218 }
219 }
220
224 [RelayCommand(CanExecute = nameof(HasQrCode))]
225 private async Task ShareExternal()
226 {
227 if (this.QrCodeBin is null)
228 return;
229
230 try
231 {
232 string? Message = this.Message;
233 if (string.IsNullOrEmpty(Message))
234 Message = ServiceRef.Localizer[nameof(AppResources.RequestPaymentMessage)];
235
236 ServiceRef.PlatformSpecific.ShareImage(this.QrCodeBin, string.Format(CultureInfo.CurrentCulture, Message, this.Amount, this.Currency),
237 ServiceRef.Localizer[nameof(AppResources.Share)], "RequestPayment.png");
238 }
239 catch (Exception ex)
240 {
241 ServiceRef.LogService.LogException(ex);
243 }
244 }
245
250 [RelayCommand]
251 public async Task OpenCalculator(object Parameter)
252 {
253 try
254 {
255 switch (Parameter?.ToString())
256 {
257 case "AmountText":
258 CalculatorNavigationArgs AmountArgs = new(this, nameof(this.AmountText));
259
260 await ServiceRef.UiService.GoToAsync(nameof(CalculatorPage), AmountArgs, BackMethod.Pop);
261 break;
262
263 case "AmountExtraText":
264 CalculatorNavigationArgs ExtraArgs = new(this, nameof(this.AmountExtraText));
265
266 await ServiceRef.UiService.GoToAsync(nameof(CalculatorPage), ExtraArgs, BackMethod.Pop);
267 break;
268 }
269 }
270 catch (Exception ex)
271 {
273 }
274 }
275
276 #region ILinkableView
277
281 public override Task<string> Title => Task.FromResult<string>(ServiceRef.Localizer[nameof(AppResources.RequestPayment)]);
282
283 #endregion
284
285
286 }
287}
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
bool IsAppearing
Returns true if the view model is shown.
Holds navigation parameters specific to views displaying a list of contacts.
A page that displays a list of the current user's contacts.
The view model to bind to when displaying the list of contacts.
Contact Information model, including related notification information.
ContactInfo? Contact
Contact Information object in database.
CaseInsensitiveString? BareJid
Bare JID of contact.
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 that allows the user to calculate the value of a numerical input field.
A view model that holds the XMPP state.
void RemoveQrCode()
Removes the QR-code
Holds navigation parameters specific to an eDaler balance event.
A page that displays information about eDaler received.
async Task OpenCalculator(object Parameter)
Opens the calculator for calculating the value of a numerical property.
RequestPaymentViewModel(RequestPaymentPage Page, EDalerBalanceNavigationArgs? Args)
The view model to bind to for requesting a payment.
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Task DisplayException(Exception Exception, string? Title=null)
Displays an alert/message box to the user.
Task GoToAsync(string Route, BackMethod BackMethod=BackMethod.Inherited, string? UniqueId=null)
Navigates the AppShell to the specified route, with page arguments to match.
Task< bool > DisplayAlert(string Title, string Message, string? Accept=null, string? Cancel=null)
Displays an alert/message box to the user.
BackMethod
Navigation Back Method
Definition: BackMethod.cs:7
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.