| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeAndLanguagePreferencesViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Collections.ObjectModel; |
| | | 10 | | using System.Globalization; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Reactive.Disposables; |
| | | 13 | | using MyNet.Globalization; |
| | | 14 | | using MyNet.Globalization.Culture; |
| | | 15 | | using MyNet.Globalization.DateTime; |
| | | 16 | | using MyNet.Observable.Behaviors.Metadata.Features.Events; |
| | | 17 | | using MyNet.UI.Resources; |
| | | 18 | | using MyNet.UI.ViewModels.Workspace; |
| | | 19 | | |
| | | 20 | | namespace MyNet.UI.ViewModels.Preferences; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Preferences page for culture and time zone settings. |
| | | 24 | | /// </summary> |
| | | 25 | | public sealed class TimeAndLanguagePreferencesViewModel : WorkspaceViewModel |
| | | 26 | | { |
| | | 27 | | private readonly IGlobalizationService _globalization; |
| | | 28 | | private bool _suppressApply; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="TimeAndLanguagePreferencesViewModel"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | public TimeAndLanguagePreferencesViewModel( |
| | | 34 | | IGlobalizationService globalization, |
| | | 35 | | IEnumerable<CultureInfo>? supportedCultures = null, |
| | | 36 | | IEnumerable<TimeZoneInfo>? supportedTimeZones = null) |
| | 0 | 37 | | : base(cultureService: globalization) |
| | | 38 | | { |
| | 0 | 39 | | ArgumentNullException.ThrowIfNull(globalization); |
| | | 40 | | |
| | 0 | 41 | | _globalization = globalization; |
| | | 42 | | |
| | 0 | 43 | | foreach (var culture in supportedCultures ?? [SupportedCultures.French, SupportedCultures.English]) |
| | 0 | 44 | | Cultures.Add(culture); |
| | | 45 | | |
| | 0 | 46 | | foreach (var timeZone in supportedTimeZones ?? TimeZoneInfo.GetSystemTimeZones()) |
| | 0 | 47 | | TimeZones.Add(timeZone); |
| | | 48 | | |
| | 0 | 49 | | _globalization.CultureChanged += OnGlobalizationCultureChanged; |
| | 0 | 50 | | _globalization.TimeZoneChanged += OnGlobalizationTimeZoneChanged; |
| | | 51 | | |
| | 0 | 52 | | Disposables.Add(Disposable.Create(() => |
| | 0 | 53 | | { |
| | 0 | 54 | | _globalization.CultureChanged -= OnGlobalizationCultureChanged; |
| | 0 | 55 | | _globalization.TimeZoneChanged -= OnGlobalizationTimeZoneChanged; |
| | 0 | 56 | | })); |
| | | 57 | | |
| | 0 | 58 | | RefreshFromService(); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets or sets the selected culture. |
| | | 63 | | /// </summary> |
| | | 64 | | public CultureInfo? SelectedCulture |
| | | 65 | | { |
| | | 66 | | get; |
| | | 67 | | set |
| | | 68 | | { |
| | 0 | 69 | | if (!SetProperty(ref field, value)) |
| | 0 | 70 | | return; |
| | | 71 | | |
| | 0 | 72 | | ApplyCulture(); |
| | 0 | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets or sets a value indicating whether the installed UI culture is used. |
| | | 78 | | /// </summary> |
| | | 79 | | public bool AutomaticCulture |
| | | 80 | | { |
| | | 81 | | get; |
| | | 82 | | set |
| | | 83 | | { |
| | 0 | 84 | | if (!SetProperty(ref field, value)) |
| | 0 | 85 | | return; |
| | | 86 | | |
| | 0 | 87 | | ApplyCulture(); |
| | 0 | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets or sets the selected time zone. |
| | | 93 | | /// </summary> |
| | | 94 | | public TimeZoneInfo? SelectedTimeZone |
| | | 95 | | { |
| | | 96 | | get; |
| | | 97 | | set |
| | | 98 | | { |
| | 0 | 99 | | if (!SetProperty(ref field, value)) |
| | 0 | 100 | | return; |
| | | 101 | | |
| | 0 | 102 | | ApplyTimeZone(); |
| | 0 | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Gets or sets a value indicating whether the local system time zone is used. |
| | | 108 | | /// </summary> |
| | | 109 | | public bool AutomaticTimeZone |
| | | 110 | | { |
| | | 111 | | get; |
| | | 112 | | set |
| | | 113 | | { |
| | 0 | 114 | | if (!SetProperty(ref field, value)) |
| | 0 | 115 | | return; |
| | | 116 | | |
| | 0 | 117 | | ApplyTimeZone(); |
| | 0 | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Gets the cultures available for selection. |
| | | 123 | | /// </summary> |
| | | 124 | | public ObservableCollection<CultureInfo> Cultures { get; } = []; |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Gets the time zones available for selection. |
| | | 128 | | /// </summary> |
| | | 129 | | public ObservableCollection<TimeZoneInfo> TimeZones { get; } = []; |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | 0 | 132 | | protected override string? CreateTitle(CultureInfo culture) => UiResources.TimeAndLanguage; |
| | | 133 | | |
| | | 134 | | /// <inheritdoc /> |
| | | 135 | | public override void OnEvent(CultureChangedEvent e) |
| | | 136 | | { |
| | 0 | 137 | | base.OnEvent(e); |
| | 0 | 138 | | RefreshCultureFromService(); |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | private void OnGlobalizationCultureChanged(object? sender, CultureChangedEventArgs e) |
| | 0 | 142 | | => RefreshCultureFromService(); |
| | | 143 | | |
| | | 144 | | private void OnGlobalizationTimeZoneChanged(object? sender, TimeZoneChangedEventArgs e) |
| | 0 | 145 | | => RefreshTimeZoneFromService(); |
| | | 146 | | |
| | | 147 | | private void RefreshFromService() |
| | | 148 | | { |
| | 0 | 149 | | _suppressApply = true; |
| | | 150 | | try |
| | | 151 | | { |
| | 0 | 152 | | RefreshCultureFromService(); |
| | 0 | 153 | | RefreshTimeZoneFromService(); |
| | 0 | 154 | | } |
| | | 155 | | finally |
| | | 156 | | { |
| | 0 | 157 | | _suppressApply = false; |
| | 0 | 158 | | } |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | private void RefreshCultureFromService() |
| | | 162 | | { |
| | 0 | 163 | | _suppressApply = true; |
| | | 164 | | try |
| | | 165 | | { |
| | 0 | 166 | | SelectedCulture = ResolveSupportedCulture(_globalization.CurrentCulture); |
| | 0 | 167 | | } |
| | | 168 | | finally |
| | | 169 | | { |
| | 0 | 170 | | _suppressApply = false; |
| | 0 | 171 | | } |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | private void RefreshTimeZoneFromService() |
| | | 175 | | { |
| | 0 | 176 | | _suppressApply = true; |
| | | 177 | | try |
| | | 178 | | { |
| | 0 | 179 | | SelectedTimeZone = _globalization.CurrentTimeZone; |
| | 0 | 180 | | } |
| | | 181 | | finally |
| | | 182 | | { |
| | 0 | 183 | | _suppressApply = false; |
| | 0 | 184 | | } |
| | 0 | 185 | | } |
| | | 186 | | |
| | | 187 | | private void ApplyCulture() |
| | | 188 | | { |
| | 0 | 189 | | if (_suppressApply) |
| | 0 | 190 | | return; |
| | | 191 | | |
| | 0 | 192 | | var cultureCode = SelectedCulture is null || AutomaticCulture |
| | 0 | 193 | | ? CultureInfo.InstalledUICulture.ToString() |
| | 0 | 194 | | : SelectedCulture.ToString(); |
| | | 195 | | |
| | 0 | 196 | | var current = _globalization.CurrentCulture; |
| | 0 | 197 | | if (cultureCode != current.Name && cultureCode != current.Parent.Name) |
| | 0 | 198 | | _globalization.SetCulture(cultureCode); |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | private void ApplyTimeZone() |
| | | 202 | | { |
| | 0 | 203 | | if (_suppressApply) |
| | 0 | 204 | | return; |
| | | 205 | | |
| | 0 | 206 | | var timeZone = SelectedTimeZone is null || AutomaticTimeZone |
| | 0 | 207 | | ? TimeZoneInfo.Local |
| | 0 | 208 | | : SelectedTimeZone; |
| | | 209 | | |
| | 0 | 210 | | if (!timeZone.Equals(_globalization.CurrentTimeZone)) |
| | 0 | 211 | | _globalization.SetTimeZone(timeZone); |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | private CultureInfo ResolveSupportedCulture(CultureInfo culture) |
| | | 215 | | { |
| | 0 | 216 | | while (!culture.Equals(CultureInfo.InvariantCulture)) |
| | | 217 | | { |
| | 0 | 218 | | if (Cultures.Contains(culture)) |
| | 0 | 219 | | return culture; |
| | | 220 | | |
| | 0 | 221 | | culture = culture.Parent; |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | return Cultures.FirstOrDefault() ?? _globalization.CurrentCulture; |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |