Started working on loading password list from viewModel

This commit is contained in:
2025-09-30 19:05:53 +02:00
parent c8d98e72d0
commit e117748bb5
6 changed files with 152 additions and 12 deletions

View File

@@ -8,19 +8,22 @@ public class MainWindow
{ {
public Window Window { get; } public Window Window { get; }
private PreferencesGroup shortcutsGroup; private PreferencesGroup shortcutsGroup;
private PasswordList passwordCollection;
private PasswordStoreShortcutCollection shortcuts; private PasswordStoreShortcutCollection shortcuts;
private Gtk.ToggleButton searchToggleButton; private Gtk.ToggleButton searchToggleButton;
private Gtk.Stack titleStack; private Gtk.Stack titleStack;
private Gtk.SearchEntry searchEntry; private Gtk.SearchEntry searchEntry;
private PreferencesGroup passwordList;
private readonly IPasswordStoreService passwordStoreService; private readonly IPasswordStoreService passwordStoreService;
private readonly string windowId = "main_window"; private const string windowId = "main_window";
private readonly string shortcutsGroupId = "shortcuts_group"; private const string shortcutsGroupId = "shortcuts_group";
private readonly string addShortcutButtonId = "add_shortcut_button"; private const string addShortcutButtonId = "add_shortcut_button";
private readonly string searchToggleButtonId = "search_button"; private const string searchToggleButtonId = "search_button";
private readonly string titleStackId = "title_stack"; private const string titleStackId = "title_stack";
private readonly string searchEntryId = "search_entry"; private const string searchEntryId = "search_entry";
private const string passwordListId = "password_list";
public MainWindow(IPasswordStoreService passwordStoreService) public MainWindow(IPasswordStoreService passwordStoreService)
{ {
@@ -38,7 +41,9 @@ public class MainWindow
{ {
shortcutsGroup = builder.GetObject(shortcutsGroupId) as PreferencesGroup; shortcutsGroup = builder.GetObject(shortcutsGroupId) as PreferencesGroup;
if (shortcutsGroup == null) if (shortcutsGroup == null)
{
throw new Exception(shortcutsGroupId); throw new Exception(shortcutsGroupId);
}
var addButton = builder.GetObject(addShortcutButtonId) as Gtk.Button; var addButton = builder.GetObject(addShortcutButtonId) as Gtk.Button;
if (addButton == null) if (addButton == null)
@@ -72,16 +77,26 @@ public class MainWindow
searchToggleButton.Active = false; searchToggleButton.Active = false;
}; };
searchEntry.AddController(focusController); searchEntry.AddController(focusController);
passwordList = builder.GetObject(passwordListId) as PreferencesGroup;
if (passwordList == null)
{
throw new Exception(passwordListId);
}
} }
catch (Exception e) catch (Exception e)
{ {
throw new Exception("Failed to load UI element with ID: " + e.Message); throw new Exception("Failed to load UI element with ID: " + e.Message);
} }
// Initialize the observable collection with property binding // Initialize the observable collection with property binding
shortcuts = new PasswordStoreShortcutCollection(shortcutsGroup, passwordStoreService); shortcuts = new PasswordStoreShortcutCollection(shortcutsGroup, passwordStoreService);
if (shortcuts.Count == 0)
{
LoadDefaultShortcuts();
}
LoadDefaultShortcuts(); passwordCollection = new PasswordList(passwordList);
} }
private void OnAddShortcutClicked(object sender, EventArgs e) private void OnAddShortcutClicked(object sender, EventArgs e)

View File

