Feat/EditEntry

This commit is contained in:
Быстров Михаил Евгеньевич
2026-03-01 14:58:00 +03:00
parent 2aeb67d14b
commit f237f0fe8f
4 changed files with 217 additions and 170 deletions

View File

@@ -39,6 +39,16 @@ public class UnlockedRepositoryViewModel : ViewModelBase
OnPropertyChanged(nameof(Passwords));
}
public void UpdateEntry(PassStoreEntryPassword updatedEntry)
{
var root = passStore.GetRootDirectory() as PassStoreEntryGroup;
if (root == null) return;
root.DeleteEntry(updatedEntry.Id);
root.ChildEntries.Add(updatedEntry);
OnPropertyChanged(nameof(Passwords));
}
public void Save()
{
passStore.Save();

View File

@@ -11,6 +11,7 @@ namespace KeyKeeper.Views;
public partial class EntryEditWindow : Window
{
public PassStoreEntryPassword? EditedEntry;
private PassStoreEntryPassword? _originalEntry;
public EntryEditWindow()
{
@@ -21,6 +22,14 @@ public partial class EntryEditWindow : Window
}
}
public void SetEntry(PassStoreEntryPassword entry)
{
_originalEntry = entry;
EntryNameEdit.Text = entry.Name;
UsernameEdit.Text = entry.Username.Value;
PasswordEdit.Text = entry.Password.Value;
}
private void PasswordTextChanged(object? sender, TextChangedEventArgs e)
{
string password = PasswordEdit?.Text ?? "";
@@ -39,49 +48,34 @@ public partial class EntryEditWindow : Window
}
int strength = CalculatePasswordStrength(password);
double maxWidth = PasswordStrengthIndicator.Bounds.Width;
if (maxWidth <= 0) maxWidth = 200;
if (maxWidth <= 0) maxWidth = 200;
PasswordStrengthFill.Width = (strength / 100.0) * maxWidth;
if (strength < 20)
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Red);
}
else if (strength < 50)
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Orange);
}
else if (strength < 70)
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Gold);
}
else
{
PasswordStrengthFill.Background = new SolidColorBrush(Colors.Green);
}
}
private int CalculatePasswordStrength(string password)
{
int score = 0;
if (password.Length >= 8) score += 20;
if (password.Length >= 12) score += 20;
if (password.Length >= 16) score += 15;
if (Regex.IsMatch(password, @"\d")) score += 10;
if (Regex.IsMatch(password, @"[a-z]")) score += 15;
if (Regex.IsMatch(password, @"[A-Z]")) score += 15;
if (Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>\/?]")) score += 20;
var uniqueChars = new System.Collections.Generic.HashSet<char>(password).Count;
score += Math.Min(20, uniqueChars * 2);
return Math.Min(100, score);
}
@@ -96,22 +90,17 @@ public partial class EntryEditWindow : Window
string password = PasswordEdit?.Text ?? "";
if (string.IsNullOrEmpty(password)) return;
Guid id = _originalEntry?.Id ?? Guid.NewGuid();
DateTime created = DateTime.UtcNow;
EditedEntry = new PassStoreEntryPassword(
Guid.NewGuid(),
DateTime.UtcNow,
id,
created,
DateTime.UtcNow,
EntryIconType.DEFAULT,
name,
new LoginField()
{
Type = LOGIN_FIELD_USERNAME_ID,
Value = username
},
new LoginField()
{
Type = LOGIN_FIELD_PASSWORD_ID,
Value = password
},
new LoginField() { Type = LOGIN_FIELD_USERNAME_ID, Value = username },
new LoginField() { Type = LOGIN_FIELD_PASSWORD_ID, Value = password },
null
);
Close();

View File

