Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VerifyCodeViewModel.cs
1using System.ComponentModel;
2using CommunityToolkit.Mvvm.ComponentModel;
3using CommunityToolkit.Mvvm.Input;
7
9{
13 public partial class VerifyCodeViewModel : BaseViewModel
14 {
15 private readonly VerifyCodeNavigationArgs? navigationArgs;
16
21 public VerifyCodeViewModel(VerifyCodeNavigationArgs? Args)
22 {
23 this.navigationArgs = Args;
24
25 if (Args is not null)
26 this.CodeVerification = Args.CodeVerification;
27 }
28
30 protected override async Task OnInitialize()
31 {
32 await base.OnInitialize();
33
34 LocalizationManager.Current.PropertyChanged += this.LocalizationManagerEventHandler;
35
36 if (this.CodeVerification is not null)
37 this.CodeVerification.CountDownTimer.Tick += this.CountDownEventHandler;
38
39 this.LocalizationManagerEventHandler(null, new(null));
40 }
41
43 protected override async Task OnDispose()
44 {
45 LocalizationManager.Current.PropertyChanged -= this.LocalizationManagerEventHandler;
46
47 if (this.CodeVerification is not null)
48 this.CodeVerification.CountDownTimer.Tick -= this.CountDownEventHandler;
49
50 if (this.navigationArgs?.VarifyCode is TaskCompletionSource<string> TaskSource)
51 TaskSource.TrySetResult(string.Empty);
52
53 await base.OnDispose();
54 }
55
56 public void LocalizationManagerEventHandler(object? sender, PropertyChangedEventArgs e)
57 {
58 this.OnPropertyChanged(nameof(this.LocalizedVerifyCodePageDetails));
59 this.OnPropertyChanged(nameof(this.LocalizedResendCodeText));
60 }
61
62 private void CountDownEventHandler(object? sender, EventArgs e)
63 {
64 this.OnPropertyChanged(nameof(this.LocalizedResendCodeText));
65 }
66
71 private async Task TrySetResultAndClosePage(string? Url)
72 {
73 if (this.navigationArgs?.VarifyCode is not null)
74 {
75 TaskCompletionSource<string?> TaskSource = this.navigationArgs.VarifyCode;
76 this.navigationArgs.VarifyCode = null;
77
78 await MainThread.InvokeOnMainThreadAsync(async () =>
79 {
80 try
81 {
82 await base.GoBack();
83 TaskSource.TrySetResult(Url);
84 }
85 catch (Exception ex)
86 {
87 ServiceRef.LogService.LogException(ex);
88 }
89 });
90 }
91 }
92
93 #region Properties
94
95 [ObservableProperty]
96 private ICodeVerification? codeVerification;
97
98 [ObservableProperty]
99 [NotifyCanExecuteChangedFor(nameof(VerifyCommand))]
100 private string? verifyCodeText;
101
102 public string LocalizedVerifyCodePageDetails
103 {
104 get
105 {
106 return ServiceRef.Localizer[nameof(AppResources.OnboardingVerifyCodePageDetails), this.navigationArgs?.PhoneOrEmail ?? string.Empty];
107 }
108 }
109
110 public string LocalizedResendCodeText
111 {
112 get
113 {
114 if ((this.CodeVerification is not null) && (this.CodeVerification.CountDownSeconds > 0))
115 return ServiceRef.Localizer[nameof(AppResources.ResendCodeSeconds), this.CodeVerification.CountDownSeconds];
116
117 return ServiceRef.Localizer[nameof(AppResources.ResendCode)];
118 }
119 }
120
121 #endregion
122
123 public bool CanVerify => !string.IsNullOrEmpty(this.VerifyCodeText) && (this.VerifyCodeText.Length == 6);
124
125 [RelayCommand(CanExecute = nameof(CanVerify))]
126 public Task Verify()
127 {
128 return this.TrySetResultAndClosePage(this.VerifyCodeText);
129 }
130
132 public override Task GoBack()
133 {
134 return this.TrySetResultAndClosePage(string.Empty);
135 }
136 }
137}
Base class that references services in the app.
Definition: ServiceRef.cs:31
static ILogService LogService
Log service.
Definition: ServiceRef.cs:91
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...
The view model to bind to when verifying a code.
override Task GoBack()
Method called when user wants to navigate to the previous screen.
override async Task OnDispose()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
override async Task OnInitialize()
Method called when view is initialized for the first time. Use this method to implement registration ...
VerifyCodeViewModel(VerifyCodeNavigationArgs? Args)
Creates a new instance of the VerifyCodeViewModel class.