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;
10
12{
18 public abstract partial class BaseViewModel : ObservableObject, ILifeCycleView, IBackButtonHandler
19 {
20 private readonly List<BaseViewModel> childViewModels = [];
21 private bool isOverlayVisible;
22 private DateTime overlayLastActivationTime;
23
28 {
29 }
30
34 public bool IsInitialized { get; private set; }
35
39 public bool IsAppearing { get; private set; }
40
44 public IEnumerable<BaseViewModel> Children => this.childViewModels;
45
46 public virtual double ViewWidthRequest => (DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density) * (7.0 / 8.0);
47 public virtual double MaximumViewHeightRequest => (DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density) * (3.0 / 4.0);
48
52 public static void GoToRegistrationStep(RegistrationStep NewStep)
53 {
55 WeakReferenceMessenger.Default.Send(new RegistrationPageMessage(ServiceRef.TagProfile.Step));
56 }
57
64 protected T AddChildViewModel<T>(T ChildViewModel) where T : BaseViewModel
65 {
66 this.childViewModels.Add(ChildViewModel);
67 return ChildViewModel;
68 }
69
76 protected T RemoveChildViewModel<T>(T ChildViewModel) where T : BaseViewModel
77 {
78 this.childViewModels.Remove(ChildViewModel);
79 return ChildViewModel;
80 }
81
85 public async Task RestoreState()
86 {
87 foreach (BaseViewModel ChildViewModel in this.childViewModels)
88 await ChildViewModel.DoRestoreState();
89
90 await this.DoRestoreState();
91 }
92
96 public async Task SaveState()
97 {
98 foreach (BaseViewModel ChildViewModel in this.childViewModels)
99 await ChildViewModel.DoSaveState();
100
101 await this.DoSaveState();
102 }
103
107 public async Task Shutdown()
108 {
109 await this.SaveState();
110 await this.OnDisposeAsync();
111 }
112
116 protected virtual Task DoRestoreState()
117 {
118 return Task.CompletedTask;
119 }
120
124 protected virtual Task DoSaveState()
125 {
126 return Task.CompletedTask;
127 }
128
134 protected string GetSettingsKey(string PropertyName)
135 {
136 return this.GetType().FullName + "." + PropertyName;
137 }
138
142 [ObservableProperty]
143 private bool isBusy;
144
149 public virtual void SetIsBusy(bool IsBusy)
150 {
151 this.IsBusy = IsBusy;
152 }
153
158 {
159 get => this.isOverlayVisible;
160 set
161 {
162 if (this.isOverlayVisible == value)
163 return;
164
165 if (value)
166 {
167 this.isOverlayVisible = true;
168 this.overlayLastActivationTime = DateTime.Now;
169 this.OnPropertyChanged();
170 }
171 else
172 {
173 TimeSpan MinimumOverlayTime = TimeSpan.FromMilliseconds(500);
174 TimeSpan ElapsedTime = DateTime.Now.Subtract(this.overlayLastActivationTime);
175
176 if (ElapsedTime >= MinimumOverlayTime)
177 {
178 this.isOverlayVisible = false;
179 this.OnPropertyChanged();
180 }
181 else
182 {
183 // It is important to use the property here, not the field, because last activation time might be updated while we are waiting,
184 // we need to recheck it and possibly reschedule.
185 Task.Delay(MinimumOverlayTime - ElapsedTime).GetAwaiter().OnCompleted(() => this.isOverlayVisible = false);
186 }
187 }
188 }
189 }
190
191
197 public static async Task<bool> AreYouSure(string Message)
198 {
199 return await ServiceRef.UiService.DisplayAlert(
200 ServiceRef.Localizer[nameof(AppResources.Confirm)], Message,
203 }
204
205 public async Task<bool> OnBackButtonPressedAsync()
206 {
207 if (this.GoBackCommand.CanExecute(null))
208 _ = this.GoBackCommand.ExecuteAsync(null);
209
210 return true;
211 }
212
216 [RelayCommand(AllowConcurrentExecutions = false)]
217 public virtual async Task GoBack()
218 {
220 }
221
227 public virtual object? GetValue(string PropertyName)
228 {
229 PropertyInfo? PI = this.GetType().GetProperty(PropertyName)
230 ?? throw new ArgumentException("Property not found: " + PropertyName, nameof(PropertyName));
231
232 return PI.GetValue(this);
233 }
234
240 public virtual void SetValue(string PropertyName, object? Value)
241 {
242 PropertyInfo? PI = this.GetType().GetProperty(PropertyName)
243 ?? throw new ArgumentException("Property not found: " + PropertyName, nameof(PropertyName));
244
245 PI.SetValue(this, Value);
246 }
247
248 public virtual async Task OnInitializeAsync()
249 {
250 await App.ServicesReady;
251
252 foreach (BaseViewModel ChildViewModel in this.childViewModels)
253 await ChildViewModel.OnInitializeAsync();
254 }
255
256 public virtual async Task OnDisposeAsync()
257 {
258 foreach (BaseViewModel ChildViewModel in this.childViewModels)
259 await ChildViewModel.OnDisposeAsync();
260 }
261
262 public virtual async Task OnAppearingAsync()
263 {
264 await App.ServicesReady;
265
266 foreach (BaseViewModel ChildViewModel in this.childViewModels)
267 await ChildViewModel.OnAppearingAsync();
268 }
269
270 public virtual async Task OnDisappearingAsync()
271 {
272 foreach (BaseViewModel ChildViewModel in this.childViewModels)
273 await ChildViewModel.OnDisappearingAsync();
274 }
275
276
277 }
278}
Represents an instance of the Neuro-Access app.
Definition: App.xaml.cs:125
static Task ServicesReady
Task that completes once all core services have finished loading.
Definition: App.xaml.cs:174
A strongly-typed resource class, for looking up localized strings, etc.
static string Yes
Looks up a localized string similar to Yes.
static string Confirm
Looks up a localized string similar to Confirm.
static string No
Looks up a localized string similar to No.
Base class that references services in the app.
Definition: ServiceRef.cs:43
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 ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:202
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
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 async Task OnDisappearingAsync()
Method called when view is disappearing from the screen.
virtual async Task OnAppearingAsync()
Method called when view is appearing on the screen.
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.
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.
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.
virtual async Task OnInitializeAsync()
Method called when view is initialized for the first time. Use this method to implement registration ...
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 void SetIsBusy(bool IsBusy)
Sets the IsBusy property.
virtual async Task OnDisposeAsync()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
RegistrationStep Step
This profile's current registration step.
Definition: ITagProfile.cs:172
void GoToStep(RegistrationStep NewStep, bool SupressEvent=false)
Changes the current onboarding step.
Implement on a Page or its ViewModel to intercept back button presses. Return true to consume (preven...
Task GoBackAsync()
Pops the current page from the navigation stack and displays the previous page.
Task< bool > DisplayAlert(string Title, string Message, string? Accept=null, string? Cancel=null)
Displays an alert/message box to the user.
Interface for views who need to react to life-cycle events.
Definition: ImplTypes.g.cs:58
RegistrationStep
The different steps of a TAG Profile registration journey.
class RegistrationPageMessage(RegistrationStep Step)
RegistrationPage view change message
Definition: Messages.cs:9