using System; using System.Windows.Input; using leak_test_project.Infrastructure; using leak_test_project.Services; using leak_test_project.ViewModels.Core; namespace leak_test_project.ViewModels { public class MainViewModel : ObservableObject, IDisposable { private ObservableObject _currentViewModel; private readonly IoBoardService _ioService; public HomeViewModel HomeVM { get; } public InOutViewModel InOutVM { get; } public DataViewModel DataVM { get; } public ParametersViewModel ParametersVM { get; } public ObservableObject CurrentViewModel { get => _currentViewModel; set => SetProperty(ref _currentViewModel, value); } public ICommand NavigateHomeCommand { get; } public ICommand NavigateInOutCommand { get; } public ICommand NavigateDataCommand { get; } public ICommand NavigateParametersCommand { get; } public ICommand NavigateCommunicationCommand { get; } public ICommand ExitCommand { get; } public MainViewModel() { // 팩토리를 통해 설정에 맞는 DIO 보드 생성 (Simulation 여부 자동 판단) var dioBoard = DioBoardFactory.CreateBoard(); _ioService = new IoBoardService(dioBoard); _ioService.SetOutput("PC_ON", true); // ViewModel 생성 시 DIO 보드 공유 HomeVM = new HomeViewModel(dioBoard); InOutVM = new InOutViewModel(() => CurrentViewModel = HomeVM, _ioService); DataVM = new DataViewModel(() => CurrentViewModel = HomeVM); ParametersVM = new ParametersViewModel(); // 기본 View 설정 CurrentViewModel = HomeVM; NavigateHomeCommand = new RelayCommand(o => CurrentViewModel = HomeVM); NavigateInOutCommand = new RelayCommand(o => { if (CurrentViewModel == InOutVM) CurrentViewModel = HomeVM; else CurrentViewModel = InOutVM; }); NavigateDataCommand = new RelayCommand(o => { if (CurrentViewModel == DataVM) CurrentViewModel = HomeVM; else CurrentViewModel = DataVM; }); NavigateParametersCommand = new RelayCommand(o => { var win = new leak_test_project.Views.ParametersWindow(); win.Owner = System.Windows.Application.Current.MainWindow; win.ShowDialog(); }); NavigateCommunicationCommand = new RelayCommand(o => { var win = new leak_test_project.Views.CommunicationWindow(); win.Owner = System.Windows.Application.Current.MainWindow; win.ShowDialog(); }); ExitCommand = new RelayCommand(o => { Dispose(); System.Windows.Application.Current.MainWindow?.Close(); }); } public void Dispose() { _ioService?.SetOutput("PC_ON", false); HomeVM?.Dispose(); } } }