From 8cb33b89fb7e4a708aa4c4ab8ab553f444df0c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Fri, 17 Apr 2020 22:53:19 +0200 Subject: [PATCH] switched to yaml as filee format --- App.xaml | 2 +- App.xaml.cs | 2 +- MainWindow.xaml | 7 +++- MainWindow.xaml.cs | 3 +- WindowViewModel.cs => MainWindowViewModel.cs | 23 ++++++----- ModernWpfPlayground.csproj | 1 + MvvmStuff/BaseViewModel.cs | 15 ++++++-- MvvmStuff/DeserializationExtension.cs | 20 ++++++++++ MvvmStuff/PropertyInfoExtension.cs | 40 -------------------- 9 files changed, 54 insertions(+), 59 deletions(-) rename WindowViewModel.cs => MainWindowViewModel.cs (83%) create mode 100644 MvvmStuff/DeserializationExtension.cs delete mode 100644 MvvmStuff/PropertyInfoExtension.cs diff --git a/App.xaml b/App.xaml index ecf826e..ae0aa6d 100644 --- a/App.xaml +++ b/App.xaml @@ -14,4 +14,4 @@ - + \ No newline at end of file diff --git a/App.xaml.cs b/App.xaml.cs index a5b3a32..0ffe5e5 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -11,7 +11,7 @@ namespace ModernWpfPlayground protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.Register(); - containerRegistry.Register(); + containerRegistry.Register(); } protected override Window CreateShell() diff --git a/MainWindow.xaml b/MainWindow.xaml index e00a730..39a0939 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -1,17 +1,20 @@ - public partial class MainWindow { - public MainWindow(WindowViewModel viewModel) + public MainWindow() { InitializeComponent(); - DataContext = viewModel; } } } diff --git a/WindowViewModel.cs b/MainWindowViewModel.cs similarity index 83% rename from WindowViewModel.cs rename to MainWindowViewModel.cs index c17f621..88c1654 100644 --- a/WindowViewModel.cs +++ b/MainWindowViewModel.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.IO; -using System.Text.Json; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; @@ -8,16 +7,19 @@ using Microsoft.Win32; using ModernWpf; using ModernWpfPlayground.MvvmStuff; using Prism.Commands; +using YamlDotNet.Serialization; namespace ModernWpfPlayground { - public class WindowViewModel : BaseViewModel + public class MainWindowViewModel : BaseViewModel { private const string AppName = "TaBEA 3.0.0"; private string? _path; private string _title = AppName; + private readonly ISerializer _serializer; + private readonly IDeserializer _deserializer; - public WindowViewModel() + public MainWindowViewModel() { ShowDialogCommand = new DelegateCommand(async () => await ShowDialogAsync().ConfigureAwait(false)); CloseCommand = new DelegateCommand(() => Application.Current.Shutdown()); @@ -28,13 +30,15 @@ namespace ModernWpfPlayground ResetViewModel(); Path = null; }); + _serializer = new SerializerBuilder().Build(); + _deserializer = new DeserializerBuilder().Build(); } public string? Path { get => _path; private set => SetProperty(ref _path, value, - () => Title = value != null ? $"{System.IO.Path.GetFileName(value)} - {AppName}" : AppName); + () => Title = value != null ? $"{System.IO.Path.GetFileName(value)} - {AppName}" : AppName); } public string Title @@ -114,10 +118,10 @@ namespace ModernWpfPlayground private void SaveViewModel() { - var contents = JsonSerializer.Serialize(Values); + var contents = _serializer.Serialize(Values); if (Path == null) { - var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.json"}; + var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.yaml"}; var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); if (result != true) return; Path = saveFileDialog.FileName; @@ -128,15 +132,16 @@ namespace ModernWpfPlayground protected override IEnumerable<(string key, object? value)> GetViewModelItems() { - var openFileDialog = new OpenFileDialog {AddExtension = true, DefaultExt = "*.json"}; + var openFileDialog = new OpenFileDialog {AddExtension = true, DefaultExt = "*.yaml"}; var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); if (result != true) yield break; var contents = File.ReadAllText(Path = openFileDialog.FileName); - var obj = JsonSerializer.Deserialize>(contents); + + var obj = _deserializer.Deserialize>(contents); foreach (var (key, value) in obj) { - yield return (key, value.Convert(ObjectAccessor[key].GetType())); + yield return (key, DeserializationExtension.Convert(value, ObjectAccessor[key].GetType())); } } } diff --git a/ModernWpfPlayground.csproj b/ModernWpfPlayground.csproj index d43d5c4..4daaa0b 100644 --- a/ModernWpfPlayground.csproj +++ b/ModernWpfPlayground.csproj @@ -12,6 +12,7 @@ + diff --git a/MvvmStuff/BaseViewModel.cs b/MvvmStuff/BaseViewModel.cs index cddeba1..d8f2b3a 100644 --- a/MvvmStuff/BaseViewModel.cs +++ b/MvvmStuff/BaseViewModel.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.CompilerServices; using FastMember; using Prism.Mvvm; @@ -21,7 +22,7 @@ namespace ModernWpfPlayground.MvvmStuff protected bool SetProperty(T value, Action? onChanged = null, [CallerMemberName] string? propertyName = null) { - if(propertyName == null) throw new ArgumentNullException(nameof(propertyName)); + if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); if (_values.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false; _values[propertyName] = value!; @@ -33,12 +34,14 @@ namespace ModernWpfPlayground.MvvmStuff protected T GetProperty(T defaultValue = default, [CallerMemberName] string? propertyName = null) { if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); - return Values.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue; + return Values.TryGetValue(propertyName, out var obj) ? (T)obj : defaultValue; } - protected void ResetViewModel() + protected void ResetViewModel(Func? predicate = null) { - foreach (var key in Values.Keys) + IEnumerable keys = _values.Keys; + if (predicate != null) keys = keys.Where(predicate); + foreach (var key in keys) { _values.Remove(key); RaisePropertyChanged(key); @@ -49,10 +52,14 @@ namespace ModernWpfPlayground.MvvmStuff protected void LoadViewModel() { + var keysFromFile = new SortedSet(); foreach (var (key, value) in GetViewModelItems()) { + keysFromFile.Add(key); ObjectAccessor[key] = value; } + + ResetViewModel(x => !keysFromFile.Contains(x)); } } } \ No newline at end of file diff --git a/MvvmStuff/DeserializationExtension.cs b/MvvmStuff/DeserializationExtension.cs new file mode 100644 index 0000000..6aa1627 --- /dev/null +++ b/MvvmStuff/DeserializationExtension.cs @@ -0,0 +1,20 @@ +using System; +using System.Globalization; + +namespace ModernWpfPlayground.MvvmStuff +{ + public static class DeserializationExtension + { + public static object? Convert(object? value, Type propertyType) + { + if (value is null) return Activator.CreateInstance(propertyType); + + if (propertyType.IsEnum && value is string s) + { + return Enum.Parse(propertyType, s); + } + + return System.Convert.ChangeType(value, propertyType, CultureInfo.InvariantCulture); + } + } +} \ No newline at end of file diff --git a/MvvmStuff/PropertyInfoExtension.cs b/MvvmStuff/PropertyInfoExtension.cs deleted file mode 100644 index 36a012f..0000000 --- a/MvvmStuff/PropertyInfoExtension.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Text.Json; - -namespace ModernWpfPlayground.MvvmStuff -{ - public static class PropertyInfoExtension - { - public static object? Convert(this JsonElement value, Type propertyType) - { - if (propertyType == null) return default; - - if (propertyType == typeof(double)) - { - return value.GetDouble(); - } - - if (propertyType == typeof(bool)) - { - return value.GetBoolean(); - } - - if (propertyType == typeof(int)) - { - return value.GetInt32(); - } - - if (propertyType.IsEnum) - { - return Enum.ToObject(propertyType, value.GetInt32()); - } - - if (propertyType == typeof(string)) - { - return value.GetString(); - } - - return default; - } - } -} \ No newline at end of file