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 { /// /// current viewModel /// [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() 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; } }