mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Create database works with new Vm
Refactoring
This commit is contained in:
69
WinAppCommon/ViewModels/RecentVm.cs
Normal file
69
WinAppCommon/ViewModels/RecentVm.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Windows.Input;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Domain.Interfaces;
|
||||
using ModernKeePass.ViewModels.ListItems;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class RecentVm : ObservableObject, IHasSelectableObject
|
||||
{
|
||||
private readonly IRecentProxy _recent;
|
||||
private ISelectableModel _selectedItem;
|
||||
private ObservableCollection<RecentItemVm> _recentItems;
|
||||
|
||||
public ObservableCollection<RecentItemVm> RecentItems
|
||||
{
|
||||
get { return _recentItems; }
|
||||
set { Set(() => RecentItems, ref _recentItems, value); }
|
||||
}
|
||||
|
||||
public ISelectableModel SelectedItem
|
||||
{
|
||||
get { return _selectedItem; }
|
||||
set
|
||||
{
|
||||
if (_selectedItem == value) return;
|
||||
if (_selectedItem != null)
|
||||
{
|
||||
_selectedItem.IsSelected = false;
|
||||
}
|
||||
|
||||
Set(() => SelectedItem, ref _selectedItem, value);
|
||||
|
||||
if (_selectedItem == null) return;
|
||||
_selectedItem.IsSelected = true;
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand ClearAllCommand { get; }
|
||||
|
||||
public RecentVm() : this (App.Services.GetRequiredService<IRecentProxy>()) { }
|
||||
|
||||
public RecentVm(IRecentProxy recent)
|
||||
{
|
||||
_recent = recent;
|
||||
ClearAllCommand = new RelayCommand(ClearAll);
|
||||
|
||||
var recentItems = _recent.GetAll().Select(r => new RecentItemVm(r));
|
||||
RecentItems = new ObservableCollection<RecentItemVm>(recentItems);
|
||||
if (RecentItems.Count > 0)
|
||||
SelectedItem = RecentItems[0];
|
||||
}
|
||||
|
||||
public void UpdateAccessTime(string token)
|
||||
{
|
||||
_recent.Get(token, true).Wait();
|
||||
}
|
||||
|
||||
private void ClearAll()
|
||||
{
|
||||
_recent.ClearAll();
|
||||
RecentItems.Clear();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user