Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SelectLanguagePopupViewModel.cs
1using System;
3using System.Collections.ObjectModel;
4using System.Globalization;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using CommunityToolkit.Mvvm.ComponentModel;
9using CommunityToolkit.Mvvm.Input;
10using CommunityToolkit.Mvvm.Messaging;
11using Microsoft.Maui.Storage;
15
17{
19 {
20 // Expose the supported languages.
21 public ObservableCollection<ObservableLanguage> Languages { get; }
22
23 [ObservableProperty]
24 private string? selectedLanguageName;
25
27 {
28 this.SelectedLanguageName = App.SelectedLanguage.Name;
29
30 this.Languages = new ObservableCollection<ObservableLanguage>(
31 App.SupportedLanguages.Select(l =>
32 new ObservableLanguage(l, this.SelectLanguageAsync)
33 {
34 IsSelected = l.Name == this.SelectedLanguageName
35 }));
36
37
38 }
39
40
41 public override Task OnAppearingAsync()
42 {
43 if(this.SelectedLanguageName is not null)
44 WeakReferenceMessenger.Default.Send(new ScrollToLanguageMessage(this.SelectedLanguageName));
45
46 return base.OnAppearingAsync();
47 }
48
49 [RelayCommand(AllowConcurrentExecutions = false)]
50 public async Task SelectLanguageAsync(string languageName)
51 {
52 // Find the language that matches the selection.
53 LanguageInfo? SelectedLanguage = this.Languages.FirstOrDefault(l => l.Language.Name == languageName)?.Language;
54 if (SelectedLanguage is null)
55 return;
56
57 // Update selection flags for visual feedback.
58 foreach (ObservableLanguage Observable in this.Languages)
59 {
60 bool IsSelected = Observable.Language.Name == languageName;
61 if (Observable.IsSelected != IsSelected)
62 Observable.IsSelected = IsSelected;
63 }
64
65 // Update selected language property.
66 this.SelectedLanguageName = SelectedLanguage.Name;
67
68 // Update the language if a new language is selected.
69 if (SelectedLanguage.Name != CultureInfo.CurrentCulture.Name)
70 {
71 Preferences.Set("user_selected_language", SelectedLanguage.TwoLetterISOLanguageName);
72 LocalizationManager.Current.CurrentCulture = SelectedLanguage;
73 }
74
75 // Display an alert that the language has been changed. And app should be restarted to make sure the changes are applied properly
76 await ServiceRef.UiService.DisplayAlert(
80 );
81
82 // Dismiss the popup via the UI service.
83 await ServiceRef.PopupService.PopAsync();
84 }
85 }
86}
Represents an instance of the Neuro-Access app.
Definition: App.xaml.cs:125
static LanguageInfo SelectedLanguage
Gets the selected language.
Definition: App.xaml.cs:199
static readonly LanguageInfo[] SupportedLanguages
Supported languages.
Definition: App.xaml.cs:179
A strongly-typed resource class, for looking up localized strings, etc.
static string LanguageChangedMessage
Looks up a localized string similar to Please restart the app to ensure all text changes language pro...
static string Ok
Looks up a localized string similar to OK.
static string LanguageChangedTitle
Looks up a localized string similar to Language changed.
Base class that references services in the app.
Definition: ServiceRef.cs:43
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
static IPopupService PopupService
Popup service for presenting application popups.
Definition: ServiceRef.cs:142
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
Base class for popup view models/>.
override Task OnAppearingAsync()
Method called when view is appearing on the screen.