@@ -11,113 +11,133 @@
Background="White"
x:DataType="vm:RepositoryWindowViewModel">
<Window.DataTemplates>
<DataTemplate DataType="{x:Type vm:UnlockedRepositoryViewModel}">
<Grid>
<!-- Синий левый край -->
<Border Width="200"
Background="#2328C4"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"/>
<StackPanel Margin="20" HorizontalAlignment="Left">
<!-- Надпись KeyKeeper -->
<TextBlock Text="KeyKeeper"
FontSize="32"
FontWeight="Bold"
HorizontalAlignment="Left"
Margin="0,0,0,20"/>
<Window.DataTemplates>
<DataTemplate DataType="{x:Type vm:UnlockedRepositoryViewModel}">
<Grid>
<!-- Синий левый край -->
<Border Width="200"
Background="#2328C4"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"/>
<StackPanel Margin="20" HorizontalAlignment="Left">
<!-- Надпись KeyKeeper -->
<TextBlock Text="KeyKeeper"
FontSize="32"
FontWeight="Bold"
HorizontalAlignment="Left"
Margin="0,0,0,20"/>
<!-- Рамочка -->
<Border BorderBrush="Gray"
BorderThickness="1"
CornerRadius="5"
Padding="20"
Background="#F5F5F5"
HorizontalAlignment="Left">
<!-- Рамочка -->
<Border BorderBrush="Gray"
BorderThickness="1"
CornerRadius="5"
Padding="20"
Background="#F5F5F5"
HorizontalAlignment="Left">
<StackPanel HorizontalAlignment="Left">
<Button Content="All Passwords"
Width="120"
Height="30"
HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel HorizontalAlignment="Left">
<Button Content="All Passwords"
Width="120"
Height="30"
HorizontalAlignment="Left"/>
</StackPanel>
</Border>
<!-- Save Passwords -->
<Button Content="Save Passwords"
Classes="accentSidebarButton"
Click="SaveButton_Click"
Height="30"
HorizontalAlignment="Left"
Margin="0,20,0,0"/>
</Border>
<!-- Save Passwords -->
<Button Content="Save Passwords"
Classes="accentSidebarButton"
Click="SaveButton_Click"
Height="30"
HorizontalAlignment="Left"
Margin="0,20,0,0"/>
<Button Content="New Entry"
Classes="accentSidebarButton"
Click="AddEntryButton_Click"
Height="30"
HorizontalAlignment="Left"
Margin="0,20,0,0"/>
</StackPanel>
<ListBox Width="580"
Margin="210 10 10 10"
ItemsSource="{Binding Passwords}"
Background="Transparent">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<!-- New Entry -->
<Button Content="New Entry"
Classes="accentSidebarButton"
Click="AddEntryButton_Click"
Height="30"
HorizontalAlignment="Left"
Margin="0,20,0,0"/>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Background="Transparent" DoubleTapped="Entry_DoubleTapped">
<StackPanel Width="100"
Margin="10"
HorizontalAlignment="Center">
<Svg Path="{Binding IconPath}" Width="48" Height="48"
/>
<TextBlock Text="{Binding Name}"
HorizontalAlignment="Center"
Foreground="Black" />
<TextBlock Text="{Binding Username.Value}"
Foreground="#666"
HorizontalAlignment="Center" />
</StackPanel>
<Border.ContextMenu>
<ContextMenu>
<MenuItem Name="entryCtxMenuCopyUsername" Header="Copy username" Click="EntryContextMenuItem_Click"/>
<MenuItem Name="entryCtxMenuCopyPassword" Header="Copy password" Click="EntryContextMenuItem_Click"/>
<MenuItem Name="entryCtxMenuDelete" Header="Delete" Click="EntryContextMenuItem_Click"/>
</ContextMenu>
</Border.ContextMenu>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<kkp:ToastNotificationHost x:Name="NotificationHost" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="20" Duration="0:0:2" />
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:LockedRepositoryViewModel}">
<StackPanel Margin="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Spacing="10">
<TextBlock Text="Enter credentials to unlock"
Foreground="#2328C4"
FontSize="32" />
<!-- Edit Selected Entry -->
<Button Content="Edit Selected Entry"
Classes="accentSidebarButton"
Click="EditEntryButton_Click"
Height="30"
HorizontalAlignment="Left"
Margin="0,20,0,0"/>
</StackPanel>
<TextBox x:Name="UnlockPasswordEdit"
Text="{Binding UnlockPassword, Mode=TwoWay}"
PasswordChar="*"
Width="450" />
<!-- ListBox с паролями -->
<ListBox x:Name="PasswordsListBox"
Width="580"
Margin="210 10 10 10"
ItemsSource="{Binding Passwords}"
Background="Transparent"
SelectionMode="Single">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<Button x:Name="UnlockButton"
Command="{Binding TryUnlock}"
HorizontalAlignment="Center"
Content="Unlock!" />
</StackPanel>
</DataTemplate>
</Window.DataTemplates>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Background="Transparent" DoubleTapped="Entry_DoubleTapped">
<StackPanel Width="100"
Margin="10"
HorizontalAlignment="Center">
<Svg Path="{Binding IconPath}" Width="48" Height="48"/>
<TextBlock Text="{Binding Name}"
HorizontalAlignment="Center"
Foreground="Black" />
<TextBlock Text="{Binding Username.Value}"
Foreground="#666"
HorizontalAlignment="Center" />
</StackPanel>
<Border.ContextMenu>
<ContextMenu>
<MenuItem Name="entryCtxMenuCopyUsername" Header="Copy username" Click="EntryContextMenuItem_Click"/>
<MenuItem Name="entryCtxMenuCopyPassword" Header="Copy password" Click="EntryContextMenuItem_Click"/>
<!-- Новый пункт меню "Edit" -->
<MenuItem Name="entryCtxMenuEdit" Header="Edit" Click="EntryContextMenuItem_Click"/>
<MenuItem Name="entryCtxMenuDelete" Header="Delete" Click="EntryContextMenuItem_Click"/>
</ContextMenu>
</Border.ContextMenu>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ContentControl Content="{Binding CurrentPage}"/>
</Window>
<kkp:ToastNotificationHost x:Name="NotificationHost"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="20"
Duration="0:0:2" />
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:LockedRepositoryViewModel}">
<StackPanel Margin="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Spacing="10">
<TextBlock Text="Enter credentials to unlock"
Foreground="#2328C4"
FontSize="32" />
<TextBox x:Name="UnlockPasswordEdit"
Text="{Binding UnlockPassword, Mode=TwoWay}"
PasswordChar="*"
Width="450" />
<Button x:Name="UnlockButton"
Command="{Binding TryUnlock}"
HorizontalAlignment="Center"
Content="Unlock!" />
</StackPanel>
</DataTemplate>
</Window.DataTemplates>
<ContentControl Content="{Binding CurrentPage}"/>
</Window>

