Started implementing proper ViewModels now that the backend is mostly done

This commit is contained in:
2025-09-26 11:02:37 +02:00
parent e2c588794e
commit 22ad11add1
4 changed files with 38 additions and 54 deletions

View File

@@ -92,7 +92,7 @@ public class MainWindow
shortcuts.Add(newShortcut); // This will automatically update the UI shortcuts.Add(newShortcut); // This will automatically update the UI
} }
private void RemoveShortcut(PasswordStoreShortcut shortcut) private void RemoveShortcut(PasswordStoreViewModel shortcut)
{ {
shortcuts.Remove(shortcut); // This will automatically update the UI shortcuts.Remove(shortcut); // This will automatically update the UI
} }
@@ -102,7 +102,7 @@ public class MainWindow
shortcuts.Add(new PasswordStoreShortcut(displayName: "Default", path: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/.password_store")); shortcuts.Add(new PasswordStoreShortcut(displayName: "Default", path: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/.password_store"));
} }
private void UpdateShortcutName(PasswordStoreShortcut shortcut, string newName) private void UpdateShortcutName(PasswordStoreViewModel shortcut, string newName)
{ {
shortcut.DisplayName = newName; // This will automatically update the UI row shortcut.DisplayName = newName; // This will automatically update the UI row
} }

View File

@@ -1,46 +0,0 @@
using System.ComponentModel;
using Logic;
namespace Keychain.ViewModels;
public class PasswordStoreShortcut : INotifyPropertyChanged
{
private IPasswordStoreService passwordService;
public event PropertyChangedEventHandler? PropertyChanged;
public string DisplayName
{
get => displayName;
set
{
displayName = value;
displayNameSet = true;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DisplayName)));
}
}
public string? IconName
{
get => iconName;
set { iconName = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IconName))); }
}
public string Path
{
get => path;
}
public PasswordStoreShortcut(string path, string iconName = "text-x-generic-symbolic", string? displayName = null)
{
this.path = path;
this.iconName = iconName;
this.displayName = path;
if (displayName != null)
{
DisplayName = displayName;
}
}
}

View File

@@ -5,10 +5,10 @@ using Gtk;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized; using System.Collections.Specialized;
public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStoreShortcut> public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStoreViewModel>
{ {
private readonly PreferencesGroup shortcutsGroup; private readonly PreferencesGroup shortcutsGroup;
private readonly Dictionary<PasswordStoreShortcut, ActionRow> itemToRowMap = new(); private readonly Dictionary<PasswordStoreViewModel, ActionRow> itemToRowMap = new();
public PasswordStoreShortcutCollection(PreferencesGroup shortcutsGroup) public PasswordStoreShortcutCollection(PreferencesGroup shortcutsGroup)
{ {
@@ -21,7 +21,7 @@ public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStor
switch (e.Action) switch (e.Action)
{ {
case NotifyCollectionChangedAction.Add: case NotifyCollectionChangedAction.Add:
foreach (PasswordStoreShortcut item in e.NewItems) foreach (PasswordStoreViewModel item in e.NewItems)
{ {
var row = CreateShortcutRow(item); var row = CreateShortcutRow(item);
itemToRowMap[item] = row; itemToRowMap[item] = row;
@@ -33,7 +33,7 @@ public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStor
break; break;
case NotifyCollectionChangedAction.Remove: case NotifyCollectionChangedAction.Remove:
foreach (PasswordStoreShortcut item in e.OldItems) foreach (PasswordStoreViewModel item in e.OldItems)
{ {
if (itemToRowMap.TryGetValue(item, out var row)) if (itemToRowMap.TryGetValue(item, out var row))
{ {
@@ -53,7 +53,7 @@ public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStor
} }
} }
private ActionRow CreateShortcutRow(PasswordStoreShortcut shortcut) private ActionRow CreateShortcutRow(PasswordStoreViewModel shortcut)
{ {
var row = new ActionRow(); var row = new ActionRow();
UpdateRowFromItem(shortcut, ref row); UpdateRowFromItem(shortcut, ref row);
@@ -66,7 +66,7 @@ public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStor
return row; return row;
} }
private void UpdateRowFromItem(PasswordStoreShortcut shortcut, ref ActionRow row) private void UpdateRowFromItem(PasswordStoreViewModel shortcut, ref ActionRow row)
{ {
row.SetTitle(shortcut.DisplayName); row.SetTitle(shortcut.DisplayName);
row.SetSubtitle(shortcut.Path); row.SetSubtitle(shortcut.Path);

View File

@@ -0,0 +1,30 @@
using System.ComponentModel;
using Models;
namespace Keychain.ViewModels;
public class PasswordStoreViewModel : INotifyPropertyChanged
{
private PasswordStore _model;
public event PropertyChangedEventHandler? PropertyChanged;
public string? DisplayName
{
get => _model.DisplayName;
}
public string? IconName
{
get => _model.IconName;
}
public string Path
{
get => _model.Path;
}
public PasswordStoreViewModel(PasswordStore item)
{
_model = item;
}
}