Skip to content

Commit

Permalink
Code optimization, function asynchrony
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 21, 2024
1 parent a866017 commit 3dd5431
Show file tree
Hide file tree
Showing 22 changed files with 250 additions and 258 deletions.
15 changes: 3 additions & 12 deletions v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,21 @@ public AddServer2ViewModel(ProfileItem profileItem, Func<EViewAction, object?, T
_config = AppHandler.Instance.Config;
_updateView = updateView;

if (profileItem.indexId.IsNullOrEmpty())
{
SelectedSource = profileItem;
}
else
{
SelectedSource = JsonUtils.DeepCopy(profileItem);
}
CoreType = SelectedSource?.coreType?.ToString();

BrowseServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
_updateView?.Invoke(EViewAction.BrowseServer, null);
});

EditServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await EditServer();
});

SaveServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await SaveServerAsync();
});

SelectedSource = profileItem.indexId.IsNullOrEmpty() ? profileItem : JsonUtils.DeepCopy(profileItem);
CoreType = SelectedSource?.coreType?.ToString();
}

private async Task SaveServerAsync()
Expand Down
11 changes: 5 additions & 6 deletions v2rayN/ServiceLib/ViewModels/AddServerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ public class AddServerViewModel : MyReactiveObject
public AddServerViewModel(ProfileItem profileItem, Func<EViewAction, object?, Task<bool>>? updateView)
{
_config = AppHandler.Instance.Config;

_updateView = updateView;

SaveCmd = ReactiveCommand.CreateFromTask(async () =>
{
await SaveServerAsync();
});

if (profileItem.indexId.IsNullOrEmpty())
{
profileItem.network = Global.DefaultNetwork;
Expand All @@ -33,11 +37,6 @@ public AddServerViewModel(ProfileItem profileItem, Func<EViewAction, object?, Ta
SelectedSource = JsonUtils.DeepCopy(profileItem);
}
CoreType = SelectedSource?.coreType?.ToString();

SaveCmd = ReactiveCommand.CreateFromTask(async () =>
{
await SaveServerAsync();
});
}

private async Task SaveServerAsync()
Expand Down
1 change: 0 additions & 1 deletion v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public BackupAndRestoreViewModel(Func<EViewAction, object?, Task<bool>>? updateV
{
await WebDavCheck();
});

RemoteBackupCmd = ReactiveCommand.CreateFromTask(async () =>
{
await RemoteBackup();
Expand Down
55 changes: 24 additions & 31 deletions v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,25 @@ public class CheckUpdateViewModel : MyReactiveObject
public IObservableCollection<CheckUpdateItem> CheckUpdateItems => _checkUpdateItem;
public ReactiveCommand<Unit, Unit> CheckUpdateCmd { get; }
[Reactive] public bool EnableCheckPreReleaseUpdate { get; set; }
[Reactive] public bool IsCheckUpdate { get; set; }
[Reactive] public bool AutoRun { get; set; }

public CheckUpdateViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
{
_config = AppHandler.Instance.Config;
_updateView = updateView;

RefreshSubItems();

CheckUpdateCmd = ReactiveCommand.CreateFromTask(async () =>
{
await CheckUpdate()
.ContinueWith(t =>
{
_ = UpdateFinished();
});
await CheckUpdate();
});

EnableCheckPreReleaseUpdate = _config.guiItem.checkPreReleaseUpdate;
IsCheckUpdate = true;

this.WhenAnyValue(
x => x.EnableCheckPreReleaseUpdate,
y => y == true)
.Subscribe(c => { _config.guiItem.checkPreReleaseUpdate = EnableCheckPreReleaseUpdate; });

RefreshSubItems();
}

private void RefreshSubItems()
Expand Down Expand Up @@ -86,31 +80,31 @@ private async Task CheckUpdate()
_lstUpdated = _checkUpdateItem.Where(x => x.IsSelected == true)
.Select(x => new CheckUpdateItem() { CoreType = x.CoreType }).ToList();

for (int k = _checkUpdateItem.Count - 1; k >= 0; k--)
for (var k = _checkUpdateItem.Count - 1; k >= 0; k--)
{
var item = _checkUpdateItem[k];
if (item.IsSelected == true)
if (item.IsSelected != true) continue;

UpdateView(item.CoreType, "...");
if (item.CoreType == _geo)
{
IsCheckUpdate = false;
UpdateView(item.CoreType, "...");
if (item.CoreType == _geo)
{
await CheckUpdateGeo();
}
else if (item.CoreType == _v2rayN)
{
await CheckUpdateN(EnableCheckPreReleaseUpdate);
}
else if (item.CoreType == ECoreType.mihomo.ToString())
{
await CheckUpdateCore(item, false);
}
else
{
await CheckUpdateCore(item, EnableCheckPreReleaseUpdate);
}
await CheckUpdateGeo();
}
else if (item.CoreType == _v2rayN)
{
await CheckUpdateN(EnableCheckPreReleaseUpdate);
}
else if (item.CoreType == ECoreType.mihomo.ToString())
{
await CheckUpdateCore(item, false);
}
else
{
await CheckUpdateCore(item, EnableCheckPreReleaseUpdate);
}
}

await UpdateFinished();
}

private void UpdatedPlusPlus(string coreType, string fileName)
Expand Down Expand Up @@ -204,7 +198,6 @@ public void UpdateFinishedResult(bool blReload)
{
if (blReload)
{
IsCheckUpdate = true;
Locator.Current.GetService<MainWindowViewModel>()?.Reload();
}
else
Expand Down
38 changes: 18 additions & 20 deletions v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace ServiceLib.ViewModels
public class ClashConnectionsViewModel : MyReactiveObject
{
private IObservableCollection<ClashConnectionModel> _connectionItems = new ObservableCollectionExtended<ClashConnectionModel>();

public IObservableCollection<ClashConnectionModel> ConnectionItems => _connectionItems;

[Reactive]
Expand Down Expand Up @@ -42,13 +41,12 @@ public ClashConnectionsViewModel(Func<EViewAction, object?, Task<bool>>? updateV
this.WhenAnyValue(
x => x.SortingSelected,
y => y >= 0)
.Subscribe(c => DoSortingSelected(c));
.Subscribe(async c => await DoSortingSelected(c));

this.WhenAnyValue(
x => x.AutoRefresh,
y => y == true)
.Subscribe(c => { _config.clashUIItem.connectionsAutoRefresh = AutoRefresh; });

ConnectionCloseCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ClashConnectionClose(false);
Expand All @@ -62,26 +60,12 @@ public ClashConnectionsViewModel(Func<EViewAction, object?, Task<bool>>? updateV
Init();
}

private void DoSortingSelected(bool c)
{
if (!c)
{
return;
}
if (SortingSelected != _config.clashUIItem.connectionsSorting)
{
_config.clashUIItem.connectionsSorting = SortingSelected;
}

GetClashConnections();
}

private void Init()
private async Task Init()
{
var lastTime = DateTime.Now;

Observable.Interval(TimeSpan.FromSeconds(5))
.Subscribe(x =>
.Subscribe(async x =>
{
if (!(AutoRefresh && _config.uiItem.showInTaskbar && _config.IsRunningCore(ECoreType.sing_box)))
{
Expand All @@ -92,14 +76,28 @@ private void Init()
{
if ((dtNow - lastTime).Minutes % _config.clashUIItem.connectionsRefreshInterval == 0)
{
GetClashConnections();
await GetClashConnections();
lastTime = dtNow;
}
Task.Delay(1000).Wait();
}
});
}

private async Task DoSortingSelected(bool c)
{
if (!c)
{
return;
}
if (SortingSelected != _config.clashUIItem.connectionsSorting)
{
_config.clashUIItem.connectionsSorting = SortingSelected;
}

await GetClashConnections();
}

private async Task GetClashConnections()
{
var ret = await ClashApiHandler.Instance.GetClashConnectionsAsync(_config);
Expand Down
58 changes: 31 additions & 27 deletions v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,26 @@ public ClashProxiesViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
_config = AppHandler.Instance.Config;
_updateView = updateView;

ProxiesReloadCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ProxiesReload();
});
ProxiesDelaytestCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ProxiesDelayTest(true);
});

ProxiesDelaytestPartCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ProxiesDelayTest(false);
});
ProxiesSelectActivityCmd = ReactiveCommand.CreateFromTask(async () =>
{
await SetActiveProxy();
});

SelectedGroup = new();
SelectedDetail = new();

AutoRefresh = _config.clashUIItem.proxiesAutoRefresh;
SortingSelected = _config.clashUIItem.proxiesSorting;
RuleModeSelected = (int)_config.clashUIItem.ruleMode;
Expand All @@ -61,7 +78,7 @@ public ClashProxiesViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
this.WhenAnyValue(
x => x.RuleModeSelected,
y => y >= 0)
.Subscribe(c => DoRulemodeSelected(c));
.Subscribe(async c => await DoRulemodeSelected(c));

this.WhenAnyValue(
x => x.SortingSelected,
Expand All @@ -73,29 +90,16 @@ public ClashProxiesViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
y => y == true)
.Subscribe(c => { _config.clashUIItem.proxiesAutoRefresh = AutoRefresh; });

ProxiesReloadCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ProxiesReload();
});
ProxiesDelaytestCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ProxiesDelayTest(true);
});

ProxiesDelaytestPartCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ProxiesDelayTest(false);
});
ProxiesSelectActivityCmd = ReactiveCommand.CreateFromTask(async () =>
{
await SetActiveProxy();
});
Init();
}

ProxiesReload();
DelayTestTask();
private async Task Init()
{
await ProxiesReload();
await DelayTestTask();
}

private void DoRulemodeSelected(bool c)
private async Task DoRulemodeSelected(bool c)
{
if (!c)
{
Expand All @@ -105,16 +109,16 @@ private void DoRulemodeSelected(bool c)
{
return;
}
SetRuleModeCheck((ERuleMode)RuleModeSelected);
await SetRuleModeCheck((ERuleMode)RuleModeSelected);
}

public void SetRuleModeCheck(ERuleMode mode)
public async Task SetRuleModeCheck(ERuleMode mode)
{
if (_config.clashUIItem.ruleMode == mode)
{
return;
}
SetRuleMode(mode);
await SetRuleMode(mode);
}

private void DoSortingSelected(bool c)
Expand Down Expand Up @@ -385,7 +389,7 @@ private async Task ProxiesDelayTest(bool blAll)
{
if (item == null)
{
GetClashProxies(true);
await GetClashProxies(true);
return;
}
if (Utils.IsNullOrEmpty(result))
Expand Down Expand Up @@ -427,7 +431,7 @@ public void ProxiesDelayTestResult(SpeedTestResult result)

#region task

public void DelayTestTask()
public async Task DelayTestTask()
{
var lastTime = DateTime.Now;

Expand Down
Loading

0 comments on commit 3dd5431

Please sign in to comment.