From 9603c1ff01ce212706b77faae947b7d60b211fee Mon Sep 17 00:00:00 2001 From: Geoffroy BONNEVILLE Date: Tue, 14 Apr 2020 17:49:29 +0200 Subject: [PATCH] Icons work again Colors work again --- .../Entry/Models/EntryVm.cs | 2 +- ModernKeePass.Domain/Enums/EntryFieldName.cs | 2 + .../KeePass/KeePassDatabaseClient.cs | 7 +++ .../Converters/ColorToBrushConverter.cs | 13 ++--- .../Converters/IconToSymbolConverter.cs | 11 ++-- ModernKeePass/Extensions/ColorExtensions.cs | 18 +++++++ .../TextBoxWithButtonStyle.xaml | 16 ++++++ ModernKeePass/ViewModels/EntryDetailVm.cs | 51 ++++++++----------- ModernKeePass/ViewModels/GroupDetailVm.cs | 7 +-- ModernKeePass/Views/EntryDetailPage.xaml | 14 ++--- ModernKeePass/Views/GroupDetailPage.xaml | 8 +-- .../UserControls/SymbolPickerUserControl.xaml | 12 +++-- .../UserControls/TopMenuUserControl.xaml | 2 +- ModernKeePass/Win81App.csproj | 1 + 14 files changed, 103 insertions(+), 61 deletions(-) create mode 100644 ModernKeePass/Extensions/ColorExtensions.cs diff --git a/ModernKeePass.Application/Entry/Models/EntryVm.cs b/ModernKeePass.Application/Entry/Models/EntryVm.cs index 36d7d5b..85aafae 100644 --- a/ModernKeePass.Application/Entry/Models/EntryVm.cs +++ b/ModernKeePass.Application/Entry/Models/EntryVm.cs @@ -50,7 +50,7 @@ namespace ModernKeePass.Application.Entry.Models .ForMember(d => d.HasExpirationDate, opts => opts.MapFrom(s => s.HasExpirationDate)) .ForMember(d => d.ExpirationDate, opts => opts.MapFrom(s => s.ExpirationDate)) .ForMember(d => d.ModificationDate, opts => opts.MapFrom(s => s.LastModificationDate)) - .ForMember(d => d.Icon, opts => opts.MapFrom(s => s.Icon)) + .ForMember(d => d.Icon, opts => opts.MapFrom(s => s.HasExpirationDate && s.ExpirationDate < DateTimeOffset.Now ? Icon.ReportHacked : s.Icon)) .ForMember(d => d.ForegroundColor, opts => opts.MapFrom(s => s.ForegroundColor)) .ForMember(d => d.BackgroundColor, opts => opts.MapFrom(s => s.BackgroundColor)); } diff --git a/ModernKeePass.Domain/Enums/EntryFieldName.cs b/ModernKeePass.Domain/Enums/EntryFieldName.cs index eeec7e7..e94a425 100644 --- a/ModernKeePass.Domain/Enums/EntryFieldName.cs +++ b/ModernKeePass.Domain/Enums/EntryFieldName.cs @@ -10,5 +10,7 @@ public const string Icon = nameof(Icon); public const string ExpirationDate = nameof(ExpirationDate); public const string HasExpirationDate = nameof(HasExpirationDate); + public const string BackgroundColor = nameof(BackgroundColor); + public const string ForegroundColor = nameof(ForegroundColor); } } \ No newline at end of file diff --git a/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs b/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs index c8932dc..8af147c 100644 --- a/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs +++ b/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs @@ -1,4 +1,5 @@ using System; +using System.Drawing; using System.Linq; using System.Threading.Tasks; using AutoMapper; @@ -242,6 +243,12 @@ namespace ModernKeePass.Infrastructure.KeePass case EntryFieldName.Icon: pwEntry.IconId = IconMapper.MapIconToPwIcon((Icon)fieldValue); break; + case EntryFieldName.BackgroundColor: + pwEntry.BackgroundColor = (Color)fieldValue; + break; + case EntryFieldName.ForegroundColor: + pwEntry.ForegroundColor = (Color)fieldValue; + break; } } diff --git a/ModernKeePass/Converters/ColorToBrushConverter.cs b/ModernKeePass/Converters/ColorToBrushConverter.cs index f042e50..b58f49c 100644 --- a/ModernKeePass/Converters/ColorToBrushConverter.cs +++ b/ModernKeePass/Converters/ColorToBrushConverter.cs @@ -1,4 +1,5 @@ -using System; +using ModernKeePass.Extensions; +using System; using System.Drawing; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media; @@ -9,19 +10,15 @@ namespace ModernKeePass.Converters { public object Convert(object value, Type targetType, object parameter, string language) { - var color = value is Color ? (Color?) value : Color.Empty; + var color = value as Color? ?? Color.Empty; if (color == Color.Empty && parameter is SolidColorBrush) return (SolidColorBrush) parameter; - return new SolidColorBrush(Windows.UI.Color.FromArgb( - color.Value.A, - color.Value.R, - color.Value.G, - color.Value.B)); + return color.ToSolidColorBrush(); } public object ConvertBack(object value, Type targetType, object parameter, string language) { var brush = value as SolidColorBrush; - return brush == null ? new Color() : Color.FromArgb(brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B); + return brush?.ToColor() ?? new Color(); } } } \ No newline at end of file diff --git a/ModernKeePass/Converters/IconToSymbolConverter.cs b/ModernKeePass/Converters/IconToSymbolConverter.cs index d2b3885..16b50bd 100644 --- a/ModernKeePass/Converters/IconToSymbolConverter.cs +++ b/ModernKeePass/Converters/IconToSymbolConverter.cs @@ -9,7 +9,8 @@ namespace ModernKeePass.Converters { public object Convert(object value, Type targetType, object parameter, string language) { - var icon = (Icon)value; + return Enum.Parse(typeof(Symbol), value.ToString()); + /*var icon = (Icon)value; switch (icon) { case Icon.Delete: return Symbol.Delete; @@ -62,12 +63,12 @@ namespace ModernKeePass.Converters case Icon.Stop: return Symbol.Stop; default: throw new ArgumentOutOfRangeException(); - } + }*/ } public object ConvertBack(object value, Type targetType, object parameter, string language) { - var symbol = (Symbol)value; + /*var symbol = (Symbol)value; var defaultIcon = parameter != null ? int.Parse(parameter as string) : -1; switch (symbol) { @@ -120,7 +121,9 @@ namespace ModernKeePass.Converters case Symbol.ReportHacked: return Icon.ReportHacked; case Symbol.Stop: return Icon.Stop; default: return defaultIcon; - } + }*/ + + return Enum.Parse(typeof(Icon), value.ToString()); } } } \ No newline at end of file diff --git a/ModernKeePass/Extensions/ColorExtensions.cs b/ModernKeePass/Extensions/ColorExtensions.cs new file mode 100644 index 0000000..4229618 --- /dev/null +++ b/ModernKeePass/Extensions/ColorExtensions.cs @@ -0,0 +1,18 @@ +using System.Drawing; +using Windows.UI.Xaml.Media; + +namespace ModernKeePass.Extensions +{ + public static class ColorExtensions + { + public static Color ToColor(this SolidColorBrush brush) + { + return Color.FromArgb(brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B); + } + + public static SolidColorBrush ToSolidColorBrush(this Color color) + { + return new SolidColorBrush(Windows.UI.Color.FromArgb(color.A, color.R, color.G, color.B)); + } + } +} \ No newline at end of file diff --git a/ModernKeePass/ResourceDictionaries/TextBoxWithButtonStyle.xaml b/ModernKeePass/ResourceDictionaries/TextBoxWithButtonStyle.xaml index 3ba21f0..fa7c176 100644 --- a/ModernKeePass/ResourceDictionaries/TextBoxWithButtonStyle.xaml +++ b/ModernKeePass/ResourceDictionaries/TextBoxWithButtonStyle.xaml @@ -244,6 +244,22 @@ + diff --git a/ModernKeePass/ViewModels/EntryDetailVm.cs b/ModernKeePass/ViewModels/EntryDetailVm.cs index 41c1ef8..2f3ee01 100644 --- a/ModernKeePass/ViewModels/EntryDetailVm.cs +++ b/ModernKeePass/ViewModels/EntryDetailVm.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.Threading.Tasks; using System.Windows.Input; using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Media; using MediatR; using Microsoft.Extensions.DependencyInjection; using ModernKeePass.Application.Database.Commands.SaveDatabase; @@ -23,6 +23,7 @@ using ModernKeePass.Domain.Enums; using ModernKeePass.Interfaces; using ModernKeePass.Application.Group.Models; using ModernKeePass.Domain.AOP; +using ModernKeePass.Extensions; namespace ModernKeePass.ViewModels { @@ -64,8 +65,8 @@ namespace ModernKeePass.ViewModels get { return _entry.Title; } set { - SetFieldValue(nameof(Title), value).Wait(); _entry.Title = value; + SetFieldValue(nameof(Title), value).Wait(); } } @@ -80,8 +81,8 @@ namespace ModernKeePass.ViewModels get { return _entry.Password; } set { - SetFieldValue(nameof(Password), value).Wait(); _entry.Password = value; + SetFieldValue(nameof(Password), value).Wait(); OnPropertyChanged(); OnPropertyChanged(nameof(PasswordComplexityIndicator)); } @@ -92,8 +93,8 @@ namespace ModernKeePass.ViewModels get { return _entry.Url?.ToString(); } set { - SetFieldValue(nameof(Url), value).Wait(); _entry.Url = new Uri(value); + SetFieldValue(nameof(Url), value).Wait(); } } @@ -102,22 +103,18 @@ namespace ModernKeePass.ViewModels get { return _entry.Notes; } set { - SetFieldValue(nameof(Notes), value).Wait(); _entry.Notes = value; + SetFieldValue(nameof(Notes), value).Wait(); } } public Symbol Icon { - get - { - if (HasExpired) return Symbol.ReportHacked; - return (Symbol) _entry.Icon; - } + get { return (Symbol)Enum.Parse(typeof(Symbol), _entry.Icon.ToString()); } set { - SetFieldValue(nameof(Icon), value).Wait(); - _entry.Icon = (Icon)value; + _entry.Icon = (Icon)Enum.Parse(typeof(Icon), value.ToString()); + SetFieldValue(nameof(Icon), _entry.Icon).Wait(); } } @@ -128,8 +125,8 @@ namespace ModernKeePass.ViewModels { if (!HasExpirationDate) return; - SetFieldValue("ExpirationDate", value).Wait(); _entry.ExpirationDate = value.Date; + SetFieldValue("ExpirationDate", _entry.ExpirationDate).Wait(); } } @@ -140,8 +137,8 @@ namespace ModernKeePass.ViewModels { if (!HasExpirationDate) return; - SetFieldValue("ExpirationDate", value).Wait(); _entry.ExpirationDate = _entry.ExpirationDate.Date.Add(value); + SetFieldValue("ExpirationDate", _entry.ExpirationDate).Wait(); } } @@ -150,35 +147,29 @@ namespace ModernKeePass.ViewModels get { return _entry.HasExpirationDate; } set { - SetFieldValue(nameof(HasExpirationDate), value).Wait(); _entry.HasExpirationDate = value; - OnPropertyChanged(); + SetFieldValue(nameof(HasExpirationDate), value).Wait(); + OnPropertyChanged(nameof(HasExpirationDate)); } } - public Color? BackgroundColor + public SolidColorBrush BackgroundColor { - get { return _entry?.BackgroundColor; } + get { return _entry?.BackgroundColor.ToSolidColorBrush(); } set { - if (value != null) - { - SetFieldValue(nameof(BackgroundColor), value).Wait(); - _entry.BackgroundColor = (Color)value; - } + _entry.BackgroundColor = value.ToColor(); + SetFieldValue(nameof(BackgroundColor), _entry.BackgroundColor).Wait(); } } - public Color? ForegroundColor + public SolidColorBrush ForegroundColor { - get { return _entry?.ForegroundColor; } + get { return _entry?.ForegroundColor.ToSolidColorBrush(); } set { - if (value != null) - { - SetFieldValue(nameof(ForegroundColor), value).Wait(); - _entry.ForegroundColor = (Color)value; - } + _entry.ForegroundColor = value.ToColor(); + SetFieldValue(nameof(ForegroundColor), _entry.ForegroundColor).Wait(); } } public IEnumerable History { get; } diff --git a/ModernKeePass/ViewModels/GroupDetailVm.cs b/ModernKeePass/ViewModels/GroupDetailVm.cs index d665928..f59c780 100644 --- a/ModernKeePass/ViewModels/GroupDetailVm.cs +++ b/ModernKeePass/ViewModels/GroupDetailVm.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; @@ -67,8 +68,8 @@ namespace ModernKeePass.ViewModels public Symbol Icon { - get { return (Symbol) _group.Icon; } - set { _group.Icon = (Icon)value; } + get { return (Symbol) Enum.Parse(typeof(Symbol), _group.Icon.ToString()); } + set { _group.Icon = (Icon) Enum.Parse(typeof(Icon), value.ToString()); } } public bool IsEditMode diff --git a/ModernKeePass/Views/EntryDetailPage.xaml b/ModernKeePass/Views/EntryDetailPage.xaml index a1c2e31..edcc334 100644 --- a/ModernKeePass/Views/EntryDetailPage.xaml +++ b/ModernKeePass/Views/EntryDetailPage.xaml @@ -19,7 +19,6 @@ -