Files
MaaAssistantArknights/src/MeoAsstGui/ViewModels/RootViewModel.cs
2022-07-05 17:08:23 +08:00

108 lines
3.0 KiB
C#

// MeoAsstGui - A part of the MeoAssistantArknights 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;
using System.Threading.Tasks;
using Stylet;
using StyletIoC;
namespace MeoAsstGui
{
public class RootViewModel : Conductor<Screen>.Collection.OneActive
{
private TrayIcon _trayIcon;
private readonly IContainer _container;
private readonly IWindowManager _windowManager;
public RootViewModel(IContainer container, IWindowManager windowManager)
{
_container = container;
_windowManager = windowManager;
}
protected override void OnViewLoaded()
{
_trayIcon = new TrayIcon();
CheckAndUpdateNow();
InitViewModels();
InitProxy();
ShowUpdateOrDownload();
}
private async void InitProxy()
{
var task = Task.Run(() =>
{
var p = _container.Get<AsstProxy>();
p.Init();
});
await task;
}
private void InitViewModels()
{
var tvm = _container.Get<TaskQueueViewModel>();
var rvm = _container.Get<RecruitViewModel>();
//var ivm = _container.Get<InfrastViewModel>();
var svm = _container.Get<SettingsViewModel>();
var cvm = _container.Get<CopilotViewModel>();
Items.Add(tvm);
Items.Add(rvm);
//Items.Add(ivm);
Items.Add(cvm);
Items.Add(svm);
svm.UpdateWindowTitle(); // 在标题栏上显示模拟器和IP端口 必须在 Items.Add(svm)之后执行。
ActiveItem = tvm;
}
private bool CheckAndUpdateNow()
{
var vuvm = _container.Get<VersionUpdateViewModel>();
return vuvm.CheckAndUpdateNow();
}
private async void ShowUpdateOrDownload()
{
var vuvm = _container.Get<VersionUpdateViewModel>();
if (vuvm.IsFirstBootAfterUpdate)
{
vuvm.IsFirstBootAfterUpdate = false;
_windowManager.ShowWindow(vuvm);
}
else
{
var task = Task.Run(() =>
{
vuvm.CheckAndDownloadUpdate();
});
await task;
}
}
private string _windowTitle = "MaaAssistantArknights";
public string WindowTitle
{
get => _windowTitle;
set => SetAndNotify(ref _windowTitle, value);
}
protected override void OnClose()
{
System.Windows.Application.Current.Shutdown();
}
}
}