Notes/Notes.Data/ViewModels/MainViewModel.cs

79 lines
2.2 KiB
C#

using System;
using System.Diagnostics.CodeAnalysis;
using CommunityToolkit.Mvvm.ComponentModel;
using Notes.Models;
using Utils;
namespace Notes.ViewModels;
public class MainViewModelDesignData:MainViewModel
{
public MainViewModelDesignData()
{
ContentViewModel = new AboutViewModel(Service);
}
}
public partial class MainViewModel:ViewModelBase
{
/// <summary>
/// current viewModel
/// </summary>
[ObservableProperty] private ViewModelBase _contentViewModel;
[ObservableProperty]
private AllNotesViewModel? _allNotesViewModel;
[ObservableProperty]
private AboutViewModel? _aboutViewModel;
[ObservableProperty]
private NoteViewModel? _currNoteViewModel;
[ObservableProperty]
private IServices _service;
public MainViewModel(IServices services)
{
_service = services;
_service.Register(this);
_contentViewModel =_aboutViewModel= new AboutViewModel(_service);
}
public MainViewModel()
{
}
public T Navitation<T>() where T: ViewModelBase
{
var type = typeof(T);
if (type == typeof(AboutViewModel))
{
AboutViewModel ??= new AboutViewModel(Service);
this.ContentViewModel = AboutViewModel;
return (T)this.ContentViewModel;
}
else if (type == typeof(AllNotesViewModel))
{
AllNotesViewModel ??= new AllNotesViewModel(Service);
this.ContentViewModel = AllNotesViewModel;
return (T)this.ContentViewModel;
}
else if (type == typeof(NoteViewModel))
{
AllNotesViewModel ??= new AllNotesViewModel(Service);
CurrNoteViewModel ??= new NoteViewModel(Service,AllNotesViewModel,new Note());
this.ContentViewModel = CurrNoteViewModel;
return (T)this.ContentViewModel;
}
throw new InvalidOperationException($"{typeof(T)} is not a {nameof(ViewModelBase)}");
}
public void OpenNote(Note note)
{
AllNotesViewModel ??= new AllNotesViewModel(Service);
CurrNoteViewModel ??= new NoteViewModel(Service,AllNotesViewModel,note);
CurrNoteViewModel.Note = note;
this.ContentViewModel = CurrNoteViewModel;
}
}