Started implementing backend
This commit is contained in:
@@ -1,50 +0,0 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Keychain.UI.ViewModels;
|
||||
|
||||
public class PasswordStoreShortcut : INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
private string displayName;
|
||||
private bool displayNameSet = false;
|
||||
private string? iconName;
|
||||
private string path;
|
||||
public bool DisplayNameSet { get => displayNameSet; }
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
namespace Keychain.UI.ViewModels;
|
||||
|
||||
using Adw;
|
||||
using Gtk;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStoreShortcut>
|
||||
{
|
||||
private readonly PreferencesGroup shortcutsGroup;
|
||||
private readonly Dictionary<PasswordStoreShortcut, ActionRow> itemToRowMap = new();
|
||||
|
||||
public PasswordStoreShortcutCollection(PreferencesGroup shortcutsGroup)
|
||||
{
|
||||
this.shortcutsGroup = shortcutsGroup;
|
||||
CollectionChanged += OnCollectionChanged;
|
||||
}
|
||||
|
||||
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
switch (e.Action)
|
||||
{
|
||||
case NotifyCollectionChangedAction.Add:
|
||||
foreach (PasswordStoreShortcut item in e.NewItems)
|
||||
{
|
||||
var row = CreateShortcutRow(item);
|
||||
itemToRowMap[item] = row;
|
||||
shortcutsGroup.Add(row);
|
||||
|
||||
// Subscribe to property changes for reactive updates
|
||||
item.PropertyChanged += (sender, args) => UpdateRowFromItem(item, ref row);
|
||||
}
|
||||
break;
|
||||
|
||||
case NotifyCollectionChangedAction.Remove:
|
||||
foreach (PasswordStoreShortcut item in e.OldItems)
|
||||
{
|
||||
if (itemToRowMap.TryGetValue(item, out var row))
|
||||
{
|
||||
shortcutsGroup.Remove(row);
|
||||
itemToRowMap.Remove(item);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case NotifyCollectionChangedAction.Reset:
|
||||
foreach (var row in itemToRowMap.Values)
|
||||
{
|
||||
shortcutsGroup.Remove(row);
|
||||
}
|
||||
itemToRowMap.Clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private ActionRow CreateShortcutRow(PasswordStoreShortcut shortcut)
|
||||
{
|
||||
var row = new ActionRow();
|
||||
UpdateRowFromItem(shortcut, ref row);
|
||||
|
||||
row.SetActivatable(true);
|
||||
row.OnActivated += (sender, args) => {
|
||||
Console.WriteLine($"[DEBUG] Opening: {shortcut.Path}");
|
||||
};
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
private void UpdateRowFromItem(PasswordStoreShortcut shortcut, ref ActionRow row)
|
||||
{
|
||||
row.SetTitle(shortcut.DisplayName);
|
||||
row.SetSubtitle(shortcut.Path);
|
||||
|
||||
//Update icon
|
||||
var existingIcon = row.GetFirstChild() as Gtk.Image;
|
||||
if (existingIcon == null)
|
||||
{
|
||||
var icon = new Gtk.Image();
|
||||
icon.SetFromIconName(shortcut.IconName);
|
||||
row.AddPrefix(icon);
|
||||
}
|
||||
else
|
||||
{
|
||||
existingIcon.SetFromIconName(shortcut.IconName);
|
||||
}
|
||||
|
||||
// Edit button
|
||||
var edit = new Button();
|
||||
edit.AddCssClass("flat");
|
||||
edit.SetValign(Align.Center);
|
||||
edit.IconName = "document-edit-symbolic";
|
||||
row.AddSuffix(edit);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user