1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-22 05:06:08 +03:00
ProxySU/ProxySuper.Core/ViewModels/MTProtoGoInstallViewModel.cs

95 lines
2.4 KiB
C#
Raw Permalink Normal View History

2021-08-23 07:00:39 +03:00
using MvvmCross.Commands;
using MvvmCross.ViewModels;
using ProxySuper.Core.Models;
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects;
using ProxySuper.Core.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProxySuper.Core.ViewModels
{
public class MTProtoGoInstallViewModel : MvxViewModel<Record>
2021-08-23 07:00:39 +03:00
{
Host _host;
MTProtoGoSettings _settings;
2021-08-23 07:00:39 +03:00
MTProtoGoService _mtproxyService;
2021-08-23 07:00:39 +03:00
2021-08-23 10:59:34 +03:00
Action _onSave;
2021-08-23 07:00:39 +03:00
public override void Prepare(Record parameter)
{
_host = parameter.Host;
_settings = parameter.MTProtoGoSettings;
2021-08-23 10:59:34 +03:00
_onSave = parameter.OnSave;
2021-08-23 07:00:39 +03:00
}
public override Task Initialize()
{
_mtproxyService = new MTProtoGoService(_host, _settings);
2021-08-23 07:00:39 +03:00
_mtproxyService.Progress.StepUpdate = () => RaisePropertyChanged("Progress");
_mtproxyService.Progress.LogsUpdate = () => RaisePropertyChanged("Logs");
_mtproxyService.Connect();
return base.Initialize();
}
public override void ViewDestroy(bool viewFinishing = true)
{
_mtproxyService.Disconnect();
this.SaveInstallLog();
base.ViewDestroy(viewFinishing);
}
public ProjectProgress Progress
{
get => _mtproxyService.Progress;
}
public string Logs
{
get => _mtproxyService.Progress.Logs;
}
#region Command
2021-08-23 10:59:34 +03:00
public IMvxCommand InstallCommand => new MvxCommand(() =>
{
_mtproxyService.Install();
// 安装时生成的Secret需要保存
_onSave();
});
public IMvxCommand UpdateSettingsCommand => new MvxCommand(() =>
{
_mtproxyService.UpdateSettings();
2021-08-23 07:00:39 +03:00
2021-08-23 10:59:34 +03:00
// 安装时生成的Secret需要保存
_onSave();
});
2021-08-23 07:00:39 +03:00
public IMvxCommand UninstallCommand => new MvxCommand(_mtproxyService.Uninstall);
#endregion
private void SaveInstallLog()
{
if (!Directory.Exists("Logs"))
{
Directory.CreateDirectory("Logs");
}
var fileName = System.IO.Path.Combine("Logs", DateTime.Now.ToString("yyyy-MM-dd hh-mm") + ".mtproxy-go.txt");
File.WriteAllText(fileName, Logs);
}
}
}