Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VerifyCodePage.xaml.cs
1using CommunityToolkit.Maui.Core.Platform;
2using CommunityToolkit.Mvvm.Messaging;
3using Microsoft.Maui.Controls.PlatformConfiguration;
4using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;
6
8{
9 public partial class VerifyCodePage
10 {
11 private readonly List<Label> innerLabels;
12
17 {
18 this.InitializeComponent();
19
20 VerifyCodeViewModel ViewModel = new(ServiceRef.UiService.PopLatestArgs<VerifyCodeNavigationArgs>());
21 this.ContentPageModel = ViewModel;
22
23 this.innerLabels = [
24 this.InnerCode1,
25 this.InnerCode2,
26 this.InnerCode3,
27 this.InnerCode4,
28 this.InnerCode5,
29 this.InnerCode6
30 ];
31
32 this.InnerCodeEntry.Text = string.Empty;
33 }
34
36 protected override async Task OnAppearingAsync()
37 {
38 await base.OnAppearingAsync();
39
40 WeakReferenceMessenger.Default.Register<KeyboardSizeMessage>(this, this.HandleKeyboardSizeMessage);
41 }
42
44 protected override async Task OnDisappearingAsync()
45 {
46 WeakReferenceMessenger.Default.Unregister<KeyboardSizeMessage>(this);
47
48 await base.OnDisappearingAsync();
49 }
50
51 private async void HandleKeyboardSizeMessage(object Recipient, KeyboardSizeMessage Message)
52 {
53 await this.Dispatcher.DispatchAsync(() =>
54 {
55 double Bottom = 0;
56 if (DeviceInfo.Platform == DevicePlatform.iOS)
57 {
58 Thickness SafeInsets = this.On<iOS>().SafeAreaInsets();
59 Bottom = SafeInsets.Bottom;
60 Thickness Margin = new(0, 0, 0, Message.KeyboardSize - Bottom);
61 this.TheMainGrid.Margin = Margin;
62 }
63
64
65 });
66 }
67
68 private async void InnerCodeEntry_TextChanged(object? Sender, TextChangedEventArgs e)
69 {
70 string NewText = e.NewTextValue;
71 int NewLength = NewText.Length;
72
73 bool IsValid = (NewLength <= this.innerLabels.Count) || NewText.ToCharArray().All(ch => !"0123456789".Contains(ch));
74
75 if (!IsValid)
76 {
77 this.InnerCodeEntry.Text = e.OldTextValue;
78 return;
79 }
80
81 for (int i = 0; i < this.innerLabels.Count; i++)
82 {
83 if (NewLength > i)
84 {
85 this.innerLabels[i].Text = NewText[i..(i + 1)];
86 VisualStateManager.GoToState(this.innerLabels[i], VisualStateManager.CommonStates.Normal);
87 }
88 else
89 {
90 this.innerLabels[i].Text = "0\u2060"; // Added a "zero width no-break space" to make the disabled state to work right :)
91 VisualStateManager.GoToState(this.innerLabels[i], VisualStateManager.CommonStates.Disabled);
92 }
93 }
94
95 if (NewLength == this.innerLabels.Count)
96 await this.InnerCodeEntry.HideKeyboardAsync();
97 }
98
99 private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
100 {
101 this.InnerCodeEntry.Focus();
102 }
103 }
104}
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
VerifyCodePage()
Creates a new instance of the VerifyCodePage class.
The view model to bind to when verifying a code.
class KeyboardSizeMessage(float KeyboardSize)
Keyboard size change message
Definition: Messages.cs:19