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 public override async Task OnInitializeAsync()
31 {
32 await base.OnInitializeAsync();
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 public override async Task OnDisposeAsync()
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.OnDisposeAsync();
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
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}
A strongly-typed resource class, for looking up localized strings, etc.
static string OnboardingVerifyCodePageDetails
Looks up a localized string similar to Please enter verification code sent to {0}.
static string ResendCode
Looks up a localized string similar to Resend Code.
static string ResendCodeSeconds
Looks up a localized string similar to Resend ({0}).
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
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...
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 OnDisposeAsync()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
VerifyCodeViewModel(VerifyCodeNavigationArgs? Args)
Creates a new instance of the VerifyCodeViewModel class.
override async Task OnInitializeAsync()
Method called when view is initialized for the first time. Use this method to implement registration ...