add cool status circle changing depends on adb device connected

This commit is contained in:
Вячеслав 2026-01-02 21:49:44 +05:00
parent bac6bb9ccd
commit 864a07c75c
4 changed files with 169 additions and 5 deletions

View file

@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Media;
using MetaforceInstaller.Core.Models;
namespace MetaforceInstaller.UI.ViewModels;
@ -12,6 +13,42 @@ public class MainWindowViewModel : INotifyPropertyChanged
public ObservableCollection<InstallationData> Installations { get; set; } = new();
private bool _isDeviceConnected;
public bool IsDeviceConnected
{
get => _isDeviceConnected;
set
{
if (_isDeviceConnected != value)
{
_isDeviceConnected = value;
OnPropertyChanged();
OnPropertyChanged(nameof(StatusColor));
OnPropertyChanged(nameof(StatusText));
}
}
}
private string _deviceSerial = string.Empty;
public string DeviceSerial
{
get => _deviceSerial;
set
{
if (_deviceSerial != value)
{
_deviceSerial = value;
OnPropertyChanged();
OnPropertyChanged(nameof(StatusText));
}
}
}
public IBrush StatusColor => IsDeviceConnected ? Brushes.Green : Brushes.Red;
public string StatusText => IsDeviceConnected ? $"Connected to {_deviceSerial}" : "Not connected";
public void LoadInstallations(IEnumerable<InstallationData> data)
{
Installations.Clear();

View file

@ -75,7 +75,10 @@
</ScrollViewer>
<DockPanel Grid.Row="2">
<Label Name="VersionLabel" HorizontalAlignment="Right" />
<Border Name="StatusCircle" Margin="4" Width="16" Height="16" Background="{Binding StatusColor}"
CornerRadius="50" HorizontalAlignment="Left" />
<Label Name="SerialNumberLabel" Content="{Binding StatusText}" />
<Label Name="VersionLabel" HorizontalAlignment="Right" VerticalAlignment="Center" />
</DockPanel>
</Grid>
</Window>

View file

@ -1,8 +1,10 @@
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;
@ -14,6 +16,7 @@ public partial class MainWindow : Window
{
private MainWindowViewModel _viewModel;
private IStorageService _storageService;
private AdbService _adbService;
public MainWindow()
{
@ -21,14 +24,68 @@ public partial class MainWindow : Window
_viewModel = new MainWindowViewModel();
_storageService = new StorageService();
_adbService = new AdbService();
DataContext = _viewModel;
VersionLabel.Content = Assembly.GetExecutingAssembly()
.GetCustomAttribute<AssemblyFileVersionAttribute>()?.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()
@ -52,7 +109,7 @@ public partial class MainWindow : Window
Console.WriteLine($"Delete {name}");
}
}
public async void OnLaunchServerClick(object? sender, RoutedEventArgs e)
{
if (sender is Button button && button.DataContext is InstallationData installationData)
@ -80,4 +137,14 @@ public partial class MainWindow : Window
Process.Start(processInfo);
}
}
protected override void OnClosed(EventArgs e)
{
_adbService.DeviceConnected -= OnAdbDeviceConnected;
_adbService.DeviceDisconnected -= OnAdbDeviceDisconnected;
_adbService.DeviceChanged -= OnAdbDeviceChanged;
_adbService.Dispose();
base.OnClosed(e);
}
}