Implemented proper viewModels

This commit is contained in:
2025-09-30 11:31:38 +02:00
parent c8c24cb7cc
commit a99b9fe0bb
7 changed files with 184 additions and 110 deletions

View File

@@ -1,5 +1,6 @@
using Adw;
using Keychain.ViewModels;
using Logic;
namespace Keychain.UI;
@@ -12,6 +13,8 @@ public class MainWindow
private Gtk.Stack titleStack;
private Gtk.SearchEntry searchEntry;
private readonly IPasswordStoreService passwordStoreService;
private readonly string windowId = "main_window";
private readonly string shortcutsGroupId = "shortcuts_group";
private readonly string addShortcutButtonId = "add_shortcut_button";
@@ -19,8 +22,9 @@ public class MainWindow
private readonly string titleStackId = "title_stack";
private readonly string searchEntryId = "search_entry";
public MainWindow()
public MainWindow(IPasswordStoreService passwordStoreService)
{
this.passwordStoreService = passwordStoreService;
var builder = new Gtk.Builder("Keychain.UI.MainWindow.MainWindow.xml");
@@ -74,38 +78,27 @@ public class MainWindow
throw new Exception("Failed to load UI element with ID: " + e.Message);
}
// Initialize the observable collection with property binding
shortcuts = new PasswordStoreShortcutCollection(shortcutsGroup);
// Initialize the observable collection with property binding
shortcuts = new PasswordStoreShortcutCollection(shortcutsGroup, passwordStoreService);
LoadDefaultShortcuts();
}
private void OnAddShortcutClicked(object sender, EventArgs e)
{
var dialog = new AddShortcutWindow().Dialog;
var dialog = new AddShortcutWindow(shortcuts).Dialog;
dialog.Present(Window);
}
private void AddShortcut(string path)
{
var newShortcut = new PasswordStoreShortcut(path: path);
shortcuts.Add(newShortcut); // This will automatically update the UI
}
private void RemoveShortcut(PasswordStoreViewModel shortcut)
{
shortcuts.Remove(shortcut); // This will automatically update the UI
}
private void LoadDefaultShortcuts()
{
shortcuts.Add(new PasswordStoreShortcut(displayName: "Default", path: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/.password_store"));
shortcuts.Add(new PasswordStoreViewModel(displayName: "Default", path: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/.password_store"));
}
private void UpdateShortcutName(PasswordStoreViewModel shortcut, string newName)
{
shortcut.DisplayName = newName; // This will automatically update the UI row
}
// private void UpdateShortcutName(PasswordStoreViewModel shortcut, string newName)
// {
// shortcut.DisplayName = newName; // This will automatically update the UI row
// }
private void SetSearchBarVisible(object sender, EventArgs e)
{