Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SelectPhoneCodePopup.xaml.cs
1using System;
3using System.Linq;
4using System.Threading;
5using System.Threading.Tasks;
6using Microsoft.Maui.Controls;
9
11{
12 public partial class SelectPhoneCodePopup : BasePopup, IDisposable
13 {
14 private readonly TaskCompletionSource<ISO_3166_Country?> result = new(TaskCreationOptions.RunContinuationsAsynchronously);
15 private CancellationTokenSource? cancellationTokenSource;
16 private bool isDisposed;
17
18 public Task<ISO_3166_Country?> Result => this.result.Task;
19
20 public static ISO_3166_Country[] Countries => ISO_3166_1.Countries;
21
23 {
24 this.InitializeComponent();
25 this.BindingContext = this;
26 this.InnerSearchBar.Text = string.Empty;
27 }
28
29 private void SearchBar_TextChanged(object? sender, TextChangedEventArgs e)
30 {
31 if (string.IsNullOrEmpty(e.NewTextValue))
32 {
33 this.InnerListView.ItemsSource = Countries;
34 return;
35 }
36
37 this.cancellationTokenSource?.Cancel();
38 this.cancellationTokenSource = new CancellationTokenSource();
39 CancellationToken token = this.cancellationTokenSource.Token;
40
41 Task.Run(() =>
42 {
43 IEnumerable<ISO_3166_Country> filtered = Countries.Where(country =>
44 country.Name.Contains(e.NewTextValue, StringComparison.OrdinalIgnoreCase)
45 || string.Equals(country.Alpha2, e.NewTextValue, StringComparison.OrdinalIgnoreCase)
46 || string.Equals(country.Alpha3, e.NewTextValue, StringComparison.OrdinalIgnoreCase)
47 || country.DialCode.Contains(e.NewTextValue, StringComparison.OrdinalIgnoreCase));
48
49 this.Dispatcher.Dispatch(() =>
50 {
51 if (!token.IsCancellationRequested)
52 {
53 this.InnerListView.ItemsSource = filtered;
54 }
55 });
56 }, token);
57 }
58
59 private async void InnerListView_SelectionChanged(object? sender, SelectionChangedEventArgs e)
60 {
61 if (this.InnerListView.SelectedItem is ISO_3166_Country selected)
62 {
63 this.result.TrySetResult(selected);
64 await ServiceRef.PopupService.PopAsync();
65 }
66 }
67
68 public override async Task OnDisappearingAsync()
69 {
70 this.result.TrySetResult(null);
71 await base.OnDisappearingAsync();
72 }
73
74 protected virtual void Dispose(bool disposing)
75 {
76 if (!this.isDisposed)
77 {
78 if (disposing)
79 {
80 this.cancellationTokenSource?.Dispose();
81 this.cancellationTokenSource = null;
82 }
83
84 this.isDisposed = true;
85 }
86 }
87
88 public void Dispose()
89 {
90 this.Dispose(true);
91 GC.SuppressFinalize(this);
92 }
93 }
94}
Conversion between Country Names and ISO-3166-1 country codes.
Definition: ISO_3166_1.cs:10
static ISO_3166_Country[] Countries
This collection built from Wikipedia entry on ISO3166-1 on 9th Feb 2016
Definition: ISO_3166_1.cs:37
Base class that references services in the app.
Definition: ServiceRef.cs:43
static IPopupService PopupService
Popup service for presenting application popups.
Definition: ServiceRef.cs:142
Compatibility wrapper so old Mopups based popups work using the new popup infrastructure.
Definition: BasePopup.cs:10
override async Task OnDisappearingAsync()
Method called when view is disappearing from the screen.
class ISO_3166_Country(string Name, string Alpha2, string Alpha3, int NumericCode, string DialCode, EmojiInfo? EmojiInfo=null)
Representation of an ISO3166-1 Country