- tweak: Dependency injection think

This commit is contained in:
2024-12-21 13:11:30 +03:00
parent 9a1bd44a6e
commit 42fde38db6
27 changed files with 541 additions and 242 deletions

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Nebula.Launcher.Models;
namespace Nebula.Launcher.ViewModels;
public partial class MainViewModel : ViewModelBase
{
public MainViewModel(AccountInfoViewModel accountInfoViewModel, IServiceProvider serviceProvider)
{
_currentPage = accountInfoViewModel;
_serviceProvider = serviceProvider;
Items = new ObservableCollection<ListItemTemplate>(_templates);
SelectedListItem = Items.First(vm => vm.ModelType == typeof(AccountInfoViewModel));
}
private readonly List<ListItemTemplate> _templates =
[
new ListItemTemplate(typeof(AccountInfoViewModel), "HomeRegular", "Account"),
];
[ObservableProperty]
private bool _isPaneOpen;
[ObservableProperty]
private ViewModelBase _currentPage;
private readonly IServiceProvider _serviceProvider;
[ObservableProperty]
private ListItemTemplate? _selectedListItem;
partial void OnSelectedListItemChanged(ListItemTemplate? value)
{
if (value is null) return;
var vm = Design.IsDesignMode
? Activator.CreateInstance(value.ModelType)
: _serviceProvider.GetService(value.ModelType);
if (vm is not ViewModelBase vmb) return;
CurrentPage = vmb;
}
public ObservableCollection<ListItemTemplate> Items { get; }
[RelayCommand]
private void TriggerPane()
{
IsPaneOpen = !IsPaneOpen;
}
}