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 Mopups.Services;
3
5{
6 public partial class SelectPhoneCodePopup : IDisposable
7 {
8 private readonly TaskCompletionSource<ISO_3166_Country?> result = new();
9 private CancellationTokenSource? cancellationTokenSource;
10 private bool isDisposed;
11
15 public Task<ISO_3166_Country?> Result => this.result.Task;
16
21
23 {
24 this.InitializeComponent();
25 this.BindingContext = this;
26
27 this.InnerSearchBar.Text = string.Empty;
28 }
29
30 private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
31 {
32 if (e.NewTextValue.Length == 0)
33 {
34 this.InnerListView.ItemsSource = Countries;
35 return;
36 }
37
38 if (this.cancellationTokenSource is not null)
39 {
40 this.cancellationTokenSource.Cancel();
41 this.cancellationTokenSource = null;
42 }
43
44 this.cancellationTokenSource = new();
45 CancellationToken Token = this.cancellationTokenSource.Token;
46
47 Task.Run(() =>
48 {
49 IEnumerable<ISO_3166_Country> CountriesFiltered = Countries.Where(el =>
50 {
51 bool Result = el.Name.Contains(e.NewTextValue, StringComparison.OrdinalIgnoreCase) ||
52 string.Equals(el.Alpha2, e.NewTextValue, StringComparison.OrdinalIgnoreCase) ||
53 string.Equals(el.Alpha3, e.NewTextValue, StringComparison.OrdinalIgnoreCase) ||
54 el.DialCode.Contains(e.NewTextValue, StringComparison.OrdinalIgnoreCase);
55
56 return Result;
57 });
58
59 this.Dispatcher.Dispatch(() =>
60 {
61 if (!Token.IsCancellationRequested)
62 this.InnerListView.ItemsSource = CountriesFiltered;
63 });
64 }, Token);
65 }
66
67 private async void InnerListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
68 {
69 this.result.TrySetResult((ISO_3166_Country)this.InnerListView.SelectedItem);
70 await MopupService.Instance.PopAsync();
71 }
72
73 protected override void OnDisappearing()
74 {
75 this.result.TrySetResult(null);
76 base.OnDisappearing();
77 }
78
79 protected virtual void Dispose(bool disposing)
80 {
81 if (!this.isDisposed)
82 {
83 if (disposing)
84 {
85 this.cancellationTokenSource?.Dispose();
86 this.cancellationTokenSource = null;
87 }
88
89 this.isDisposed = true;
90 }
91 }
92
96 public void Dispose()
97 {
98 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
99 this.Dispose(disposing: true);
100 GC.SuppressFinalize(this);
101 }
102 }
103}
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
static ISO_3166_Country[] Countries
Available country definitions.
Task< ISO_3166_Country?> Result
Task waiting for result. null means dialog was closed without selection.
class ISO_3166_Country(string Name, string Alpha2, string Alpha3, int NumericCode, string DialCode, EmojiInfo? EmojiInfo=null)
Representation of an ISO3166-1 Country