using System; using System.Reactive; using Avalonia.Controls; using Avalonia.Threading; using detect.gui.Api; using detect.gui.Models; using detect.gui.VWS; using ReactiveUI; using Serilog; using Splat; using WebViewControl; using xwd.utils; namespace detect.gui.VWMS; public class MainWindowModel : ViewModelBase { /// /// 当前登录用户 /// 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", "#/system/user", "" ]; private string? _address; public string? Address { get => _address; set => this.RaiseAndSetIfChanged(ref _address, value); } public ReactiveCommand GoCommand { get; } public ReactiveCommand LogoutCommand { get; } public MainWindowModel() { ApiService.Instance().StartService(); GoCommand = ReactiveCommand.Create( p => SetAddress(int.Parse(p))); LogoutCommand = ReactiveCommand.Create(Logout); } public void Dispose() { ApiService.Instance().StopService(); } private void Logout() { CurrentUser = null; SetAddress(5); Dispatcher.UIThread.InvokeAsync(() => { var webView = Locator.Current.GetService()!.Get("WebView"); webView.IsVisible = false; }); } public void SetAddress(int index) { if (index < 5) Locator.Current.GetService()!.IsLoading = true; 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); } }