@@ -0,0 +1,83 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Adw;
using Gtk;
using Logic;
namespace Keychain.ViewModels;
public class PasswordList : ObservableCollection<PasswordViewModel>
{
private readonly IPasswordService _passwordService;
private readonly PreferencesGroup list;
private Dictionary<PasswordViewModel, ActionRow> itemToRowMap = new();
public PasswordList(PreferencesGroup list, IPasswordService passwordService)
: base()
{
this.list = list;
CollectionChanged += OnCollectionChanged;
_passwordService = passwordService;
//test
Add(new PasswordViewModel("Sample Password"));
}
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (PasswordViewModel item in e.NewItems)
{
var row = CreateActionRow(item);
itemToRowMap[item] = row;
list.Add(row);
// Subscribe to property changes for reactive updates
item.PropertyChanged += (sender, args) => UpdateRowFromItem(item, ref row);
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (PasswordViewModel item in e.OldItems)
{
if (itemToRowMap.TryGetValue(item, out var row))
{
list.Remove(row);
itemToRowMap.Remove(item);
}
}
break;
case NotifyCollectionChangedAction.Reset:
foreach (var row in itemToRowMap.Values)
{
list.Remove(row);
}
itemToRowMap.Clear();
break;
}
}
private ActionRow CreateActionRow(PasswordViewModel item)
{
var row = new ActionRow();
UpdateRowFromItem(item, ref row);
return row;
}
private void UpdateRowFromItem(PasswordViewModel item, ref ActionRow row)
{
row.SetTitle(item.Name);
// Inactive button for intuitive UI
var go = new Button();
go.Sensitive = false;
go.AddCssClass("flat");
go.SetValign(Align.Center);
go.IconName = "go-next-symbolic";
row.AddSuffix(go);
}
}

View File

@@ -8,10 +8,10 @@ using System.Collections.Specialized;
public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStoreViewModel> public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStoreViewModel>
{ {
private IPasswordStoreService _passwordStoreService; private readonly IPasswordStoreService _passwordStoreService;
private readonly PreferencesGroup shortcutsGroup; private readonly PreferencesGroup shortcutsGroup;
private readonly Dictionary<PasswordStoreViewModel, ActionRow> itemToRowMap = new(); private Dictionary<PasswordStoreViewModel, ActionRow> itemToRowMap = new();
public PasswordStoreShortcutCollection(PreferencesGroup shortcutsGroup, IPasswordStoreService passwordStoreService) public PasswordStoreShortcutCollection(PreferencesGroup shortcutsGroup, IPasswordStoreService passwordStoreService)
: base() : base()
@@ -19,6 +19,12 @@ public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStor
this.shortcutsGroup = shortcutsGroup; this.shortcutsGroup = shortcutsGroup;
_passwordStoreService = passwordStoreService; _passwordStoreService = passwordStoreService;
CollectionChanged += OnCollectionChanged; CollectionChanged += OnCollectionChanged;
// Initialize from existing stores
foreach (var store in _passwordStoreService.GetAll())
{
Add(new PasswordStoreViewModel(store));
}
} }
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)

View File

@@ -0,0 +1,30 @@
using System.ComponentModel;
using Models;
namespace Keychain.ViewModels;
public class PasswordViewModel : INotifyPropertyChanged
{
private Password _model { get; }
public event PropertyChangedEventHandler? PropertyChanged;
public Password Model { get => _model; }
public string Name
{
get => _model.Name;
}
public PasswordViewModel(Password item)
{
_model = item;
}
public PasswordViewModel(string name)
{
_model = new Password
{
Name = name,
};
}
}

6
src/Models/Password.cs Normal file
View File

@@ -0,0 +1,6 @@
namespace Models;
public class Password
{
public string Name { get; set; }
}

View File

@@ -13,7 +13,7 @@ public class JsonRepository : IRepository, IDisposable
private List<PasswordStore> _cache; private List<PasswordStore> _cache;
private bool _cacheAhead; private bool _cacheAhead;
public JsonRepository(string fileName) public JsonRepository()
{ {
string? xdgDataHome = Environment.GetEnvironmentVariable("XDG_DATA_HOME"); string? xdgDataHome = Environment.GetEnvironmentVariable("XDG_DATA_HOME");
string dataHome; string dataHome;