Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NeuroWalletOrchestratorService.cs
1using EDaler;
2using EDaler.Uris;
15using NeuroFeatures;
17
19{
20 [Singleton]
21 internal class NeuroWalletOrchestratorService : LoadableService, INeuroWalletOrchestratorService
22 {
23 public NeuroWalletOrchestratorService()
24 {
25 }
26
27 public override Task Load(bool isResuming, CancellationToken cancellationToken)
28 {
29 if (this.BeginLoad(isResuming, cancellationToken))
30 {
31 ServiceRef.XmppService.EDalerBalanceUpdated += this.Wallet_BalanceUpdated;
32 ServiceRef.XmppService.NeuroFeatureAdded += this.Wallet_TokenAdded;
33 ServiceRef.XmppService.NeuroFeatureRemoved += this.Wallet_TokenRemoved;
34 ServiceRef.XmppService.NeuroFeatureStateUpdated += this.Wallet_StateUpdated;
35 ServiceRef.XmppService.NeuroFeatureVariablesUpdated += this.Wallet_VariablesUpdated;
36
37 this.EndLoad(true);
38 }
39
40 return Task.CompletedTask;
41 }
42
43 public override Task Unload()
44 {
45 if (this.BeginUnload())
46 {
47 ServiceRef.XmppService.EDalerBalanceUpdated -= this.Wallet_BalanceUpdated;
48 ServiceRef.XmppService.NeuroFeatureAdded -= this.Wallet_TokenAdded;
49 ServiceRef.XmppService.NeuroFeatureRemoved -= this.Wallet_TokenRemoved;
50 ServiceRef.XmppService.NeuroFeatureStateUpdated -= this.Wallet_StateUpdated;
51 ServiceRef.XmppService.NeuroFeatureVariablesUpdated -= this.Wallet_VariablesUpdated;
52
53 this.EndUnload();
54 }
55
56 return Task.CompletedTask;
57 }
58
59 #region Event Handlers
60
61 private async Task Wallet_BalanceUpdated(object? Sender, BalanceEventArgs e)
62 {
63 if (e.Balance.Amount > 0 && !ServiceRef.TagProfile.HasWallet)
64 ServiceRef.TagProfile.HasWallet = true;
65
67 }
68
69 private async Task Wallet_TokenAdded(object? Sender, TokenEventArgs e)
70 {
72 }
73
74 private async Task Wallet_TokenRemoved(object? Sender, TokenEventArgs e)
75 {
77 out NotificationEvent[]? Events) && Events is not null)
78 {
80 }
81
83 }
84
85 private async Task Wallet_StateUpdated(object? Sender, NewStateEventArgs e)
86 {
88 }
89
90 private async Task Wallet_VariablesUpdated(object? Sender, VariablesUpdatedEventArgs e)
91 {
93 }
94
95 #endregion
96
100 public async Task OpenEDalerWallet()
101 {
102 try
103 {
104 Balance Balance = await ServiceRef.XmppService.GetEDalerBalance();
105 (decimal PendingAmount, string PendingCurrency, PendingPayment[] PendingPayments) = await ServiceRef.XmppService.GetPendingEDalerPayments();
106 (AccountEvent[] Events, bool More) = await ServiceRef.XmppService.GetEDalerAccountEvents(Constants.BatchSizes.AccountEventBatchSize);
107
108 WalletNavigationArgs e = new(Balance, PendingAmount, PendingCurrency, PendingPayments, Events, More);
109
111 }
112 catch (Exception ex)
113 {
115 }
116 }
117
122 public async Task OpenEDalerUri(string Uri)
123 {
124 if (!ServiceRef.XmppService.TryParseEDalerUri(Uri, out EDalerUri Parsed, out string Reason))
125 {
126 await ServiceRef.UiService.DisplayAlert(ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
127 ServiceRef.Localizer[nameof(AppResources.InvalidEDalerUri), Reason]);
128 return;
129 }
130
131 if (Parsed.Expires.AddDays(1) < DateTime.UtcNow)
132 {
133 await ServiceRef.UiService.DisplayAlert(ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
134 ServiceRef.Localizer[nameof(AppResources.ExpiredEDalerUri)]);
135 return;
136 }
137
138 if (Parsed is EDalerIssuerUri)
139 {
140 MainThread.BeginInvokeOnMainThread(async () =>
141 {
143 });
144 }
145 else if (Parsed is EDalerDestroyerUri)
146 {
147 // TODO
148 }
149 else if (Parsed is EDalerPaymentUri)
150 {
151 MainThread.BeginInvokeOnMainThread(async () =>
152 {
154 });
155 }
156 else if (Parsed is EDalerIncompletePaymentUri)
157 {
158 MainThread.BeginInvokeOnMainThread(async () =>
159 {
161 });
162 }
163 else
164 {
165 await ServiceRef.UiService.DisplayAlert(ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
166 ServiceRef.Localizer[nameof(AppResources.UnrecognizedEDalerURI)]);
167 return;
168 }
169 }
170
175 public async Task OpenNeuroFeatureUri(string Uri)
176 {
177 int i = Uri.IndexOf(':');
178 if (i < 0)
179 return;
180
181 string TokenId = Uri[(i + 1)..];
182
183 try
184 {
185 Token Token = await ServiceRef.XmppService.GetNeuroFeature(TokenId);
186
188 Events = [];
189
190 TokenDetailsNavigationArgs Args = new(new TokenItem(Token, Events));
191
192 await ServiceRef.UiService.GoToAsync(nameof(TokenDetailsPage), Args, BackMethod.Pop);
193 }
194 catch (Exception ex)
195 {
196 ServiceRef.LogService.LogException(ex);
198 }
199 }
200
201 }
202}
Account event
Definition: AccountEvent.cs:16
Contains information about a balance.
Definition: Balance.cs:11
Contains information about a pending payment.
eDaler URI representing the destruction of eDaler by an operator.
eDaler URI representing the issuance of eDaler by an operator.
eDaler URI representing the payment of eDaler from a sender to a receiver.
Abstract base class for eDaler URIs
Definition: EDalerUri.cs:14
Incomplete eDaler URI for simplifying payments to a predefined recipient.
const int AccountEventBatchSize
Number of account events to load in a single batch.
Definition: Constants.cs:779
A set of never changing property constants and helpful values.
Definition: Constants.cs:7
bool BeginLoad(bool IsResuming, CancellationToken CancellationToken)
Sets the IsLoading flag if the service isn't already loading.
void EndLoad(bool isLoaded)
Sets the IsLoading and IsLoaded flags and fires an event representing the current load state of the s...
bool BeginUnload()
Sets the IsLoading flag if the service isn't already unloading.
void EndUnload()
Sets the IsLoading and IsLoaded flags and fires an event representing the current load state of the s...
Contains information about a change in a state-machine associated with a token.
Contains information about a change in internal variables of a state-machine associated with a token.
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 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
Holds navigation parameters specific to eDaler URIs.
A page that allows the user to receive newly issued eDaler.
A page that allows the user to view the contents of its eDaler wallet, pending payments and recent ac...
Holds navigation parameters specific to the eDaler wallet.
A page that allows the user to realize payments.
A page that allows the user to accept an offline payment.
A page that allows the user to view information about a token.
Neuro-Feature Token
Definition: Token.cs:43
Task DeleteEvents(NotificationEventType Type, CaseInsensitiveString Category)
Deletes events for a given button and category.
bool TryGetNotificationEvents(NotificationEventType Type, CaseInsensitiveString Category, [NotNullWhen(true)] out NotificationEvent[]? Events)
Tries to get available notification events.
Task NewEvent(NotificationEvent Event)
Registers a new event and notifies the user.
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.
abstract class NotificationEvent()
Abstract base class of notification events.
NotificationEventType
Button on which event is to be displayed.
BackMethod
Navigation Back Method
Definition: BackMethod.cs:7