Project_Keywi/Project.View/MenuView/MenuView.axaml.cs

204 lines
5.8 KiB
C#

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media;
using Project.Controller;
using Project.Model;
using Project.Persistence;
using Avalonia.Input.Platform; // Fügen Sie diese Zeile am Anfang der Datei hinzu
using System.Threading.Tasks;
namespace Project.View;
public partial class MenuView : Window
{
readonly AppController? _controller;
public MenuView()
{
InitializeComponent();
CreateEntryButtons();
Title = "KEYWI - your password manager!";
}
public MenuView(AppController controller)
{
InitializeComponent();
_controller = controller;
}
private async void RefreshButton_OnClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
CreateEntryButtons();
}
//opens the window to create a new entry
private async void NewEntryButton_OnClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var newEntryWindow = new NewEntry();
newEntryWindow.Show();
}
private void CreateEntryButtons()
{
// dynamically creating buttons for each entry
entriesContainer.Children.Clear(); // Clear existing buttons
var entries = SQLite.GetEntries(AppController._currentUser);
foreach (var entry in entries)
{
var button = new Button
{
Content = entry.Name, // Display the entry name on the button
Margin = new Thickness(5),
Padding = new Thickness(10),
Width = 200,
Background = Brushes.SlateGray,
};
// add click handler for the button
button.Click += (object? sender, Avalonia.Interactivity.RoutedEventArgs e) => EntryDetails(entry);
// Add the button to your container
entriesContainer.Children.Add(button);
}
}
//refreshing the buttons when saving a new entry
public void AfterSavingNewEntry()
{
CreateEntryButtons();
}
//shows the details for the selected entry
public void EntryDetails(SavedEntries entry)
{
ShowEntryDetailsL.Children.Clear();
ShowEntryDetailsR.Children.Clear();
ShowEntryDetailsL.Children.Add(new TextBlock
{
Text = $"Name:",
Margin = new Thickness(0, 0, 0, 5)
});
ShowEntryDetailsL.Children.Add(new TextBlock
{
Text = $"Email/Username:",
Margin = new Thickness(0, 0, 0, 5)
});
ShowEntryDetailsL.Children.Add(new TextBlock
{
Text = $"URL:",
Margin = new Thickness(0, 0, 0, 5)
});
ShowEntryDetailsL.Children.Add(new TextBlock
{
Text = $"Password:",
Margin = new Thickness(0, 0, 0, 5)
});
ShowEntryDetailsL.Children.Add(new TextBlock
{
Text = $"Note:",
Margin = new Thickness(0, 0, 0, 5)
});
ShowEntryDetailsR.Children.Add(new TextBox()
{
Text = $"{entry.Name}",
Margin = new Thickness(0, 0, 0, 5)
});
ShowEntryDetailsR.Children.Add(new TextBox()
{
Text = $"{entry.MailUsername}",
Margin = new Thickness(0, 0, 0, 5)
});
ShowEntryDetailsR.Children.Add(new TextBox()
{
Text = $"{entry.Url}",
Margin = new Thickness(0, 0, 0, 5)
});
var passwordBox = new TextBox
{
Name = "LoginPasswordBox",
PasswordChar = '•',
Text = Crypto.EncryptPassword(entry.Pass),
Margin = new Thickness(0, 0, 0, 5)
};
ShowEntryDetailsR.Children.Add(passwordBox);
//dynamically creating a new horizontal stackpanel
var buttonContainer = new StackPanel
{
Orientation = Avalonia.Layout.Orientation.Horizontal,
Spacing = 5
};
//creating 2 buttons next to each other to show/hide and copy password
var showPasswordButton = new Button
{
Width = 40,
Height = 30,
Content = "👁"
};
var copyPasswordButton = new Button
{
Width = 40,
Height = 30,
Content = "📋"
};
//on click events for both buttons
showPasswordButton.Click += (sender, args) =>
{
if (passwordBox.PasswordChar == '•')
{
passwordBox.Text = Crypto.DecryptPassword(entry.Pass);
passwordBox.PasswordChar = '\0';
}
else
{
passwordBox.Text = Crypto.EncryptPassword(entry.Pass);
passwordBox.PasswordChar = '•';
}
};
copyPasswordButton.Click += async (sender, args) =>
{
var clipboard = TopLevel.GetTopLevel(this)?.Clipboard;
if (clipboard != null)
{
await clipboard.SetTextAsync(Crypto.DecryptPassword(entry.Pass));
// visual feedback
copyPasswordButton.Content = "✓";
await Task.Delay(1000);
copyPasswordButton.Content = "📋";
}
};
//show note if it exists
if (!string.IsNullOrEmpty(entry.Note))
{
ShowEntryDetailsR.Children.Add(new TextBox()
{
Text = $"{entry.Note}",
Margin = new Thickness(0, 0, 0, 5)
});
}
//add buttons
buttonContainer.Children.Add(showPasswordButton);
buttonContainer.Children.Add(copyPasswordButton);
ShowEntryDetailsR.Children.Add(buttonContainer);
}
}