View File

@@ -10,7 +10,7 @@ using Avalonia.Controls.Presenters;
namespace KeyKeeper.Views;
public partial class RepositoryWindow: Window
public partial class RepositoryWindow : Window
{
public RepositoryWindow(RepositoryWindowViewModel model)
{
@@ -21,7 +21,7 @@ public partial class RepositoryWindow: Window
await new ErrorDialog(message).ShowDialog(this);
};
}
protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
@@ -39,6 +39,36 @@ public partial class RepositoryWindow: Window
}
}
private async void EditEntryButton_Click(object sender, RoutedEventArgs args)
{
if (DataContext is RepositoryWindowViewModel vm_ && vm_.CurrentPage is UnlockedRepositoryViewModel vm)
{
var listBox = this.FindControlRecursive<ListBox>("PasswordsListBox");
if (listBox == null)
{
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("ListBox not found");
return;
}
var selectedEntry = listBox.SelectedItem as PassStoreEntryPassword;
if (selectedEntry == null)
{
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("No entry selected");
return;
}
EntryEditWindow dialog = new();
dialog.SetEntry(selectedEntry);
await dialog.ShowDialog(this);
if (dialog.EditedEntry != null)
{
vm.UpdateEntry(dialog.EditedEntry);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Entry updated");
}
}
}
private void SaveButton_Click(object sender, RoutedEventArgs args)
{
if (DataContext is RepositoryWindowViewModel vm && vm.CurrentPage is UnlockedRepositoryViewModel pageVm)
@@ -49,40 +79,46 @@ public partial class RepositoryWindow: Window
private void Entry_DoubleTapped(object sender, TappedEventArgs args)
{
if (args.Source is StyledElement s)
if (args.Source is StyledElement s && s.DataContext is PassStoreEntryPassword pwd)
{
if (s.DataContext is PassStoreEntryPassword pwd)
Clipboard!.SetTextAsync(pwd.Password.Value);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Password copied to clipboard");
}
}
private async void EntryContextMenuItem_Click(object sender, RoutedEventArgs args)
{
if (args.Source is StyledElement s && s.DataContext is PassStoreEntryPassword pwd)
{
if (s.Name == "entryCtxMenuCopyUsername")
{
Clipboard!.SetTextAsync(pwd.Username.Value);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Username copied to clipboard");
}
else if (s.Name == "entryCtxMenuCopyPassword")
{
Clipboard!.SetTextAsync(pwd.Password.Value);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Password copied to clipboard");
}
}
}
private void EntryContextMenuItem_Click(object sender, RoutedEventArgs args) {
if (args.Source is StyledElement s)
{
if (s.DataContext is PassStoreEntryPassword pwd)
else if (s.Name == "entryCtxMenuEdit")
{
if (s.Name == "entryCtxMenuCopyUsername")
if (DataContext is RepositoryWindowViewModel vm && vm.CurrentPage is UnlockedRepositoryViewModel pageVm)
{
Clipboard!.SetTextAsync(pwd.Username.Value);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Username copied to clipboard");
}
else if (s.Name == "entryCtxMenuCopyPassword")
{
Clipboard!.SetTextAsync(pwd.Password.Value);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Password copied to clipboard");
}
else if (s.Name == "entryCtxMenuDelete")
{
if (s.DataContext is PassStoreEntryPassword entry)
EntryEditWindow dialog = new();
dialog.SetEntry(pwd);
await dialog.ShowDialog(this);
if (dialog.EditedEntry != null)
{
if (DataContext is RepositoryWindowViewModel vm && vm.CurrentPage is UnlockedRepositoryViewModel pageVm)
{
pageVm.DeleteEntry(entry.Id);
}
pageVm.UpdateEntry(dialog.EditedEntry);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Entry updated");
}
}
}
else if (s.Name == "entryCtxMenuDelete")
{
if (DataContext is RepositoryWindowViewModel vm && vm.CurrentPage is UnlockedRepositoryViewModel pageVm)
{
pageVm.DeleteEntry(pwd.Id);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Entry deleted");
}
}
@@ -99,31 +135,23 @@ public static class VisualTreeExtensions
private static T? FindControlRecursiveInternal<T>(Visual parent, string name, int depth) where T : Visual
{
if (parent == null)
return null;
if (parent is T t && parent.Name == name)
return t;
if (parent == null) return null;
if (parent is T t && parent.Name == name) return t;
foreach (var child in parent.GetVisualChildren())
{
if (child == null)
continue;
if (child == null) continue;
var result = FindControlRecursiveInternal<T>(child, name, depth + 1);
if (result != null)
return result;
if (result != null) return result;
}
// Also check logical children if they're not in visual tree
if (parent is ContentPresenter contentPresenter)
{
var content = contentPresenter.Content as Visual;
if (content != null)
{
var result = FindControlRecursiveInternal<T>(content, name, depth + 1);
if (result != null)
return result;
if (result != null) return result;
}
}