mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Implement add and delete entries and groups
New command bar Some layout changes Some refactoring
This commit is contained in:
@@ -1,19 +1,25 @@
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using ModernKeePass.Mappings;
|
||||
using ModernKeePassLib;
|
||||
using ModernKeePassLib.Security;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI;
|
||||
using Windows.UI.Text;
|
||||
using Windows.UI.Xaml;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class EntryVm
|
||||
{
|
||||
public GroupVm ParentGroup { get; }
|
||||
public PwEntry Entry => _pwEntry;
|
||||
public string Title
|
||||
{
|
||||
get { return GetEntryValue(PwDefs.TitleField); }
|
||||
get
|
||||
{
|
||||
var title = GetEntryValue(PwDefs.TitleField);
|
||||
return title == null ? "New entry" : title;
|
||||
}
|
||||
set { SetEntryValue(PwDefs.TitleField, value); }
|
||||
}
|
||||
public string UserName
|
||||
@@ -37,14 +43,17 @@ namespace ModernKeePass.ViewModels
|
||||
set { SetEntryValue(PwDefs.NotesField, value); }
|
||||
}
|
||||
|
||||
public SolidColorBrush BackgroundColor => CreateFromColor(_pwEntry.BackgroundColor, Colors.Transparent);
|
||||
public SolidColorBrush BackgroundColor => CreateFromColor(_pwEntry?.BackgroundColor, Colors.Transparent);
|
||||
|
||||
public SolidColorBrush ForegroundColor => CreateFromColor(_pwEntry.ForegroundColor, Colors.White);
|
||||
public SolidColorBrush ForegroundColor => CreateFromColor(_pwEntry?.ForegroundColor, Colors.White);
|
||||
|
||||
public FontWeight FontWeight => _pwEntry == null ? FontWeights.Bold : FontWeights.Normal;
|
||||
|
||||
public Symbol IconSymbol
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pwEntry == null) return Symbol.Add;
|
||||
var result = PwIconToSegoeMapping.GetSymbolFromIcon(_pwEntry.IconId);
|
||||
return result == Symbol.More ? Symbol.Permissions : result;
|
||||
}
|
||||
@@ -53,29 +62,35 @@ namespace ModernKeePass.ViewModels
|
||||
private readonly PwEntry _pwEntry;
|
||||
|
||||
public EntryVm() { }
|
||||
public EntryVm(PwEntry entry)
|
||||
public EntryVm(PwEntry entry, GroupVm parent)
|
||||
{
|
||||
_pwEntry = entry;
|
||||
ParentGroup = parent;
|
||||
}
|
||||
|
||||
public void RemoveEntry()
|
||||
{
|
||||
ParentGroup.RemoveEntry(this);
|
||||
}
|
||||
|
||||
private string GetEntryValue(string key)
|
||||
{
|
||||
return _pwEntry.Strings.GetSafe(key).ReadString();
|
||||
return _pwEntry?.Strings.GetSafe(key).ReadString();
|
||||
}
|
||||
|
||||
private void SetEntryValue(string key, string newValue)
|
||||
{
|
||||
_pwEntry.Strings.Set(key, new ProtectedString(true, newValue));
|
||||
_pwEntry?.Strings.Set(key, new ProtectedString(true, newValue));
|
||||
}
|
||||
|
||||
private SolidColorBrush CreateFromColor(System.Drawing.Color color, Windows.UI.Color defaultValue)
|
||||
private SolidColorBrush CreateFromColor(System.Drawing.Color? color, Windows.UI.Color defaultValue)
|
||||
{
|
||||
if (color == System.Drawing.Color.Empty) return new SolidColorBrush(defaultValue);
|
||||
if (!color.HasValue || color.Value == System.Drawing.Color.Empty) return new SolidColorBrush(defaultValue);
|
||||
return new SolidColorBrush(Windows.UI.Color.FromArgb(
|
||||
color.A,
|
||||
color.R,
|
||||
color.G,
|
||||
color.B));
|
||||
color.Value.A,
|
||||
color.Value.R,
|
||||
color.Value.G,
|
||||
color.Value.B));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using ModernKeePass.Mappings;
|
||||
using ModernKeePassLib;
|
||||
@@ -9,63 +10,76 @@ namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class GroupVm : INotifyPropertyChanged
|
||||
{
|
||||
private PwGroup _pwGroup;
|
||||
public GroupVm ParentGroup { get; }
|
||||
public ObservableCollection<EntryVm> Entries { get; set; } = new ObservableCollection<EntryVm>();
|
||||
public ObservableCollection<GroupVm> Groups { get; set; } = new ObservableCollection<GroupVm>();
|
||||
public string Name => _pwGroup == null ? "New group" : _pwGroup.Name;
|
||||
|
||||
public ObservableCollection<EntryVm> Entries { get; set; }
|
||||
public ObservableCollection<GroupVm> Groups { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int EntryCount => Entries.Count - 1;
|
||||
|
||||
public int EntryCount => Entries.Count;
|
||||
public int GroupCount => Groups.Count - 1;
|
||||
|
||||
public int GroupCount => Groups.Count;
|
||||
public Visibility DetailsVisibility => _pwGroup == null ? Visibility.Collapsed : Visibility.Visible;
|
||||
public Visibility NewVisibility => _pwGroup == null ? Visibility.Visible : Visibility.Collapsed;
|
||||
|
||||
public Symbol IconSymbol
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pwGroup == null) return Symbol.Add;
|
||||
var result = PwIconToSegoeMapping.GetSymbolFromIcon(_pwGroup.IconId);
|
||||
return result == Symbol.More ? Symbol.Folder : result;
|
||||
}
|
||||
}
|
||||
|
||||
public GroupVm()
|
||||
{
|
||||
Name = "GroupName";
|
||||
Entries = new ObservableCollection<EntryVm>();
|
||||
Groups = new ObservableCollection<GroupVm>();
|
||||
}
|
||||
public bool IsNotRoot => ParentGroup != null;
|
||||
|
||||
public GroupVm(PwGroup pwGroup)
|
||||
private readonly PwGroup _pwGroup;
|
||||
public GroupVm() {}
|
||||
|
||||
public GroupVm(PwGroup pwGroup, GroupVm parent)
|
||||
{
|
||||
_pwGroup = pwGroup;
|
||||
Name = pwGroup.Name;
|
||||
Entries = new ObservableCollection<EntryVm>(pwGroup.Entries.Select(e => new EntryVm(e)));
|
||||
//Entries.Insert(0, new EntryVm { Title = " + New entry" });
|
||||
Groups = new ObservableCollection<GroupVm>(pwGroup.Groups.Select(g => new GroupVm(g)));
|
||||
ParentGroup = parent;
|
||||
Entries = new ObservableCollection<EntryVm>(pwGroup.Entries.Select(e => new EntryVm(e, this)));
|
||||
Entries.Insert(0, new EntryVm ());
|
||||
//Entries.Add(new EntryVm { Title = " New entry" });
|
||||
Groups = new ObservableCollection<GroupVm>(pwGroup.Groups.Select(g => new GroupVm(g, this)));
|
||||
//Groups.Insert(0, new GroupVm { Name = " + New group" });
|
||||
Groups.Insert(0, new GroupVm ());
|
||||
}
|
||||
|
||||
public void CreateNewGroup(string title)
|
||||
public void CreateNewGroup()
|
||||
{
|
||||
var pwGroup = new PwGroup(true, true, title, PwIcon.Folder);
|
||||
var pwGroup = new PwGroup(true, true, "New group", PwIcon.Folder);
|
||||
_pwGroup.AddGroup(pwGroup, true);
|
||||
Groups.Add(new GroupVm(pwGroup));
|
||||
NotifyPropertyChanged("Groups");
|
||||
Groups.Add(new GroupVm(pwGroup, this));
|
||||
}
|
||||
|
||||
public void CreateNewEntry(string title)
|
||||
public void CreateNewEntry()
|
||||
{
|
||||
var pwEntry = new PwEntry(true, true);
|
||||
_pwGroup.AddEntry(pwEntry, true);
|
||||
Entries.Add(new EntryVm(pwEntry));
|
||||
NotifyPropertyChanged("Entries");
|
||||
Entries.Add(new EntryVm(pwEntry, this));
|
||||
}
|
||||
|
||||
public void RemoveGroup()
|
||||
{
|
||||
_pwGroup.ParentGroup.Groups.Remove(_pwGroup);
|
||||
ParentGroup.Groups.Remove(this);
|
||||
}
|
||||
|
||||
public void RemoveEntry(EntryVm entry)
|
||||
{
|
||||
_pwGroup.Entries.Remove(entry.Entry);
|
||||
Entries.Remove(entry);
|
||||
}
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public void NotifyPropertyChanged(string propertyName)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user