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.Events;
3using EDaler.Uris;
16using NeuroFeatures;
19
21{
22 [Singleton]
23 internal class NeuroWalletOrchestratorService : LoadableService, INeuroWalletOrchestratorService
24 {
25 public NeuroWalletOrchestratorService()
26 {
27 }
28
29 public override Task Load(bool isResuming, CancellationToken CancellationToken)
30 {
31 if (this.BeginLoad(isResuming, CancellationToken))
32 {
33 ServiceRef.XmppService.EDalerBalanceUpdated += this.Wallet_BalanceUpdated;
34 ServiceRef.XmppService.NeuroFeatureAdded += this.Wallet_TokenAdded;
35 ServiceRef.XmppService.NeuroFeatureRemoved += this.Wallet_TokenRemoved;
36 ServiceRef.XmppService.NeuroFeatureStateUpdated += Wallet_StateUpdated;
37 ServiceRef.XmppService.NeuroFeatureVariablesUpdated += this.Wallet_VariablesUpdated;
38
39 this.EndLoad(true);
40 }
41
42 return Task.CompletedTask;
43 }
44
45 public override Task Unload()
46 {
47 if (this.BeginUnload())
48 {
49 ServiceRef.XmppService.EDalerBalanceUpdated -= this.Wallet_BalanceUpdated;
50 ServiceRef.XmppService.NeuroFeatureAdded -= this.Wallet_TokenAdded;
51 ServiceRef.XmppService.NeuroFeatureRemoved -= this.Wallet_TokenRemoved;
52 ServiceRef.XmppService.NeuroFeatureStateUpdated -= Wallet_StateUpdated;
53 ServiceRef.XmppService.NeuroFeatureVariablesUpdated -= this.Wallet_VariablesUpdated;
54
55 this.EndUnload();
56 }
57
58 return Task.CompletedTask;
59 }
60
61 #region Event Handlers
62
63 private async Task Wallet_BalanceUpdated(object? Sender, BalanceEventArgs e)
64 {
65 if (e.Balance.Amount > 0 && !ServiceRef.TagProfile.HasWallet)
66 ServiceRef.TagProfile.HasWallet = true;
67
70
71 NotificationIntent Intent = new()
72 {
74 Title = Title,
75 Body = Body,
76 Action = NotificationAction.OpenBalance,
77 EntityId = e.Balance.Currency
78 };
79
80 await ServiceRef.Provider.GetRequiredService<INotificationServiceV2>().AddAsync(Intent, NotificationSource.Xmpp, null, CancellationToken.None);
81 }
82
83 private async Task Wallet_TokenAdded(object? Sender, TokenEventArgs e)
84 {
85 // Do nothing here. Check XMPPService
86 }
87
88 private async Task Wallet_TokenRemoved(object? Sender, TokenEventArgs e)
89 {
92
93 NotificationIntent Intent = new()
94 {
96 Title = Title,
97 Body = Body,
98 Action = NotificationAction.OpenToken,
99 EntityId = e.Token.TokenId
100 };
101
102 await ServiceRef.Provider.GetRequiredService<INotificationServiceV2>().AddAsync(Intent, NotificationSource.Xmpp, null, CancellationToken.None);
103 }
104
105 private static async Task Wallet_StateUpdated(object? Sender, NewStateEventArgs e)
106 {
107 NotificationIntent Intent = new()
108 {
109 Channel = Constants.PushChannels.Tokens,
110 Title = ServiceRef.Localizer[nameof(AppResources.State)],
111 Action = NotificationAction.OpenToken,
112 EntityId = e.TokenId
113 };
114
115 await ServiceRef.Provider.GetRequiredService<INotificationServiceV2>().AddAsync(Intent, NotificationSource.Xmpp, null, CancellationToken.None);
116 }
117
118 private async Task Wallet_VariablesUpdated(object? Sender, VariablesUpdatedEventArgs e)
119 {
120 NotificationIntent Intent = new()
121 {
122 Channel = Constants.PushChannels.Tokens,
123 Title = ServiceRef.Localizer[nameof(AppResources.State)],
124 Action = NotificationAction.OpenToken,
125 EntityId = e.TokenId
126 };
127
128 await ServiceRef.Provider.GetRequiredService<INotificationServiceV2>().AddAsync(Intent, NotificationSource.Xmpp, null, CancellationToken.None);
129 }
130
131 #endregion
132
136 public async Task OpenEDalerWallet()
137 {
138 try
139 {
140 Balance Balance = await ServiceRef.XmppService.GetEDalerBalance();
141 (decimal PendingAmount, string PendingCurrency, PendingPayment[] PendingPayments) = await ServiceRef.XmppService.GetPendingEDalerPayments();
142 (AccountEvent[] Events, bool More) = await ServiceRef.XmppService.GetEDalerAccountEvents(Constants.BatchSizes.AccountEventBatchSize);
143
144 WalletNavigationArgs e = new(Balance, PendingAmount, PendingCurrency, PendingPayments, Events, More);
145
147 }
148 catch (Exception ex)
149 {
151 }
152 }
153
158 public async Task OpenEDalerUri(string Uri)
159 {
160 if (!ServiceRef.XmppService.TryParseEDalerUri(Uri, out EDalerUri Parsed, out string Reason))
161 {
164 return;
165 }
166
167 if (Parsed.Expires.AddDays(1) < DateTime.UtcNow)
168 {
171 return;
172 }
173
174 if (Parsed is EDalerIssuerUri)
175 {
176 MainThread.BeginInvokeOnMainThread(async () =>
177 {
179 });
180 }
181 else if (Parsed is EDalerDestroyerUri)
182 {
183 // TODO
184 }
185 else if (Parsed is EDalerPaymentUri)
186 {
187 MainThread.BeginInvokeOnMainThread(async () =>
188 {
190 });
191 }
192 else if (Parsed is EDalerIncompletePaymentUri)
193 {
194 MainThread.BeginInvokeOnMainThread(async () =>
195 {
197 });
198 }
199 else
200 {
203 return;
204 }
205 }
206
211 public async Task OpenNeuroFeatureUri(string Uri)
212 {
213 int i = Uri.IndexOf(':');
214 if (i < 0)
215 return;
216
217 string TokenId = Uri[(i + 1)..];
218
219 try
220 {
221 Token Token = await ServiceRef.XmppService.GetNeuroFeature(TokenId);
222
224 Events = [];
225
226 TokenDetailsNavigationArgs Args = new(new TokenItem(Token, Events));
227
229 }
230 catch (Exception ex)
231 {
232 ServiceRef.LogService.LogException(ex);
234 }
235 }
236
237 }
238}
Account event
Definition: AccountEvent.cs:16
Contains information about a balance.
Definition: Balance.cs:11
decimal Amount
Amount at given point in time.
Definition: Balance.cs:44
Wallet balance event arguments.
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:926
const string EDaler
eDaler channel
Definition: Constants.cs:759
const string Tokens
Tokens channel
Definition: Constants.cs:764
A set of never changing property constants and helpful values.
Definition: Constants.cs:24
A strongly-typed resource class, for looking up localized strings, etc.
static string State
Looks up a localized string similar to State.
static string NotificationTokenRemovedBody
Looks up a localized string similar to Your wallet was updated..
static string NotificationTokenRemovedTitle
Looks up a localized string similar to Token removed.
static string NotificationBalanceUpdatedTitle
Looks up a localized string similar to Balance updated.
static string ExpiredEDalerUri
Looks up a localized string similar to This eDaler code has expired..
static string InvalidEDalerUri
Looks up a localized string similar to Invalid eDaler URI: {0}.
static string NotificationBalanceUpdatedBody
Looks up a localized string similar to Your balance was updated..
static string UnrecognizedEDalerURI
Looks up a localized string similar to Unrecognized eDaler URI..
static string ErrorTitle
Looks up a localized string similar to An error has occurred.
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...
Platform-neutral intent describing how to route a notification.
Base class that references services in the app.
Definition: ServiceRef.cs:43
static IServiceProvider Provider
The service provider for the app. This is set before the app is started, and will be used to resolve ...
Definition: ServiceRef.cs:48
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 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
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.
Event arguments events when the current state of a state-machine has changed.
Event arguments for token events.
Event arguments events when the variables of a state-machine has changed.
Neuro-Feature Token
Definition: Token.cs:46
bool TryGetNotificationEvents(NotificationEventType Type, CaseInsensitiveString Category, [NotNullWhen(true)] out NotificationEvent[]? Events)
Tries to get available notification events.
Interface for the redesigned notification service.
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.
abstract class NotificationEvent()
Abstract base class of notification events.
NotificationAction
Actions that can be routed from notifications.
NotificationEventType
Button on which event is to be displayed.
NotificationSource
Describes the source producing a notification.
BackMethod
Navigation Back Method
Definition: BackMethod.cs:7
Action
The Action field indicates the action performed by the Reporting-MTA as a result of its attempt to de...