// // MaaWpfGui - A part of the MaaCoreArknights project // Copyright (C) 2021 MistEO and Contributors // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY // using System.Threading.Tasks; using System.Windows; using MaaWpfGui.Main; using Stylet; using StyletIoC; namespace MaaWpfGui.ViewModels.UI { /// /// The root view model. /// public class RootViewModel : Conductor.Collection.OneActive { private readonly IWindowManager _windowManager; private readonly AsstProxy _asstProxy; private readonly TaskQueueViewModel _taskQueueViewModel; private readonly RecruitViewModel _recruitViewModel; private readonly SettingsViewModel _settingsViewModel; private readonly CopilotViewModel _copilotViewModel; private readonly DepotViewModel _depotViewModel; private readonly VersionUpdateViewModel _versionUpdateViewModel; /// /// Initializes a new instance of the class. /// /// The IoC container. /// The window manager. public RootViewModel(IContainer container, IWindowManager windowManager) { _windowManager = windowManager; _asstProxy = container.Get(); _taskQueueViewModel = container.Get(); _recruitViewModel = container.Get(); _settingsViewModel = container.Get(); _copilotViewModel = container.Get(); _depotViewModel = container.Get(); _versionUpdateViewModel = container.Get(); } /// protected override void OnViewLoaded() { CheckAndUpdateNow(); InitViewModels(); InitProxy(); ShowUpdateOrDownload(); } private async void InitProxy() { await Task.Run(_asstProxy.Init); } private void InitViewModels() { Items.Add(_taskQueueViewModel); Items.Add(_copilotViewModel); Items.Add(_recruitViewModel); Items.Add(_depotViewModel); Items.Add(_settingsViewModel); _settingsViewModel.UpdateWindowTitle(); // 在标题栏上显示模拟器和IP端口 必须在 Items.Add(settings)之后执行。 ActiveItem = _taskQueueViewModel; } private bool CheckAndUpdateNow() { return _versionUpdateViewModel.CheckAndUpdateNow(); } private async void ShowUpdateOrDownload() { if (_versionUpdateViewModel.IsFirstBootAfterUpdate) { _versionUpdateViewModel.IsFirstBootAfterUpdate = false; _windowManager.ShowWindow(_versionUpdateViewModel); } else { var ret = await Task.Run(() => _versionUpdateViewModel.CheckAndDownloadUpdate()); if (ret == VersionUpdateViewModel.CheckUpdateRetT.OK) { _versionUpdateViewModel.AskToRestart(); } } } private string _windowTitle = "MAA"; /// /// Gets or sets the window title. /// public string WindowTitle { get => _windowTitle; set => SetAndNotify(ref _windowTitle, value); } protected override void OnInitialActivate() { base.OnInitialActivate(); // TrayIcon应该在显示rootViewModel之后再加载 Bootstrapper.SetTrayIconInSettingsViewModel(_settingsViewModel); } /// protected override void OnClose() { Application.Current.Shutdown(); } } }