Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BaseViewModel.cs
1using System.Reflection;
2using CommunityToolkit.Mvvm.ComponentModel;
3using CommunityToolkit.Mvvm.Input;
4using CommunityToolkit.Mvvm.Messaging;
8
10{
16 public abstract partial class BaseViewModel : ObservableObject, ILifeCycleView
17 {
18 private readonly List<BaseViewModel> childViewModels = [];
19 private bool isOverlayVisible;
20 private DateTime overlayLastActivationTime;
21
26 {
27 }
28
32 public bool IsInitialized { get; private set; }
33
37 public bool IsAppearing { get; private set; }
38
42 public IEnumerable<BaseViewModel> Children => this.childViewModels;
43
44 public virtual double ViewWidthRequest => (DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density) * (7.0 / 8.0);
45 public virtual double MaximumViewHeightRequest => (DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density) * (3.0 / 4.0);
46
50 public static void GoToRegistrationStep(RegistrationStep NewStep)
51 {
53 WeakReferenceMessenger.Default.Send(new RegistrationPageMessage(ServiceRef.TagProfile.Step));
54 }
55
62 protected T AddChildViewModel<T>(T ChildViewModel) where T : BaseViewModel
63 {
64 this.childViewModels.Add(ChildViewModel);
65 return ChildViewModel;
66 }
67
74 protected T RemoveChildViewModel<T>(T ChildViewModel) where T : BaseViewModel
75 {
76 this.childViewModels.Remove(ChildViewModel);
77 return ChildViewModel;
78 }
79
83 public async Task RestoreState()
84 {
85 foreach (BaseViewModel ChildViewModel in this.childViewModels)
86 await ChildViewModel.DoRestoreState();
87
88 await this.DoRestoreState();
89 }
90
94 public async Task SaveState()
95 {
96 foreach (BaseViewModel ChildViewModel in this.childViewModels)
97 await ChildViewModel.DoSaveState();
98
99 await this.DoSaveState();
100 }
101
105 public async Task Shutdown()
106 {
107 await this.SaveState();
108 await this.DoDisappearing();
109 }
110
114 protected virtual Task DoRestoreState()
115 {
116 return Task.CompletedTask;
117 }
118
122 protected virtual Task DoSaveState()
123 {
124 return Task.CompletedTask;
125 }
126
132 protected string GetSettingsKey(string PropertyName)
133 {
134 return this.GetType().FullName + "." + PropertyName;
135 }
136
140 [ObservableProperty]
141 private bool isBusy;
142
147 public virtual void SetIsBusy(bool IsBusy)
148 {
149 this.IsBusy = IsBusy;
150 }
151
156 {
157 get => this.isOverlayVisible;
158 set
159 {
160 if (this.isOverlayVisible == value)
161 return;
162
163 if (value)
164 {
165 this.isOverlayVisible = true;
166 this.overlayLastActivationTime = DateTime.Now;
167 this.OnPropertyChanged();
168 }
169 else
170 {
171 TimeSpan MinimumOverlayTime = TimeSpan.FromMilliseconds(500);
172 TimeSpan ElapsedTime = DateTime.Now.Subtract(this.overlayLastActivationTime);
173
174 if (ElapsedTime >= MinimumOverlayTime)
175 {
176 this.isOverlayVisible = false;
177 this.OnPropertyChanged();
178 }
179 else
180 {
181 // It is important to use the property here, not the field, because last activation time might be updated while we are waiting,
182 // we need to recheck it and possibly reschedule.
183 Task.Delay(MinimumOverlayTime - ElapsedTime).GetAwaiter().OnCompleted(() => this.isOverlayVisible = false);
184 }
185 }
186 }
187 }
188
193 public async Task DoInitialize()
194 {
195 if (!this.IsInitialized)
196 {
197 this.IsInitialized = true;
198
199 await this.OnInitialize();
200 }
201 }
202
207 protected virtual Task OnInitialize()
208 {
209 return Task.CompletedTask; // Do nothing by default.
210 }
211
216 public async Task DoDispose()
217 {
218 if (this.IsAppearing)
219 await this.DoDisappearing();
220
221 if (this.IsInitialized)
222 {
223 this.IsInitialized = false;
224
225 await this.OnDispose();
226 }
227 }
228
233 protected virtual Task OnDispose()
234 {
235 return Task.CompletedTask; // Do nothing by default.
236 }
237
241 public virtual async Task DoAppearing()
242 {
243 if (!this.IsInitialized)
244 await this.DoInitialize();
245
246 if (!this.IsAppearing)
247 {
248 DeviceDisplay.KeepScreenOn = true;
249
250 await this.OnAppearing();
251
252 foreach (BaseViewModel ChildViewModel in this.childViewModels)
253 await ChildViewModel.DoAppearing();
254
255 this.IsAppearing = true;
256 }
257 }
258
262 protected virtual Task OnAppearing()
263 {
264 return Task.CompletedTask; // Do nothing by default.
265 }
266
270 public async Task DoDisappearing()
271 {
272 if (this.IsAppearing)
273 {
274 foreach (BaseViewModel ChildViewModel in this.childViewModels)
275 await ChildViewModel.DoDisappearing();
276
277 await this.OnDisappearing();
278
279 this.IsAppearing = false;
280 }
281 }
282
286 protected virtual Task OnDisappearing()
287 {
288 return Task.CompletedTask; // Do nothing by default.
289 }
290
296 public static async Task<bool> AreYouSure(string Message)
297 {
298 return await ServiceRef.UiService.DisplayAlert(
299 ServiceRef.Localizer[nameof(AppResources.Confirm)], Message,
300 ServiceRef.Localizer[nameof(AppResources.Yes)],
301 ServiceRef.Localizer[nameof(AppResources.No)]);
302 }
303
307 [RelayCommand]
308 public virtual async Task GoBack()
309 {
310 await ServiceRef.UiService.GoBackAsync();
311 }
312
318 public virtual object? GetValue(string PropertyName)
319 {
320 PropertyInfo? PI = this.GetType().GetProperty(PropertyName)
321 ?? throw new ArgumentException("Property not found: " + PropertyName, nameof(PropertyName));
322
323 return PI.GetValue(this);
324 }
325
331 public virtual void SetValue(string PropertyName, object? Value)
332 {
333 PropertyInfo? PI = this.GetType().GetProperty(PropertyName)
334 ?? throw new ArgumentException("Property not found: " + PropertyName, nameof(PropertyName));
335
336 PI.SetValue(this, Value);
337 }
338
339 }
340}
Base class that references services in the app.
Definition: ServiceRef.cs:31
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
static ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:79
static IStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:235
A base class for all view models, inheriting from the BindableObject. NOTE: using this class requir...
static async Task< bool > AreYouSure(string Message)
Asks the user to confirm an action.
virtual Task OnDisappearing()
Method called when view is disappearing from the screen.
virtual Task OnDispose()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
virtual Task OnInitialize()
Method called when view is initialized for the first time. Use this method to implement registration ...
static void GoToRegistrationStep(RegistrationStep NewStep)
Set a new registration step
string GetSettingsKey(string PropertyName)
Helper method for getting a unique settings key for a given property.
virtual async Task DoAppearing()
Method called when view is appearing on the screen.
bool IsOverlayVisible
Gets or sets a value which indicates if the protective overlay with a spinner is visible.
virtual void SetValue(string PropertyName, object? Value)
Sets the value of a property in the view model.
T AddChildViewModel< T >(T ChildViewModel)
Use this method when nesting view models. This is the view model equivalent of master/detail pages.
T RemoveChildViewModel< T >(T ChildViewModel)
Use this method when nesting view models. This is the view model equivalent of master/detail pages.
virtual ? object GetValue(string PropertyName)
Gets the value of a property in the view model.
virtual Task DoRestoreState()
Override this method to do view model specific restoring of state when it's parent page/view appears ...
async Task Shutdown()
Convenience method that calls SaveState and then DoDisappearing.
bool IsAppearing
Returns true if the view model is shown.
virtual async Task GoBack()
Method called when user wants to navigate to the previous screen.
bool IsInitialized
Returns true if the view model is initialized.
IEnumerable< BaseViewModel > Children
Gets the child view models.
async Task DoDisappearing()
Method called when view is disappearing from the screen.
async Task DoDispose()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
async Task DoInitialize()
Method called when view is initialized for the first time. Use this method to implement registration ...
BaseViewModel()
Create an instance of a BaseViewModel.
async Task SaveState()
Called by the parent page when it disappears on screen, before the DoDisappearing method is called.
async Task RestoreState()
Called by the parent page when it appears on screen, after the DoAppearing method is called.
virtual Task DoSaveState()
Override this method to do view model specific saving of state when it's parent page/view disappears ...
virtual Task OnAppearing()
Method called when view is appearing on the screen.
virtual void SetIsBusy(bool IsBusy)
Sets the IsBusy property.
RegistrationStep Step
This profile's current registration step.
Definition: ITagProfile.cs:161
void GoToStep(RegistrationStep NewStep, bool SupressEvent=false)
Changes the current onboarding step.
Interface for views who need to react to life-cycle events.
RegistrationStep
The different steps of a TAG Profile registration journey.
class RegistrationPageMessage(RegistrationStep Step)
RegistrationPage view change message
Definition: Messages.cs:8