using AutoService.Models; using Avalonia.Controls.ApplicationLifetimes; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.DependencyInjection; using MsBox.Avalonia; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AutoService.ViewModels { public partial class MainWindowVM:ViewModelBase { private readonly IServiceProvider _serviceProvider; private readonly IServiceRepository _serviceRepository; private readonly LocalContainer _container; [ObservableProperty] private ObservableCollection services; [ObservableProperty] private string clientName; [ObservableProperty] private string autoModel; [ObservableProperty] private Service selectedService; public MainWindowVM(IServiceProvider serviceProvider, IServiceRepository serviceRepository, LocalContainer localContainer) { _serviceProvider = serviceProvider; _serviceRepository = serviceRepository; _container = localContainer; ClientName = ""; AutoModel = ""; try { services = new ObservableCollection(_serviceRepository.GetAll()); SelectedService = Services.First(); } catch(Exception e) { Console.WriteLine(e); services = new(); } } [RelayCommand] public async Task StartWorksWindowAsync() { var _currentWindow = (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime).MainWindow; if (ClientName.Length > 2 && AutoModel.Length > 2) { if (!_container.StringValues.ContainsKey("ClientName")) { _container.StringValues.Add("ClientName", ClientName); } else { _container.StringValues["ClientName"] = ClientName; } if (!_container.StringValues.ContainsKey("AutoModel")) { _container.StringValues.Add("AutoModel", AutoModel); } else { _container.StringValues["AutoModel"] = AutoModel; } _container.SelectedService = SelectedService; var vm = _serviceProvider.GetRequiredService(); var win = _serviceProvider.GetRequiredService(); win.DataContext = vm; vm.SetWindow(win); win.Show(); _currentWindow.Close(); } else { var errWin = MessageBoxManager.GetMessageBoxStandard("Косяк!", "Поля \"Имя\" и \"Марка машины\" должны быть заполнены!", MsBox.Avalonia.Enums.ButtonEnum.Ok); await errWin.ShowAsync(); } } } }