mirror of
https://github.com/2dust/v2rayN.git
synced 2026-10-14 01:04:59 +03:00
ViewModel-First (#9621)
* Interaction * Remove view action * ViewModel-first * MainGirdOrientation Hot Reload * Fix avalonia preview * Remove CloseWindowInteraction * Fix * Avoid threading issues * Fix * ProfilesSelect * Try fix previewer * Remove AppEvents.ProfilesRefreshRequested * Remove AppEvents.SubscriptionsRefreshRequested * Remove AppEvents.ProxiesReloadRequested * Remove AppEvents.AdjustMainLvColWidthRequested * Remove AppEvents.SetDefaultServerRequested * Remove AppEvents.RoutingsMenuRefreshRequested * Remove AppEvents.TestServerRequested * Remove AppEvents.InboundDisplayRequested * Remove AppEvents.SubscriptionsUpdateRequested * Remove AppEvents.ShowHideWindowRequested * Remove AppEvents.ReloadRequested * Remove AppEvents.AddServerRequested * Fix
This commit is contained in:
@@ -4,6 +4,21 @@ namespace ServiceLib.ViewModels;
|
||||
|
||||
public class MainWindowViewModel : MyReactiveObject
|
||||
{
|
||||
public Interaction<Unit, string?> ReadTextFromClipboardInteraction { get; } = new();
|
||||
public Interaction<Unit, byte[]?> ScanScreenInteraction { get; } = new();
|
||||
public Interaction<Unit, string?> BrowseImageFileInteraction { get; } = new();
|
||||
public Interaction<bool?, Unit> ShowHideWindowInteraction { get; } = new();
|
||||
|
||||
public bool DesignMode { get; set; }
|
||||
|
||||
public ProfilesViewModel ProfilesViewModel { get; } = new();
|
||||
public MsgViewModel MsgViewModel { get; } = new();
|
||||
public ClashProxiesViewModel ClashProxiesViewModel { get; } = new();
|
||||
public ClashConnectionsViewModel ClashConnectionsViewModel { get; } = new();
|
||||
public CheckUpdateViewModel CheckUpdateViewModel { get; } = new();
|
||||
public BackupAndRestoreViewModel BackupAndRestoreViewModel { get; } = new();
|
||||
public StatusBarViewModel StatusBarViewModel { get; } = StatusBarViewModel.Instance;
|
||||
|
||||
#region Menu
|
||||
|
||||
//servers
|
||||
@@ -67,15 +82,17 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
[Reactive] public bool BlNewUpdate { get; set; }
|
||||
|
||||
[Reactive] public EGirdOrientation MainGirdOrientation { get; set; }
|
||||
|
||||
#endregion Menu
|
||||
|
||||
#region Init
|
||||
|
||||
public MainWindowViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
|
||||
public MainWindowViewModel()
|
||||
{
|
||||
_config = AppManager.Instance.Config;
|
||||
_updateView = updateView;
|
||||
BlIsWindows = Utils.IsWindows();
|
||||
MainGirdOrientation = _config.UiItem.MainGirdOrientation;
|
||||
|
||||
#region WhenAnyValue && ReactiveCommand
|
||||
|
||||
@@ -191,7 +208,8 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
});
|
||||
GlobalHotkeySettingCmd = ReactiveCommand.CreateFromTask(async () =>
|
||||
{
|
||||
if (await _updateView?.Invoke(EViewAction.GlobalHotkeySettingWindow, null) == true)
|
||||
var globalHotkeySettingViewModel = new GlobalHotkeySettingViewModel();
|
||||
if (await AppManager.Instance.WindowDialog.ShowDialogAsync(globalHotkeySettingViewModel) == true)
|
||||
{
|
||||
NoticeManager.Instance.Enqueue(ResUI.OperationSuccess);
|
||||
}
|
||||
@@ -233,26 +251,11 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
#region AppEvents
|
||||
|
||||
AppEvents.ReloadRequested
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async _ => await Reload());
|
||||
|
||||
AppEvents.AddServerViaScanRequested
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async _ => await AddServerViaScanAsync());
|
||||
|
||||
AppEvents.AddServerViaClipboardRequested
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async _ => await AddServerViaClipboardAsync(null));
|
||||
|
||||
AppEvents.SubscriptionsUpdateRequested
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async blProxy => await UpdateSubscriptionProcess("", blProxy));
|
||||
|
||||
AppEvents.HasUpdateNotified
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
@@ -260,6 +263,48 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
#endregion AppEvents
|
||||
|
||||
var vmReloadRequestedList = new List<IObservable<Unit>>
|
||||
{
|
||||
ProfilesViewModel.ReloadRequested.AsObservable(),
|
||||
StatusBarViewModel.ReloadRequested.AsObservable(),
|
||||
CheckUpdateViewModel.ReloadRequested.AsObservable(),
|
||||
};
|
||||
|
||||
foreach (var reloadRequested in vmReloadRequestedList)
|
||||
{
|
||||
reloadRequested
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async _ => await Reload());
|
||||
}
|
||||
|
||||
StatusBarViewModel.AddServerViaScanRequested
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async _ => await AddServerViaScanAsync());
|
||||
|
||||
StatusBarViewModel.AddServerViaClipboardRequested
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async _ => await AddServerViaClipboardAsync(null));
|
||||
|
||||
StatusBarViewModel.ShowHideWindowRequested
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async blShow =>
|
||||
{
|
||||
await ShowHideWindowInteraction.Handle(blShow);
|
||||
});
|
||||
|
||||
StatusBarViewModel.SetDefaultServerRequested
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async indexId => await ProfilesViewModel.SetDefaultServer(indexId));
|
||||
|
||||
StatusBarViewModel.SubscriptionsUpdateRequested
|
||||
.AsObservable()
|
||||
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||
.Subscribe(async blProxy => await UpdateSubscriptionProcess("", blProxy));
|
||||
|
||||
_ = Init();
|
||||
}
|
||||
|
||||
@@ -267,6 +312,11 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
{
|
||||
AppManager.Instance.ShowInTaskbar = true;
|
||||
|
||||
if (DesignMode)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//await ConfigHandler.InitBuiltinRouting(_config);
|
||||
await ConfigHandler.InitBuiltinDNS(_config);
|
||||
await ConfigHandler.InitBuiltinFullConfigTemplate(_config);
|
||||
@@ -279,7 +329,7 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
{
|
||||
await StatisticsManager.Instance.Init(_config, UpdateStatisticsHandler);
|
||||
}
|
||||
await RefreshServers();
|
||||
await RefreshServersDispatcherAsync();
|
||||
|
||||
await Reload();
|
||||
}
|
||||
@@ -304,7 +354,7 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
if (success)
|
||||
{
|
||||
var indexIdOld = _config.IndexId;
|
||||
await RefreshServers();
|
||||
await RefreshServersDispatcherAsync();
|
||||
|
||||
// If indexId changed or subIndexId is empty, directly reload.
|
||||
if (indexIdOld != _config.IndexId || _config.SubIndexId.IsNullOrEmpty())
|
||||
@@ -323,7 +373,7 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
if (_config.UiItem.EnableAutoAdjustMainLvColWidth)
|
||||
{
|
||||
AppEvents.AdjustMainLvColWidthRequested.Publish();
|
||||
await ProfilesViewModel.AdjustMainLvColWidth();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -344,14 +394,20 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
private async Task RefreshServers()
|
||||
{
|
||||
AppEvents.ProfilesRefreshRequested.Publish();
|
||||
await ProfilesViewModel.RefreshServers();
|
||||
await StatusBarViewModel.RefreshServers();
|
||||
|
||||
await Task.Delay(200);
|
||||
// await Task.Delay(200);
|
||||
}
|
||||
|
||||
private void RefreshSubscriptions()
|
||||
private async Task RefreshServersDispatcherAsync()
|
||||
{
|
||||
AppEvents.SubscriptionsRefreshRequested.Publish();
|
||||
await Observable.Start(async () => await RefreshServers(), RxSchedulers.MainThreadScheduler);
|
||||
}
|
||||
|
||||
private async Task RefreshSubscriptions()
|
||||
{
|
||||
await Observable.Start(async () => await ProfilesViewModel.RefreshSubscriptions(), RxSchedulers.MainThreadScheduler);
|
||||
}
|
||||
|
||||
#endregion Servers && Groups
|
||||
@@ -370,19 +426,22 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
bool? ret = false;
|
||||
if (eConfigType == EConfigType.Custom)
|
||||
{
|
||||
ret = await _updateView?.Invoke(EViewAction.AddServer2Window, item);
|
||||
var addServer2ViewModel = new AddServer2ViewModel(item);
|
||||
ret = await AppManager.Instance.WindowDialog.ShowDialogAsync(addServer2ViewModel);
|
||||
}
|
||||
else if (eConfigType.IsGroupType())
|
||||
{
|
||||
ret = await _updateView?.Invoke(EViewAction.AddGroupServerWindow, item);
|
||||
var addGroupServerViewModel = new AddGroupServerViewModel(item);
|
||||
ret = await AppManager.Instance.WindowDialog.ShowDialogAsync(addGroupServerViewModel);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = await _updateView?.Invoke(EViewAction.AddServerWindow, item);
|
||||
var addServerViewModel = new AddServerViewModel(item);
|
||||
ret = await AppManager.Instance.WindowDialog.ShowDialogAsync(addServerViewModel);
|
||||
}
|
||||
if (ret == true)
|
||||
{
|
||||
await RefreshServers();
|
||||
await RefreshServersDispatcherAsync();
|
||||
if (item.IndexId == _config.IndexId)
|
||||
{
|
||||
await Reload();
|
||||
@@ -392,16 +451,22 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
public async Task AddServerViaClipboardAsync(string? clipboardData)
|
||||
{
|
||||
var stringData = clipboardData;
|
||||
if (clipboardData == null)
|
||||
{
|
||||
await _updateView?.Invoke(EViewAction.AddServerViaClipboard, null);
|
||||
return;
|
||||
var result = await ReadTextFromClipboardInteraction.Handle(Unit.Default);
|
||||
if (result.IsNullOrEmpty())
|
||||
{
|
||||
NoticeManager.Instance.Enqueue(ResUI.OperationFailed);
|
||||
return;
|
||||
}
|
||||
stringData = result;
|
||||
}
|
||||
var ret = await ConfigHandler.AddBatchServers(_config, clipboardData, _config.SubIndexId, false);
|
||||
var ret = await ConfigHandler.AddBatchServers(_config, stringData, _config.SubIndexId, false);
|
||||
if (ret > 0)
|
||||
{
|
||||
RefreshSubscriptions();
|
||||
await RefreshServers();
|
||||
await RefreshSubscriptions();
|
||||
await RefreshServersDispatcherAsync();
|
||||
NoticeManager.Instance.Enqueue(string.Format(ResUI.SuccessfullyImportedServerViaClipboard, ret));
|
||||
}
|
||||
else
|
||||
@@ -412,8 +477,8 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
public async Task AddServerViaScanAsync()
|
||||
{
|
||||
_updateView?.Invoke(EViewAction.ScanScreenTask, null);
|
||||
await Task.CompletedTask;
|
||||
var result = await ScanScreenInteraction.Handle(Unit.Default);
|
||||
await ScanScreenResult(result);
|
||||
}
|
||||
|
||||
public async Task ScanScreenResult(byte[]? bytes)
|
||||
@@ -424,8 +489,8 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
public async Task AddServerViaImageAsync()
|
||||
{
|
||||
_updateView?.Invoke(EViewAction.ScanImageTask, null);
|
||||
await Task.CompletedTask;
|
||||
var imageFileName = await BrowseImageFileInteraction.Handle(Unit.Default);
|
||||
await AddScanResultAsync(imageFileName);
|
||||
}
|
||||
|
||||
public async Task ScanImageResult(string fileName)
|
||||
@@ -450,8 +515,8 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
var ret = await ConfigHandler.AddBatchServers(_config, result, _config.SubIndexId, false);
|
||||
if (ret > 0)
|
||||
{
|
||||
RefreshSubscriptions();
|
||||
await RefreshServers();
|
||||
await RefreshSubscriptions();
|
||||
await RefreshServersDispatcherAsync();
|
||||
NoticeManager.Instance.Enqueue(ResUI.SuccessfullyImportedServerViaScan);
|
||||
}
|
||||
else
|
||||
@@ -467,9 +532,10 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
private async Task SubSettingAsync()
|
||||
{
|
||||
if (await _updateView?.Invoke(EViewAction.SubSettingWindow, null) == true)
|
||||
var subSettingViewModel = new SubSettingViewModel();
|
||||
if (await AppManager.Instance.WindowDialog.ShowDialogAsync(subSettingViewModel) == true)
|
||||
{
|
||||
RefreshSubscriptions();
|
||||
await RefreshSubscriptions();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -484,28 +550,38 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
private async Task OptionSettingAsync()
|
||||
{
|
||||
var ret = await _updateView?.Invoke(EViewAction.OptionSettingWindow, null);
|
||||
var settingViewModel = new OptionSettingViewModel();
|
||||
var ret = await AppManager.Instance.WindowDialog.ShowDialogAsync(settingViewModel);
|
||||
if (ret == true)
|
||||
{
|
||||
AppEvents.InboundDisplayRequested.Publish();
|
||||
MainGirdOrientation = _config.UiItem.MainGirdOrientation;
|
||||
RxSchedulers.MainThreadScheduler.Schedule(async () =>
|
||||
{
|
||||
await StatusBarViewModel.InboundDisplayStatus();
|
||||
});
|
||||
await Reload();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RoutingSettingAsync()
|
||||
{
|
||||
var ret = await _updateView?.Invoke(EViewAction.RoutingSettingWindow, null);
|
||||
var routingSettingViewModel = new RoutingSettingViewModel();
|
||||
var ret = await AppManager.Instance.WindowDialog.ShowDialogAsync(routingSettingViewModel);
|
||||
if (ret == true)
|
||||
{
|
||||
await ConfigHandler.InitBuiltinRouting(_config);
|
||||
AppEvents.RoutingsMenuRefreshRequested.Publish();
|
||||
RxSchedulers.MainThreadScheduler.Schedule(async () =>
|
||||
{
|
||||
await StatusBarViewModel.RefreshRoutingsMenu();
|
||||
});
|
||||
await Reload();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DNSSettingAsync()
|
||||
{
|
||||
var ret = await _updateView?.Invoke(EViewAction.DNSSettingWindow, null);
|
||||
var dnsSettingViewModel = new DNSSettingViewModel();
|
||||
var ret = await AppManager.Instance.WindowDialog.ShowDialogAsync(dnsSettingViewModel);
|
||||
if (ret == true)
|
||||
{
|
||||
await Reload();
|
||||
@@ -514,7 +590,8 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
|
||||
private async Task FullConfigTemplateAsync()
|
||||
{
|
||||
var ret = await _updateView?.Invoke(EViewAction.FullConfigTemplateWindow, null);
|
||||
var fullConfigTemplateViewModel = new FullConfigTemplateViewModel();
|
||||
var ret = await AppManager.Instance.WindowDialog.ShowDialogAsync(fullConfigTemplateViewModel);
|
||||
if (ret == true)
|
||||
{
|
||||
await Reload();
|
||||
@@ -524,7 +601,7 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
private async Task ClearServerStatistics()
|
||||
{
|
||||
await StatisticsManager.Instance.ClearAllServerStatistics();
|
||||
await RefreshServers();
|
||||
await RefreshServersDispatcherAsync();
|
||||
}
|
||||
|
||||
private async Task OpenTheFileLocation()
|
||||
@@ -561,6 +638,12 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
return;
|
||||
}
|
||||
|
||||
if (DesignMode)
|
||||
{
|
||||
_reloadSemaphore.Release();
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SetReloadEnabled(false);
|
||||
@@ -583,12 +666,22 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
await SysProxyHandler.UpdateSysProxy(_config, false);
|
||||
await Task.Delay(1000);
|
||||
});
|
||||
AppEvents.TestServerRequested.Publish();
|
||||
RxSchedulers.MainThreadScheduler.Schedule(async () =>
|
||||
{
|
||||
await StatusBarViewModel.TestServerAvailability();
|
||||
});
|
||||
|
||||
var showClashUI = AppManager.Instance.IsRunningCore(ECoreType.sing_box);
|
||||
if (showClashUI)
|
||||
{
|
||||
AppEvents.ProxiesReloadRequested.Publish();
|
||||
//await Observable.Start(async () =>
|
||||
//{
|
||||
// await ClashProxiesViewModel.ProxiesReload();
|
||||
//}, RxSchedulers.MainThreadScheduler);
|
||||
RxSchedulers.MainThreadScheduler.Schedule(async () =>
|
||||
{
|
||||
await ClashProxiesViewModel.ProxiesReload();
|
||||
});
|
||||
}
|
||||
|
||||
ReloadResult(showClashUI);
|
||||
@@ -633,7 +726,10 @@ public class MainWindowViewModel : MyReactiveObject
|
||||
{
|
||||
await ConfigHandler.ApplyRegionalPreset(_config, type);
|
||||
await ConfigHandler.InitRouting(_config);
|
||||
AppEvents.RoutingsMenuRefreshRequested.Publish();
|
||||
RxSchedulers.MainThreadScheduler.Schedule(async () =>
|
||||
{
|
||||
await StatusBarViewModel.RefreshRoutingsMenu();
|
||||
});
|
||||
|
||||
await ConfigHandler.SaveConfig(_config);
|
||||
await new UpdateService(_config, UpdateTaskHandler).UpdateGeoFileAll();
|
||||
|
||||
Reference in New Issue
Block a user