using System; using System.Threading; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using SpotXInstaller.Models; using SpotXInstaller.Services; namespace SpotXInstaller.ViewModels; public partial class MainViewModel : ViewModelBase { private readonly InstallerService _installer = new(); private CancellationTokenSource? _cts; // --- СОСТОЯНИЯ ЭКРАНОВ --- [ObservableProperty] private bool _isStepLoading = true; [ObservableProperty] private bool _isStepHome; [ObservableProperty] private bool _isStepSettings; [ObservableProperty] private bool _isStepProgress; // --- POPUP --- [ObservableProperty] private bool _showPopup; [ObservableProperty] private string _popupTitle = ""; [ObservableProperty] private string _popupMessage = ""; [ObservableProperty] private string _popupColor = "#8B5CF6"; // --- ГАЛОЧКИ --- [ObservableProperty] private bool _blockUpdates = true; [ObservableProperty] private bool _hidePodcasts = true; [ObservableProperty] private bool _hidePremium = true; [ObservableProperty] private bool _clearCache; // --- ПРОГРЕСС --- [ObservableProperty] private double _progressValue; [ObservableProperty] private string _progressStatus = "Подготовка..."; [ObservableProperty] private string _miniLog = ""; public MainViewModel() => _ = BootAsync(); private async Task BootAsync() { await Task.Delay(3000); SwitchTo(home: true); } private void SwitchTo(bool loading = false, bool home = false, bool settings = false, bool progress = false) { IsStepLoading = loading; IsStepHome = home; IsStepSettings = settings; IsStepProgress = progress; } [RelayCommand] private void GoToSettings() => SwitchTo(settings: true); [RelayCommand] private void GoBackToHome() => SwitchTo(home: true); [RelayCommand] private void ClosePopup() => ShowPopup = false; [RelayCommand] private async Task StartInstallAsync() { await RunInstallerAsync(isUninstall: false); } [RelayCommand] private async Task StartUninstallAsync() { await RunInstallerAsync(isUninstall: true); } private async Task RunInstallerAsync(bool isUninstall) { ProgressValue = 0; MiniLog = ""; ProgressStatus = isUninstall ? "Подготовка восстановления..." : "Подготовка установки..."; SwitchTo(progress: true); _cts?.Dispose(); _cts = new CancellationTokenSource(); var progress = new Progress<(int Percent, string Status)>(p => { ProgressValue = p.Percent; ProgressStatus = p.Status; }); var log = new Progress(l => MiniLog = l); try { int code = isUninstall ? await _installer.UninstallAsync(progress, log, _cts.Token) : await _installer.InstallAsync( new InstallOptions(BlockUpdates, HidePodcasts, HidePremium, ClearCache), progress, log, _cts.Token); SwitchTo(home: true); if (code == 0) ShowNotification( isUninstall ? "ГОТОВО" : "УСПЕХ", isUninstall ? "Оригинальный клиент Spotify восстановлен." : "SpotX успешно установлен.", isUninstall ? "#8B5CF6" : "#00E676"); else ShowNotification("ОШИБКА", $"Скрипт завершился с кодом {code}.\n\n{MiniLog[..Math.Min(200, MiniLog.Length)]}", "#FF3B30"); } catch (OperationCanceledException) { SwitchTo(home: true); ShowNotification("ОТМЕНЕНО", "Операция прервана.", "#FFB300"); } catch (Exception ex) { SwitchTo(home: true); ShowNotification("ОШИБКА", ex.Message, "#FF3B30"); } } private void ShowNotification(string title, string message, string hexColor) { PopupTitle = title; PopupMessage = message; PopupColor = hexColor; ShowPopup = true; } }