/
hlebusheck
/
notes
Обзор
Документация
Войти
/
hlebusheck
/
notes
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
localization
Notes.Wpf/ConfigurationWindow.xaml.cs
142 строки
5 KB
Hlebusheck
Исправление ошибок
21 апр 2024, 22:18
21 апр 2024, 22:18
fc46370
Код
Авторство
О чём код?
using Microsoft.Win32; using Notes.Model; using Notes.Repository; using Notes.SqliteRepository; using Notes.Wpf.Controls; using System; using System.IO; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace Notes { public partial class ConfigurationWindow : Window { private readonly IMemoRepository _repository; private readonly string[] _localizations = [ "en-US", "ru-RU" ]; public ConfigurationWindow(IMemoRepository repository) { InitializeComponent(); _repository = repository; DbContext = (DataContext?)App.ServiceProvider.GetService(typeof(DataContext)) ?? throw new Exception("Db is null."); DataContext = this; } public DataContext DbContext { get; } private async void ImportClick(object sender, RoutedEventArgs e) => await Import(); private async void ExportClick(object sender, RoutedEventArgs e) => await Export(); private async Task Import() { var openFileDialog = new OpenFileDialog() { Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*", RestoreDirectory = true }; if (openFileDialog.ShowDialog() == true) { using var stream = File.OpenRead(openFileDialog.FileName); if (stream == null) return; Memo[]? newItems = await JsonSerializer.DeserializeAsync<Memo[]>(stream); stream.Close(); if (newItems == null || newItems.Length == 0) return; await _repository.Import(newItems); if (Owner is MainWindow mainWindow) mainWindow.Refresh(); CustomMessageBox.Show( this, $"Import completed.", "Informing", "\u0027", (SolidColorBrush)Application.Current.FindResource("InformationSolidBrush"), MessageBoxButton.OK); } } private async Task Export() { var saveFileDialog = new SaveFileDialog() { Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*", RestoreDirectory = true, FileName = "Exported" }; if (saveFileDialog.ShowDialog() == true) { var content = JsonSerializer.Serialize((await _repository.Get()).ToArray()); await File.WriteAllTextAsync(saveFileDialog.FileName, content); CustomMessageBox.Show( this, $"Export completed.", "Informing", "\u0030", (SolidColorBrush)Application.Current.FindResource("InformationSolidBrush"), MessageBoxButton.OK); } } private void LoadedWindow(object sender, RoutedEventArgs e) { var minButton = (Button)Template.FindName("MinButton", this); var maxButton = (Button)Template.FindName("MaxButton", this); minButton.Visibility = Visibility.Collapsed; maxButton.Visibility = Visibility.Collapsed; ResizeMode = ResizeMode.NoResize; SetSelectedLocalization(); } private void SelectLocalizationClick(object sender, RoutedEventArgs e) { if (sender is not RadioButton radioButton || radioButton.Tag is not string cultureName || !_localizations.Contains(cultureName)) return; App.Language = cultureName; ResourceDictionary dictionary = new() { Source = new Uri($"Resources/Localizations/Lang.{cultureName}.xaml", UriKind.Relative) }; ResourceDictionary? oldDictionary = Application.Current.Resources.MergedDictionaries .FirstOrDefault(dictionary => dictionary.Source != null && dictionary.Source.OriginalString.StartsWith("Resources/Localizations/Lang.")); if (oldDictionary != null) { int index = Application.Current.Resources.MergedDictionaries.IndexOf(oldDictionary); Application.Current.Resources.MergedDictionaries.Remove(oldDictionary); Application.Current.Resources.MergedDictionaries.Insert(index, dictionary); } else { Application.Current.Resources.MergedDictionaries.Add(dictionary); } SetSelectedLocalization(); } private void SetSelectedLocalization() { switch (App.Language) { case "ru-RU": ButtonRU.IsChecked = true; break; case "en-US": ButtonEN.IsChecked = true; break; } } } }