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 public bool IsMerchant => ServiceRef.TagProfile?.IsMerchant ?? false;
136
137 #endregion
138
142 [RelayCommand]
143 private Task GenerateQrCode()
144 {
145 string Uri;
146
147 if (this.EncryptMessage && ServiceRef.TagProfile.LegalIdentity is not null)
148 {
149 Uri = ServiceRef.XmppService.CreateIncompleteEDalerPayMeUri(ServiceRef.TagProfile.LegalIdentity, this.Amount, this.AmountExtra,
150 this.Currency ?? string.Empty, this.Message ?? string.Empty);
151 }
152 else
153 {
154 Uri = ServiceRef.XmppService.CreateIncompleteEDalerPayMeUri(ServiceRef.XmppService.BareJid, this.Amount, this.AmountExtra,
155 this.Currency ?? string.Empty, this.Message ?? string.Empty);
156 }
157
158 MainThread.BeginInvokeOnMainThread(async () =>
159 {
160 this.GenerateQrCode(Uri);
161
162 await this.page.ShowQrCode();
163
164 this.HasQrCode = true;
165 });
166
167 return Task.CompletedTask;
168 }
169
173 [RelayCommand]
174 private async Task ShareContact()
175 {
176 try
177 {
178 TaskCompletionSource<ContactInfoModel?> Selected = new();
180 {
181 CanScanQrCode = true
182 };
183
184 await ServiceRef.NavigationService.GoToAsync(nameof(MyContactsPage), ContactListArgs, BackMethod.Pop);
185
186 ContactInfoModel? Contact = await Selected.Task;
187 if (Contact is null)
188 return;
189
190 if (string.IsNullOrEmpty(Contact.BareJid))
191 {
194 return;
195 }
196
197 StringBuilder Markdown = new();
198
199 Markdown.Append("![");
200 Markdown.Append(ServiceRef.Localizer[nameof(AppResources.RequestPayment)]);
201 Markdown.Append("](");
202 Markdown.Append(this.QrCodeUri);
203 Markdown.Append(')');
204
205 await ChatViewModel.ExecuteSendMessage(string.Empty, Markdown.ToString(), Contact.BareJid);
206
207 if (Contact.Contact is not null)
208 {
209 await Task.Delay(100); // Otherwise, page doesn't show properly. (Underlying timing issue. TODO: Find better solution.)
210
211 ChatNavigationArgs ChatArgs = new(Contact.Contact);
212 await ServiceRef.NavigationService.GoToAsync(nameof(ChatPage), ChatArgs, BackMethod.Inherited, Contact.BareJid);
213 }
214 }
215 catch (Exception ex)
216 {
217 ServiceRef.LogService.LogException(ex);
219 }
220 }
221
225 [RelayCommand]
226 private async Task ShareExternal()
227 {
228 if (this.QrCodeBin is null)
229 return;
230
231 try
232 {
233 string? Message = this.Message;
234 if (string.IsNullOrEmpty(Message))
236
237 ServiceRef.PlatformSpecific.ShareImage(this.QrCodeBin, string.Format(CultureInfo.CurrentCulture, Message, this.Amount, this.Currency),
238 ServiceRef.Localizer[nameof(AppResources.Share)], "RequestPayment.png");
239 }
240 catch (Exception ex)
241 {
242 ServiceRef.LogService.LogException(ex);
244 }
245 }
246
251 [RelayCommand]
252 public async Task OpenCalculator(object Parameter)
253 {
254 try
255 {
256 switch (Parameter?.ToString())
257 {
258 case "AmountText":
259 CalculatorNavigationArgs AmountArgs = new(this, nameof(this.AmountText));
260
261 await ServiceRef.NavigationService.GoToAsync(nameof(CalculatorPage), AmountArgs, BackMethod.Pop);
262 break;
263
264 case "AmountExtraText":
265 CalculatorNavigationArgs ExtraArgs = new(this, nameof(this.AmountExtraText));
266
267 await ServiceRef.NavigationService.GoToAsync(nameof(CalculatorPage), ExtraArgs, BackMethod.Pop);
268 break;
269 }
270 }
271 catch (Exception ex)
272 {
274 }
275 }
276
277 #region ILinkableView
278
282 public override Task<string> Title => Task.FromResult<string>(ServiceRef.Localizer[nameof(AppResources.RequestPayment)]);
283
284 #endregion
285
286
287 }
288}
A strongly-typed resource class, for looking up localized strings, etc.
static string Share
Looks up a localized string similar to Share.
static string NetworkAddressOfContactUnknown
Looks up a localized string similar to Network address of contact unknown..
static string RequestPayment
Looks up a localized string similar to Request Payment.
static string SelectFromWhomToRequestPayment
Looks up a localized string similar to Select from whom to request payment..
static string RequestPaymentMessage
Looks up a localized string similar to Requesting payment of {0} {1}..
static string ErrorTitle
Looks up a localized string similar to An error has occurred.
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
static INavigationService NavigationService
The navigation service for navigating between pages.
Definition: ServiceRef.cs:178
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
static IPlatformSpecific PlatformSpecific
Localization service
Definition: ServiceRef.cs:383
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:15
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:48
void ShareImage(byte[] PngFile, string Message, string Title, string FileName)
Shares an image in PNG format.
Task GoToAsync(string Route)
Navigates to the specified route and pushes the page onto the navigation stack.
Task DisplayException(Exception Exception, string? Title=null)
Displays an alert/message box to the user.
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.