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)); 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() public void Save()
{ {
passStore.Save(); passStore.Save();

View File

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

View File

@@ -51,17 +51,30 @@
HorizontalAlignment="Left" HorizontalAlignment="Left"
Margin="0,20,0,0"/> Margin="0,20,0,0"/>
<!-- New Entry -->
<Button Content="New Entry" <Button Content="New Entry"
Classes="accentSidebarButton" Classes="accentSidebarButton"
Click="AddEntryButton_Click" Click="AddEntryButton_Click"
Height="30" Height="30"
HorizontalAlignment="Left" HorizontalAlignment="Left"
Margin="0,20,0,0"/>
<!-- Edit Selected Entry -->
<Button Content="Edit Selected Entry"
Classes="accentSidebarButton"
Click="EditEntryButton_Click"
Height="30"
HorizontalAlignment="Left"
Margin="0,20,0,0"/> Margin="0,20,0,0"/>
</StackPanel> </StackPanel>
<ListBox Width="580"
<!-- ListBox с паролями -->
<ListBox x:Name="PasswordsListBox"
Width="580"
Margin="210 10 10 10" Margin="210 10 10 10"
ItemsSource="{Binding Passwords}" ItemsSource="{Binding Passwords}"
Background="Transparent"> Background="Transparent"
SelectionMode="Single">
<ListBox.ItemsPanel> <ListBox.ItemsPanel>
<ItemsPanelTemplate> <ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" /> <WrapPanel Orientation="Horizontal" />
@@ -74,8 +87,7 @@
<StackPanel Width="100" <StackPanel Width="100"
Margin="10" Margin="10"
HorizontalAlignment="Center"> HorizontalAlignment="Center">
<Svg Path="{Binding IconPath}" Width="48" Height="48" <Svg Path="{Binding IconPath}" Width="48" Height="48"/>
/>
<TextBlock Text="{Binding Name}" <TextBlock Text="{Binding Name}"
HorizontalAlignment="Center" HorizontalAlignment="Center"
Foreground="Black" /> Foreground="Black" />
@@ -87,6 +99,8 @@
<ContextMenu> <ContextMenu>
<MenuItem Name="entryCtxMenuCopyUsername" Header="Copy username" Click="EntryContextMenuItem_Click"/> <MenuItem Name="entryCtxMenuCopyUsername" Header="Copy username" Click="EntryContextMenuItem_Click"/>
<MenuItem Name="entryCtxMenuCopyPassword" Header="Copy password" 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"/> <MenuItem Name="entryCtxMenuDelete" Header="Delete" Click="EntryContextMenuItem_Click"/>
</ContextMenu> </ContextMenu>
</Border.ContextMenu> </Border.ContextMenu>
@@ -94,9 +108,15 @@
</DataTemplate> </DataTemplate>
</ListBox.ItemTemplate> </ListBox.ItemTemplate>
</ListBox> </ListBox>
<kkp:ToastNotificationHost x:Name="NotificationHost" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="20" Duration="0:0:2" />
<kkp:ToastNotificationHost x:Name="NotificationHost"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="20"
Duration="0:0:2" />
</Grid> </Grid>
</DataTemplate> </DataTemplate>
<DataTemplate DataType="{x:Type vm:LockedRepositoryViewModel}"> <DataTemplate DataType="{x:Type vm:LockedRepositoryViewModel}">
<StackPanel Margin="20" <StackPanel Margin="20"
HorizontalAlignment="Center" HorizontalAlignment="Center"

View File

@@ -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) private void SaveButton_Click(object sender, RoutedEventArgs args)
{ {
if (DataContext is RepositoryWindowViewModel vm && vm.CurrentPage is UnlockedRepositoryViewModel pageVm) if (DataContext is RepositoryWindowViewModel vm && vm.CurrentPage is UnlockedRepositoryViewModel pageVm)
@@ -49,20 +79,16 @@ public partial class RepositoryWindow: Window
private void Entry_DoubleTapped(object sender, TappedEventArgs args) 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); Clipboard!.SetTextAsync(pwd.Password.Value);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Password copied to clipboard"); this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Password copied to clipboard");
} }
} }
}
private void EntryContextMenuItem_Click(object sender, RoutedEventArgs args) { private async void EntryContextMenuItem_Click(object sender, RoutedEventArgs args)
if (args.Source is StyledElement s)
{ {
if (s.DataContext is PassStoreEntryPassword pwd) if (args.Source is StyledElement s && s.DataContext is PassStoreEntryPassword pwd)
{ {
if (s.Name == "entryCtxMenuCopyUsername") if (s.Name == "entryCtxMenuCopyUsername")
{ {
@@ -74,15 +100,25 @@ public partial class RepositoryWindow: Window
Clipboard!.SetTextAsync(pwd.Password.Value); Clipboard!.SetTextAsync(pwd.Password.Value);
this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Password copied to clipboard"); this.FindControlRecursive<ToastNotificationHost>("NotificationHost")?.Show("Password copied to clipboard");
} }
else if (s.Name == "entryCtxMenuDelete") else if (s.Name == "entryCtxMenuEdit")
{
if (s.DataContext is PassStoreEntryPassword entry)
{ {
if (DataContext is RepositoryWindowViewModel vm && vm.CurrentPage is UnlockedRepositoryViewModel pageVm) if (DataContext is RepositoryWindowViewModel vm && vm.CurrentPage is UnlockedRepositoryViewModel pageVm)
{ {
pageVm.DeleteEntry(entry.Id); EntryEditWindow dialog = new();
dialog.SetEntry(pwd);
await dialog.ShowDialog(this);
if (dialog.EditedEntry != null)
{
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"); 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 private static T? FindControlRecursiveInternal<T>(Visual parent, string name, int depth) where T : Visual
{ {
if (parent == null) if (parent == null) return null;
return null; if (parent is T t && parent.Name == name) return t;
if (parent is T t && parent.Name == name)
return t;
foreach (var child in parent.GetVisualChildren()) foreach (var child in parent.GetVisualChildren())
{ {
if (child == null) if (child == null) continue;
continue;
var result = FindControlRecursiveInternal<T>(child, name, depth + 1); var result = FindControlRecursiveInternal<T>(child, name, depth + 1);
if (result != null) if (result != null) return result;
return result;
} }
// Also check logical children if they're not in visual tree
if (parent is ContentPresenter contentPresenter) if (parent is ContentPresenter contentPresenter)
{ {
var content = contentPresenter.Content as Visual; var content = contentPresenter.Content as Visual;
if (content != null) if (content != null)
{ {
var result = FindControlRecursiveInternal<T>(content, name, depth + 1); var result = FindControlRecursiveInternal<T>(content, name, depth + 1);
if (result != null) if (result != null) return result;
return result;
} }
} }