detect/detect.gui/VWMS/MainWindowModel.cs

124 lines
3.0 KiB
C#
Raw Normal View History

2024-11-26 11:24:25 +08:00
using System;
using System.Reactive;
2024-11-27 15:20:42 +08:00
using Avalonia.Controls;
using Avalonia.Threading;
2024-11-26 11:24:25 +08:00
using detect.gui.Api;
using detect.gui.Models;
2024-11-27 15:20:42 +08:00
using detect.gui.VWS;
2024-11-26 11:24:25 +08:00
using ReactiveUI;
using Serilog;
2024-11-27 15:20:42 +08:00
using Splat;
using WebViewControl;
2024-11-26 11:24:25 +08:00
using xwd.utils;
namespace detect.gui.VWMS;
public class MainWindowModel : ViewModelBase<MainWindowModel>
{
/// <summary>
/// 当前登录用户
/// </summary>
private UserModel? _currentUser;
public UserModel? CurrentUser
{
get => _currentUser;
set => this.RaiseAndSetIfChanged(ref _currentUser, value);
}
private bool _isHomeView;
public bool IsHomeView
{
get => _isHomeView;
set => this.RaiseAndSetIfChanged(ref _isHomeView, value);
}
private bool _isDetectTaskView;
public bool IsDetectTaskView
{
get => _isDetectTaskView;
set => this.RaiseAndSetIfChanged(ref _isDetectTaskView, value);
}
private bool _isDeviceView;
public bool IsDeviceView
{
get => _isDeviceView;
set => this.RaiseAndSetIfChanged(ref _isDeviceView, value);
}
private bool _isLogView;
public bool IsLogView
{
get => _isLogView;
set => this.RaiseAndSetIfChanged(ref _isLogView, value);
}
private bool _isUserView;
public bool IsUserView
{
get => _isUserView;
set => this.RaiseAndSetIfChanged(ref _isUserView, value);
}
private readonly string[] _localRoutes =
[
"#/dashboard",
"#/data/task",
"#/data/device",
"#/system/log",
2024-11-27 15:20:42 +08:00
"#/system/user",
""
2024-11-26 11:24:25 +08:00
];
private string? _address;
public string? Address
{
get => _address;
set => this.RaiseAndSetIfChanged(ref _address, value);
}
2024-11-27 15:20:42 +08:00
public ReactiveCommand<string, Unit> GoCommand { get; }
2024-11-26 11:24:25 +08:00
public ReactiveCommand<Unit, Unit> LogoutCommand { get; }
public MainWindowModel()
{
ApiService.Instance().StartService();
2024-11-27 15:20:42 +08:00
GoCommand = ReactiveCommand.Create<string>( p => SetAddress(int.Parse(p)));
2024-11-26 11:24:25 +08:00
LogoutCommand = ReactiveCommand.Create(Logout);
}
public void Dispose()
{
ApiService.Instance().StopService();
}
private void Logout()
{
CurrentUser = null;
2024-11-27 15:20:42 +08:00
SetAddress(5);
Dispatcher.UIThread.InvokeAsync(() =>
{
var webView = Locator.Current.GetService<MainWindow>()!.Get<WebView>("WebView");
webView.IsVisible = false;
});
2024-11-26 11:24:25 +08:00
}
public void SetAddress(int index)
{
2025-01-08 13:49:31 +08:00
if (index < 4)
2024-11-27 15:20:42 +08:00
Locator.Current.GetService<MainWindowModel>()!.IsLoading = true;
2024-11-26 11:24:25 +08:00
var isOnLine = AppSettingsManager.Manager().GetBool("Embedded.IsOnline");
var path = AppSettingsManager.Manager().GetString("Embedded.Path", "./dist/index.html");
var prefix = isOnLine ? path : AppDomain.CurrentDomain.BaseDirectory + path;
Address = $"{prefix}{_localRoutes[index]}";
Log.Information("current address: {address}", Address);
}
}