Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MyTokensViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using CommunityToolkit.Mvvm.Input;
6using NeuroFeatures;
8using System.Collections.ObjectModel;
10
12{
18 {
19 private readonly MyTokensNavigationArgs? navigationArgs = Args;
20
22 public override async Task OnInitializeAsync()
23 {
24 await base.OnInitializeAsync();
25
26 try
27 {
29 SortedDictionary<CaseInsensitiveString, NotificationEvent[]> EventsByCateogy = ServiceRef.NotificationService.GetEventsByCategory(NotificationEventType.Wallet);
30
31 MainThread.BeginInvokeOnMainThread(() =>
32 {
33 if (e.Ok)
34 {
35 this.Tokens.Clear();
36
37 if (e.Tokens is not null)
38 {
39 foreach (Token Token in e.Tokens)
40 {
41 if (!EventsByCateogy.TryGetValue(Token.TokenId, out NotificationEvent[]? Events))
42 Events = [];
43
44 this.Tokens.Add(new TokenItem(Token, this.navigationArgs?.TokenItemProvider, Events));
45 }
46
47 this.HasTokens = true;
48 this.HasMoreTokens = e.Tokens.Length == Constants.BatchSizes.TokenBatchSize;
49 }
50 else
51 this.HasTokens = false;
52 }
53 else
54 this.HasTokens = false;
55 });
56
57 ServiceRef.XmppService.NeuroFeatureAdded += this.Wallet_TokenAdded;
58 ServiceRef.XmppService.NeuroFeatureRemoved += this.Wallet_TokenRemoved;
59 }
60 catch (Exception ex)
61 {
62 ServiceRef.LogService.LogException(ex);
63 }
64 }
65
67 public override Task OnDisposeAsync()
68 {
69 ServiceRef.XmppService.NeuroFeatureAdded -= this.Wallet_TokenAdded;
70 ServiceRef.XmppService.NeuroFeatureRemoved -= this.Wallet_TokenRemoved;
71
72 if (this.navigationArgs?.TokenItemProvider is TaskCompletionSource<TokenItem?> TaskSource)
73 TaskSource.TrySetResult(null);
74
75 return base.OnDisposeAsync();
76 }
77
78 private Task Wallet_TokenAdded(object? Sender, TokenEventArgs e)
79 {
81 Events = [];
82
83 MainThread.BeginInvokeOnMainThread(() =>
84 {
85 TokenItem Item = new(e.Token, this.navigationArgs?.TokenItemProvider, Events);
86
87 if (this.Tokens.Count == 0)
88 this.Tokens.Add(Item);
89 else
90 this.Tokens.Insert(0, Item);
91 });
92
93 return Task.CompletedTask;
94 }
95
96 private Task Wallet_TokenRemoved(object? Sender, TokenEventArgs e)
97 {
98 MainThread.BeginInvokeOnMainThread(() =>
99 {
100 int i, c = this.Tokens.Count;
101
102 for (i = 0; i < c; i++)
103 {
104 TokenItem Item = this.Tokens[i];
105
106 if (Item.TokenId == e.Token.TokenId)
107 {
108 this.Tokens.RemoveAt(i);
109 break;
110 }
111 }
112 });
113
114 return Task.CompletedTask;
115 }
116
117 #region Properties
118
122 [ObservableProperty]
123 private bool hasTokens;
124
128 [ObservableProperty]
129 private bool hasMoreTokens;
130
134 public ObservableCollection<TokenItem> Tokens { get; } = [];
135
136 #endregion
137
141 [RelayCommand]
142 private Task Back()
143 {
144 return this.GoBack();
145 }
146
148 public override Task GoBack()
149 {
150 this.navigationArgs?.TokenItemProvider.TrySetResult(null);
151 return base.GoBack();
152 }
153
157 [RelayCommand]
158 private async Task LoadMoreTokens()
159 {
160 if (this.HasMoreTokens)
161 {
162 this.HasMoreTokens = false; // So multiple requests are not made while scrolling.
163
164 try
165 {
166 TokensEventArgs e = await ServiceRef.XmppService.GetNeuroFeatures(this.Tokens.Count, Constants.BatchSizes.TokenBatchSize);
167 SortedDictionary<CaseInsensitiveString, NotificationEvent[]> EventsByCateogy = ServiceRef.NotificationService.GetEventsByCategory(NotificationEventType.Wallet);
168
169 MainThread.BeginInvokeOnMainThread(() =>
170 {
171 if (e.Ok)
172 {
173 if (e.Tokens is not null)
174 {
175 foreach (Token Token in e.Tokens)
176 {
177 if (!EventsByCateogy.TryGetValue(Token.TokenId, out NotificationEvent[]? Events))
178 Events = [];
179
180 this.Tokens.Add(new TokenItem(Token, Events));
181 }
182
183 this.HasMoreTokens = e.Tokens.Length == Constants.BatchSizes.TokenBatchSize;
184 }
185 }
186 });
187 }
188 catch (Exception ex)
189 {
190 ServiceRef.LogService.LogException(ex);
191 }
192 }
193 }
194
195 }
196}
const int TokenBatchSize
Number of tokens to load in a single batch.
Definition: Constants.cs:921
A set of never changing property constants and helpful values.
Definition: Constants.cs:24
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
static INotificationService NotificationService
Service for managing notifications for the user.
Definition: ServiceRef.cs:334
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:190
A view model that holds the XMPP state.
Event arguments for token events.
Event arguments for Tokens responses
string TokenId
Token ID
Definition: Token.cs:116
bool Ok
If the response is an OK result response (true), or an error response (false).
bool TryGetNotificationEvents(NotificationEventType Type, CaseInsensitiveString Category, [NotNullWhen(true)] out NotificationEvent[]? Events)
Tries to get available notification events.
SortedDictionary< CaseInsensitiveString, NotificationEvent[]> GetEventsByCategory(NotificationEventType Type)
Gets available notification events for a button, sorted by category.
abstract class NotificationEvent()
Abstract base class of notification events.
NotificationEventType
Button on which event is to be displayed.
partial class MyTokensViewModel(MyTokensNavigationArgs? Args)
The view model to bind to for when displaying my tokens.