InkForge/app/InkForge.Desktop/ViewModels/Workspaces/WorkspaceViewModel.cs

56 lines
1.4 KiB
C#
Raw Permalink Normal View History

2024-05-02 21:44:13 +02:00
using System.Collections.ObjectModel;
using Avalonia;
using DynamicData;
2024-02-26 18:08:18 +01:00
using InkForge.Desktop.Models;
2024-03-17 22:27:01 +01:00
using Microsoft.Extensions.DependencyInjection;
2024-02-26 18:08:18 +01:00
2024-03-17 22:27:01 +01:00
namespace InkForge.Desktop.ViewModels.Workspaces
2024-02-26 18:08:18 +01:00
{
2024-04-05 12:31:34 +02:00
public class WorkspaceViewModel
2024-03-17 22:27:01 +01:00
{
2024-05-02 21:44:13 +02:00
private readonly Workspace _workspace;
2024-04-05 12:31:34 +02:00
private readonly NoteStore _noteStore;
2024-05-02 21:44:13 +02:00
private readonly ReadOnlyObservableCollection<Node<Note, int>> _notes;
2024-02-26 18:08:18 +01:00
2024-05-02 21:44:13 +02:00
public string Name => _workspace.Name;
2024-02-26 18:08:18 +01:00
2024-05-02 21:44:13 +02:00
public ReadOnlyObservableCollection<Node<Note, int>> Notes => _notes;
2024-02-26 18:08:18 +01:00
2024-05-02 21:44:13 +02:00
public WorkspaceViewModel(Workspace workspace, NoteStore noteStore)
2024-04-05 12:31:34 +02:00
{
2024-05-02 21:44:13 +02:00
_workspace = workspace;
2024-04-05 12:31:34 +02:00
_noteStore = noteStore;
2024-05-02 21:44:13 +02:00
noteStore.Notes
.AsObservableChangeSet(m => m.Key)
.Transform(m => m.Value, true)
.TransformToTree(m => m.Id)
.Bind(out _notes).Subscribe();
2024-04-05 12:31:34 +02:00
}
2024-03-17 22:27:01 +01:00
}
public interface IWorkspaceViewModelFactory
{
WorkspaceViewModel Create(Workspace workspace);
}
namespace Internal
{
2024-05-02 21:44:13 +02:00
internal class WorkspaceViewModelFactory : IWorkspaceViewModelFactory
2024-03-17 22:27:01 +01:00
{
private static ObjectFactory<WorkspaceViewModel>? s_workspaceViewModelFactory;
2024-05-02 21:44:13 +02:00
public static WorkspaceViewModel Create(Workspace workspace)
2024-03-17 22:27:01 +01:00
{
s_workspaceViewModelFactory ??= ActivatorUtilities.CreateFactory<WorkspaceViewModel>([typeof(Workspace)]);
2024-05-02 21:44:13 +02:00
return s_workspaceViewModelFactory(workspace.Services, [workspace]);
2024-03-17 22:27:01 +01:00
}
WorkspaceViewModel IWorkspaceViewModelFactory.Create(Workspace workspace) => Create(workspace);
}
}
2024-02-26 18:08:18 +01:00
}