using System; using System.Diagnostics; using System.Reflection; using AdvancedSharpAdbClient.Models; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Threading; using MetaforceInstaller.Core.Intefaces; using MetaforceInstaller.Core.Models; using MetaforceInstaller.Core.Services; using MetaforceInstaller.UI.ViewModels; namespace MetaforceInstaller.UI.Windows; public partial class MainWindow : Window { private MainWindowViewModel _viewModel; private IStorageService _storageService; private AdbService _adbService; public MainWindow() { InitializeComponent(); _viewModel = new MainWindowViewModel(); _storageService = new StorageService(); _adbService = new AdbService(); DataContext = _viewModel; VersionLabel.Content = Assembly.GetExecutingAssembly() .GetCustomAttribute()?.Version; NewInstallationButton.Click += OnNewInstalltionClick; _adbService.DeviceConnected += OnAdbDeviceConnected; _adbService.DeviceDisconnected += OnAdbDeviceDisconnected; _adbService.DeviceChanged += OnAdbDeviceChanged; LoadInstallations(); UpdateDeviceStatus(); } private void UpdateDeviceStatus() { var isConnected = _adbService.IsDeviceConnected; _viewModel.IsDeviceConnected = isConnected; if (isConnected) { try { var deviceInfo = _adbService.GetDeviceInfo(); _viewModel.DeviceSerial = deviceInfo.SerialNumber; } catch { _viewModel.DeviceSerial = "Unknown"; } } else { _viewModel.DeviceSerial = string.Empty; } } private void OnAdbDeviceConnected(object? sender, DeviceDataEventArgs e) { Dispatcher.UIThread.Post(() => { UpdateDeviceStatus(); }); } private void OnAdbDeviceDisconnected(object? sender, DeviceDataEventArgs e) { Dispatcher.UIThread.Post(() => { UpdateDeviceStatus(); }); } private void OnAdbDeviceChanged(object? sender, DeviceDataEventArgs e) { Dispatcher.UIThread.Post(() => { UpdateDeviceStatus(); }); } private void LoadInstallations() { var appData = _storageService.Load(); _viewModel.LoadInstallations(appData.Installations); } public async void OnNewInstalltionClick(object? sender, RoutedEventArgs e) { var newInstallationDialog = new NewInstallationDialog(_storageService); await newInstallationDialog.ShowDialog(this); LoadInstallations(); } public async void OnDeleteInstallationClick(object? sender, RoutedEventArgs e) { if (sender is Button button && button.DataContext is InstallationData installationData) { var name = installationData.Title; Console.WriteLine($"Delete {name}"); } } public async void OnLaunchServerClick(object? sender, RoutedEventArgs e) { if (sender is Button button && button.DataContext is InstallationData installationData) { var exePath = installationData.Parts.WindowsServerPath; var processInfo = new ProcessStartInfo { FileName = exePath, UseShellExecute = false, }; Process.Start(processInfo); } } public async void OnLaunchAdminClick(object? sender, RoutedEventArgs e) { if (sender is Button button && button.DataContext is InstallationData installationData) { var exePath = installationData.Parts.WindowsAdminPath; var processInfo = new ProcessStartInfo { FileName = exePath, UseShellExecute = false, }; Process.Start(processInfo); } } protected override void OnClosed(EventArgs e) { _adbService.DeviceConnected -= OnAdbDeviceConnected; _adbService.DeviceDisconnected -= OnAdbDeviceDisconnected; _adbService.DeviceChanged -= OnAdbDeviceChanged; _adbService.Dispose(); base.OnClosed(e); } }