mirror of
https://github.com/2dust/v2rayN.git
synced 2025-10-14 04:19:12 +00:00
Update MsgViewModel.cs
This commit is contained in:
parent
a65eeb561a
commit
e9daf26bcf
1 changed files with 23 additions and 51 deletions
|
@ -1,5 +1,6 @@
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Reactive.Linq;
|
using System.Reactive.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using ReactiveUI;
|
using ReactiveUI;
|
||||||
using ReactiveUI.Fody.Helpers;
|
using ReactiveUI.Fody.Helpers;
|
||||||
|
@ -13,11 +14,8 @@ public class MsgViewModel : MyReactiveObject
|
||||||
private bool _lastMsgFilterNotAvailable;
|
private bool _lastMsgFilterNotAvailable;
|
||||||
private bool _blLockShow = false;
|
private bool _blLockShow = false;
|
||||||
|
|
||||||
[Reactive]
|
[Reactive] public string MsgFilter { get; set; }
|
||||||
public string MsgFilter { get; set; }
|
[Reactive] public bool AutoRefresh { get; set; }
|
||||||
|
|
||||||
[Reactive]
|
|
||||||
public bool AutoRefresh { get; set; }
|
|
||||||
|
|
||||||
public MsgViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
|
public MsgViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
|
||||||
{
|
{
|
||||||
|
@ -26,63 +24,43 @@ public class MsgViewModel : MyReactiveObject
|
||||||
MsgFilter = _config.MsgUIItem.MainMsgFilter ?? string.Empty;
|
MsgFilter = _config.MsgUIItem.MainMsgFilter ?? string.Empty;
|
||||||
AutoRefresh = _config.MsgUIItem.AutoRefresh ?? true;
|
AutoRefresh = _config.MsgUIItem.AutoRefresh ?? true;
|
||||||
|
|
||||||
this.WhenAnyValue(
|
this.WhenAnyValue(x => x.MsgFilter).Subscribe(c => DoMsgFilter());
|
||||||
x => x.MsgFilter)
|
this.WhenAnyValue(x => x.AutoRefresh, y => y == true)
|
||||||
.Subscribe(c => DoMsgFilter());
|
|
||||||
|
|
||||||
this.WhenAnyValue(
|
|
||||||
x => x.AutoRefresh,
|
|
||||||
y => y == true)
|
|
||||||
.Subscribe(c => { _config.MsgUIItem.AutoRefresh = AutoRefresh; });
|
.Subscribe(c => { _config.MsgUIItem.AutoRefresh = AutoRefresh; });
|
||||||
|
|
||||||
AppEvents.SendMsgViewRequested
|
AppEvents.SendMsgViewRequested
|
||||||
.AsObservable()
|
.AsObservable()
|
||||||
//.ObserveOn(RxApp.MainThreadScheduler)
|
|
||||||
.Subscribe(async content => await AppendQueueMsg(content));
|
.Subscribe(async content => await AppendQueueMsg(content));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task AppendQueueMsg(string msg)
|
private async Task AppendQueueMsg(string msg)
|
||||||
{
|
{
|
||||||
//if (msg == Global.CommandClearMsg)
|
if (!AutoRefresh) return;
|
||||||
//{
|
await EnqueueQueueMsg(msg);
|
||||||
// ClearMsg();
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
if (AutoRefresh == false)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_ = EnqueueQueueMsg(msg);
|
|
||||||
|
|
||||||
if (_blLockShow)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!_config.UiItem.ShowInTaskbar)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (_blLockShow || !_config.UiItem.ShowInTaskbar) return;
|
||||||
_blLockShow = true;
|
_blLockShow = true;
|
||||||
|
|
||||||
await Task.Delay(500);
|
await Task.Delay(500);
|
||||||
var txt = string.Join("", _queueMsg.ToArray());
|
|
||||||
await _updateView?.Invoke(EViewAction.DispatcherShowMsg, txt);
|
var sb = new StringBuilder();
|
||||||
|
while (_queueMsg.TryDequeue(out var line))
|
||||||
|
{
|
||||||
|
sb.Append(line);
|
||||||
|
}
|
||||||
|
if (sb.Length > 0)
|
||||||
|
await _updateView?.Invoke(EViewAction.DispatcherShowMsg, sb.ToString());
|
||||||
|
|
||||||
_blLockShow = false;
|
_blLockShow = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task EnqueueQueueMsg(string msg)
|
private async Task EnqueueQueueMsg(string msg)
|
||||||
{
|
{
|
||||||
//filter msg
|
|
||||||
if (MsgFilter.IsNotEmpty() && !_lastMsgFilterNotAvailable)
|
if (MsgFilter.IsNotEmpty() && !_lastMsgFilterNotAvailable)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!Regex.IsMatch(msg, MsgFilter))
|
if (!Regex.IsMatch(msg, MsgFilter)) return;
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -91,26 +69,20 @@ public class MsgViewModel : MyReactiveObject
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Enqueue
|
|
||||||
if (_queueMsg.Count > _numMaxMsg)
|
if (_queueMsg.Count > _numMaxMsg)
|
||||||
{
|
{
|
||||||
for (int k = 0; k < _queueMsg.Count - _numMaxMsg; k++)
|
for (int k = 0; k < _queueMsg.Count - _numMaxMsg; k++)
|
||||||
{
|
|
||||||
_queueMsg.TryDequeue(out _);
|
_queueMsg.TryDequeue(out _);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
_queueMsg.Enqueue(msg);
|
_queueMsg.Enqueue(msg);
|
||||||
if (!msg.EndsWith(Environment.NewLine))
|
if (!msg.EndsWith(Environment.NewLine))
|
||||||
{
|
|
||||||
_queueMsg.Enqueue(Environment.NewLine);
|
_queueMsg.Enqueue(Environment.NewLine);
|
||||||
}
|
|
||||||
await Task.CompletedTask;
|
await Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ClearMsg()
|
public void ClearMsg() => _queueMsg.Clear();
|
||||||
{
|
|
||||||
_queueMsg.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DoMsgFilter()
|
private void DoMsgFilter()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue