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