Started working on loading password list from viewModel
This commit is contained in:
83
src/App/ViewModels/PasswordList.cs
Normal file
83
src/App/ViewModels/PasswordList.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user