改进加速计时逻辑

This commit is contained in:
rmbgame
2021-06-29 23:40:28 +08:00
parent ebfcb9779b
commit 7d9607b97e
17 changed files with 124 additions and 27 deletions
@@ -27,11 +27,27 @@ namespace System.Application.UI.Views
_isOpenWindow = false;
e.Cancel = true;
Hide();
if (this.ViewModel is not null)
foreach (var tab in this.ViewModel.TabItems)
{
tab.Deactivation();
}
}
#endif
base.OnClosed(e);
}
protected override void FluentWindow_Opened(object? sender, EventArgs e)
{
if (this.ViewModel is not null)
foreach (var tab in this.ViewModel.TabItems)
{
tab.Activation();
}
base.FluentWindow_Opened(sender, e);
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
@@ -4,11 +4,12 @@ using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Selection;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using System.Application.UI.ViewModels;
namespace System.Application.UI.Views.Pages
{
public class CommunityProxyPage : UserControl
public class CommunityProxyPage : ReactiveUserControl<CommunityProxyPageViewModel>
{
//private ListBox _items;
//private IScrollable _scroll;
@@ -1,10 +1,11 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using System.Application.UI.ViewModels;
namespace System.Application.UI.Views.Pages
{
public class DebugPage : UserControl
public class DebugPage : ReactiveUserControl<DebugPageViewModel>
{
public DebugPage()
{
@@ -2,12 +2,13 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using CefNet.Avalonia;
using System.Application.UI.Views.Controls;
namespace System.Application.UI.Views.Pages
{
public class DebugWebViewPage : UserControl, IDisposable
public class DebugWebViewPage : ReactiveUserControl<DebugWebViewPage>, IDisposable
{
readonly WebView3 webView;
readonly TextBox urlTextBox;
@@ -1,10 +1,11 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using System.Application.UI.ViewModels;
namespace System.Application.UI.Views.Pages
{
public class StartPage : UserControl
public class StartPage : ReactiveUserControl<StartPageViewModel>
{
public StartPage()
{
@@ -1,10 +1,11 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using System.Application.UI.ViewModels;
namespace System.Application.UI.Views.Pages
{
public class SteamAccountPage : UserControl
public class SteamAccountPage : ReactiveUserControl<SteamAccountPageViewModel>
{
public SteamAccountPage()
{
@@ -180,7 +180,7 @@ namespace Avalonia.Controls
}
protected bool _isOpenWindow;
private void FluentWindow_Opened(object? sender, EventArgs e)
protected virtual void FluentWindow_Opened(object? sender, EventArgs e)
{
_isOpenWindow = true;
if (this.DataContext is WindowViewModel vm)
@@ -86,8 +86,10 @@ namespace System.Application.Services
}
}
private DateTime _AccelerateTime;
public DateTime AccelerateTime
private DateTimeOffset _StartAccelerateTime;
private DateTimeOffset _AccelerateTime;
public DateTimeOffset AccelerateTime
{
get => _AccelerateTime;
set
@@ -100,6 +102,7 @@ namespace System.Application.Services
}
}
public bool IsEnableScript
{
get => ProxySettings.IsEnableScript.Value;
@@ -224,7 +227,8 @@ namespace System.Application.Services
}
}
}
StartTiming();
_StartAccelerateTime = DateTimeOffset.Now;
StartTimer();
Toast.Show(AppResources.CommunityFix_StartProxySuccess);
}
else
@@ -235,6 +239,7 @@ namespace System.Application.Services
else
{
httpProxyService.StopProxy();
StopTimer();
void OnStopRemoveHostsByTag()
{
var needClear = IHostsFileService.Instance.ContainsHostsByTag();
@@ -416,18 +421,21 @@ namespace System.Application.Services
}
}
public void StartTiming()
private Timer timer;
public void StartTimer()
{
AccelerateTime = new();
Task.Run(() =>
AccelerateTime = new DateTimeOffset().Add((DateTimeOffset.Now - _StartAccelerateTime));
timer = new Timer((state) =>
{
Thread.CurrentThread.IsBackground = true;
while (ProxyStatus)
{
AccelerateTime = AccelerateTime.AddSeconds(1);
Thread.Sleep(1000);
}
});
AccelerateTime = AccelerateTime.AddSeconds(1);
}, nameof(AccelerateTime), 1000, 1000);
}
public void StopTimer()
{
timer.Dispose();
}
public static void OnExitRestoreHosts()
@@ -70,7 +70,7 @@ namespace System.Application.UI.ViewModels
});
}
internal async virtual void Initialize()
public async virtual void Initialize()
{
await Task.CompletedTask;
}
@@ -58,7 +58,7 @@ namespace System.Application.UI.ViewModels
}
protected bool IsFirstActivation = true;
internal async virtual void Activation()
public async virtual void Activation()
{
if (IsFirstActivation)
{
@@ -67,7 +67,7 @@ namespace System.Application.UI.ViewModels
await Task.CompletedTask;
}
internal async virtual void Deactivation()
public async virtual void Deactivation()
{
await Task.CompletedTask;
}
@@ -90,9 +90,27 @@ namespace System.Application.UI.ViewModels
AutoRunProxy?.CheckmarkChange(ProxySettings.ProgramStartupRunProxy.Value);
}
public override void Activation()
{
if (ProxyService.Current.ProxyStatus)
{
ProxyService.Current.StartTimer();
}
base.Activation();
}
internal async override void Initialize()
public override void Deactivation()
{
if (ProxyService.Current.ProxyStatus)
{
ProxyService.Current.StopTimer();
}
base.Deactivation();
}
public async override void Initialize()
{
await Task.CompletedTask;
}
@@ -150,7 +150,7 @@ namespace System.Application.UI.ViewModels
public ReactiveCommand<Unit, Unit> HideAppCommand { get; }
public ReactiveCommand<Unit, Unit> IdleAppCommand { get; }
internal override void Activation()
public override void Activation()
{
if (IsFirstActivation)
{
@@ -101,7 +101,7 @@ namespace System.Application.UI.ViewModels
public ReactiveCommand<Unit, Unit> ExportAuthCommand { get; }
internal async override void Activation()
public async override void Activation()
{
SourceAuthCount = await AuthService.Current.GetRealAuthenticatorCount();
//if (IsFirstActivation)
@@ -23,7 +23,7 @@ namespace System.Application.UI.ViewModels
protected set { throw new NotImplementedException(); }
}
internal override void Activation()
public override void Activation()
{
if (IsFirstActivation)
if (ProxySettings.IsAutoCheckScriptUpdate)
@@ -84,7 +84,7 @@ namespace System.Application.UI.ViewModels
public bool IsUserEmpty => !SteamUsers.Any_Nullable();
internal async override void Initialize()
public async override void Initialize()
{
var list = steamService.GetRememberUserList();
@@ -1,11 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Steam;
namespace System.Application.Services.Implementation
{
public class ArchiSteamFarmServiceImpl : IArchiSteamFarmService
{
public Func<string, Task<string?>>? GetConsoleWirteFunc { get; set; }
public async void Start(string[]? args = null)
{
try
@@ -17,5 +23,49 @@ namespace System.Application.Services.Implementation
Toast.Show(ex.Message);
}
}
/// <summary>
/// 执行asf指令
/// </summary>
/// <param name="command"></param>
public async Task<string?> ExecuteCommand(string command)
{
Bot? targetBot = Bot.Bots?.OrderBy(bot => bot.Key, Bot.BotsComparer).Select(bot => bot.Value).FirstOrDefault();
if (targetBot == null)
{
//Console.WriteLine(@"<< " + Strings.ErrorNoBotsDefined);
return null;
}
//Console.WriteLine(@"<> " + Strings.Executing);
ulong steamOwnerID = ASF.GlobalConfig?.SteamOwnerID ?? ArchiSteamFarm.Storage.GlobalConfig.DefaultSteamOwnerID;
string? response = await targetBot.Commands.Response(steamOwnerID, command!).ConfigureAwait(false);
return response;
}
/// <summary>
/// 获取bot只读集合
/// </summary>
/// <returns></returns>
public IReadOnlyDictionary<string, Bot>? GetReadOnlyAllBots()
{
return Bot.BotsReadOnly;
}
/// <summary>
/// 设置并保存bot
/// </summary>
/// <returns></returns>
public void SetBot(string botName)
{
}
}
}