diff --git a/src/ZonyLrcTools.Desktop/ViewModels/AlbumDownloadViewModel.cs b/src/ZonyLrcTools.Desktop/ViewModels/AlbumDownloadViewModel.cs index 6e2b6a7..7ff6e88 100644 --- a/src/ZonyLrcTools.Desktop/ViewModels/AlbumDownloadViewModel.cs +++ b/src/ZonyLrcTools.Desktop/ViewModels/AlbumDownloadViewModel.cs @@ -1,7 +1,14 @@ using System.Collections.ObjectModel; +using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using Microsoft.Extensions.Options; +using ZonyLrcTools.Common; using ZonyLrcTools.Common.Album; +using ZonyLrcTools.Common.Configuration; +using ZonyLrcTools.Common.Infrastructure.IO; +using ZonyLrcTools.Common.Infrastructure.Threading; +using ZonyLrcTools.Common.TagInfo; using ZonyLrcTools.Desktop.Infrastructure.Localization; using ZonyLrcTools.Desktop.Services; @@ -11,9 +18,14 @@ public partial class AlbumDownloadViewModel : ViewModelBase { private readonly IAlbumDownloader _albumDownloader; private readonly IDialogService _dialogService; + private readonly IFileScanner _fileScanner; + private readonly ITagLoader _tagLoader; + private readonly GlobalOptions _options; private readonly IUILocalizationService? _localization; private CancellationTokenSource? _downloadCts; + private readonly Dictionary _musicFileMap = new(); + private List _scannedMusicInfos = new(); [ObservableProperty] private string? _selectedFolderPath; @@ -34,9 +46,26 @@ public partial class AlbumDownloadViewModel : ViewModelBase private double _progressPercentage; [ObservableProperty] + [NotifyCanExecuteChangedFor(nameof(StartDownloadCommand))] + [NotifyCanExecuteChangedFor(nameof(SelectFolderCommand))] [NotifyPropertyChangedFor(nameof(CanStartDownload))] private bool _isDownloading; + [ObservableProperty] + [NotifyCanExecuteChangedFor(nameof(StartDownloadCommand))] + [NotifyCanExecuteChangedFor(nameof(SelectFolderCommand))] + [NotifyPropertyChangedFor(nameof(CanStartDownload))] + private bool _isScanning; + + [ObservableProperty] + private int _scanProgressCount; + + [ObservableProperty] + private int _scanTotalCount; + + [ObservableProperty] + private double _scanProgressPercentage; + // Localized strings public string AlbumTitle => _localization?["Album_Title"] ?? "Album Cover Download"; public string AlbumDescription => _localization?["Album_Description"] ?? "Download album artwork for your music collection"; @@ -46,6 +75,7 @@ public partial class AlbumDownloadViewModel : ViewModelBase public string AlbumParallel => _localization?["Album_Parallel"] ?? "Parallel:"; public string AlbumStartDownload => _localization?["Album_StartDownload"] ?? "Start Download"; public string AlbumStopDownload => _localization?["Album_StopDownload"] ?? "Stop Download"; + public string AlbumScanning => _localization?["Lyrics_Status_Scanning"] ?? "Scanning files..."; public string CommonTotal => _localization?["Common_Total"] ?? "Total:"; public string CommonFiles => _localization?["Common_Files"] ?? "files"; public string CommonSuccess => _localization?["Common_Success"] ?? "Success:"; @@ -54,21 +84,27 @@ public partial class AlbumDownloadViewModel : ViewModelBase public string ColumnArtist => _localization?["Column_Artist"] ?? "Artist"; public string ColumnStatus => _localization?["Column_Status"] ?? "Status"; - public bool CanStartDownload => !IsDownloading && !string.IsNullOrEmpty(SelectedFolderPath); + public bool CanStartDownload => !IsDownloading && !IsScanning && MusicFiles.Count > 0; public bool HasNoFiles => MusicFiles.Count == 0; public string EmptyStateText => _localization?["Album_EmptyState"] ?? "No tasks yet, please select a folder to start scanning"; - public ObservableCollection MusicFiles { get; } = new(); + public ObservableCollection MusicFiles { get; } = new(); public AlbumDownloadViewModel( IAlbumDownloader albumDownloader, IDialogService dialogService, + IFileScanner fileScanner, + ITagLoader tagLoader, + IOptions options, IUILocalizationService? localization = null) { _albumDownloader = albumDownloader; _dialogService = dialogService; + _fileScanner = fileScanner; + _tagLoader = tagLoader; + _options = options.Value; _localization = localization; if (_localization != null) @@ -79,6 +115,8 @@ public partial class AlbumDownloadViewModel : ViewModelBase MusicFiles.CollectionChanged += (_, _) => { OnPropertyChanged(nameof(HasNoFiles)); + OnPropertyChanged(nameof(CanStartDownload)); + StartDownloadCommand.NotifyCanExecuteChanged(); }; } @@ -92,6 +130,7 @@ public partial class AlbumDownloadViewModel : ViewModelBase OnPropertyChanged(nameof(AlbumParallel)); OnPropertyChanged(nameof(AlbumStartDownload)); OnPropertyChanged(nameof(AlbumStopDownload)); + OnPropertyChanged(nameof(AlbumScanning)); OnPropertyChanged(nameof(CommonTotal)); OnPropertyChanged(nameof(CommonFiles)); OnPropertyChanged(nameof(CommonSuccess)); @@ -102,36 +141,151 @@ public partial class AlbumDownloadViewModel : ViewModelBase OnPropertyChanged(nameof(EmptyStateText)); } - [RelayCommand] + [RelayCommand(CanExecute = nameof(CanSelectFolder))] private async Task SelectFolderAsync() { - var folder = await _dialogService.ShowFolderPickerAsync("Select Music Folder"); + var folder = await _dialogService.ShowFolderPickerAsync( + _localization?["Album_SelectFolder"] ?? "Select Music Folder"); if (string.IsNullOrEmpty(folder)) return; SelectedFolderPath = folder; - OnPropertyChanged(nameof(CanStartDownload)); + await ScanFilesAsync(); } - [RelayCommand] - private async Task StartDownloadAsync() + private bool CanSelectFolder => !IsScanning && !IsDownloading; + + /// + /// Tag-read the folder the same way the lyrics page does: identical row set, so the user sees + /// the same songs whichever feature they open first. + /// + private async Task ScanFilesAsync() { if (string.IsNullOrEmpty(SelectedFolderPath)) return; + IsScanning = true; + MusicFiles.Clear(); + _musicFileMap.Clear(); + _scannedMusicInfos.Clear(); + ScanProgressCount = 0; + ScanTotalCount = 0; + ScanProgressPercentage = 0; + TotalCount = 0; + CompletedCount = 0; + FailedCount = 0; + ProgressPercentage = 0; + + try + { + var files = (await _fileScanner.ScanMusicFilesAsync(SelectedFolderPath, _options.SupportFileExtensions)) + .ToList(); + + if (files.Count == 0) + { + await _dialogService.ShowMessageAsync( + _localization?["Common_Info"] ?? "Info", + _localization?["Error_NoMusicFiles"] ?? "No music files found in the selected folder."); + return; + } + + ScanTotalCount = files.Count; + + using var warpTask = new WarpTask(ParallelCount); + var scanTasks = files.Select(file => + warpTask.RunAsync(async () => + { + var musicInfo = await _tagLoader.LoadTagAsync(file); + + await Dispatcher.UIThread.InvokeAsync(() => + { + if (musicInfo != null && + (!string.IsNullOrEmpty(musicInfo.Name) || !string.IsNullOrEmpty(musicInfo.Artist))) + { + var vm = new AlbumFileViewModel + { + FilePath = musicInfo.FilePath, + Name = musicInfo.Name, + Artist = musicInfo.Artist, + StatusMessage = _localization?["Status_Pending"] ?? "Pending" + }; + MusicFiles.Add(vm); + _musicFileMap[musicInfo.FilePath] = vm; + _scannedMusicInfos.Add(musicInfo); + } + + ScanProgressCount++; + ScanProgressPercentage = ScanProgressCount * 100.0 / ScanTotalCount; + }); + })); + + await Task.WhenAll(scanTasks); + + TotalCount = MusicFiles.Count; + } + catch (Exception ex) + { + await _dialogService.ShowMessageAsync( + _localization?["Common_Error"] ?? "Error", + ex.Message); + } + finally + { + IsScanning = false; + OnPropertyChanged(nameof(CanStartDownload)); + } + } + + [RelayCommand(CanExecute = nameof(CanStartDownload))] + private async Task StartDownloadAsync() + { + if (_scannedMusicInfos.Count == 0) return; + IsDownloading = true; CompletedCount = 0; FailedCount = 0; ProgressPercentage = 0; + TotalCount = _scannedMusicInfos.Count; + + foreach (var vm in MusicFiles) + { + vm.IsProcessed = false; + vm.IsSuccessful = false; + vm.StatusMessage = _localization?["Status_Pending"] ?? "Pending"; + } _downloadCts = new CancellationTokenSource(); try { - // TODO: Implement actual download logic using _albumDownloader - await Task.Delay(1000, _downloadCts.Token); // Placeholder + await _albumDownloader.DownloadAsync( + _scannedMusicInfos, + ParallelCount, + async musicInfo => + { + await Dispatcher.UIThread.InvokeAsync(() => + { + if (_musicFileMap.TryGetValue(musicInfo.FilePath, out var vm)) + { + vm.IsProcessed = true; + vm.IsSuccessful = musicInfo.IsSuccessful; + vm.StatusMessage = musicInfo.IsSuccessful + ? _localization?["Status_Success"] ?? "Success" + : _localization?["Status_Failed"] ?? "Failed"; + } + + if (musicInfo.IsSuccessful) + CompletedCount++; + else + FailedCount++; + + var done = CompletedCount + FailedCount; + ProgressPercentage = done * 100.0 / TotalCount; + }); + }, + _downloadCts.Token); } catch (OperationCanceledException) { - // User cancelled + // User cancelled. } finally { @@ -147,3 +301,24 @@ public partial class AlbumDownloadViewModel : ViewModelBase _downloadCts?.Cancel(); } } + +public partial class AlbumFileViewModel : ObservableObject +{ + [ObservableProperty] + private string _filePath = string.Empty; + + [ObservableProperty] + private string _name = string.Empty; + + [ObservableProperty] + private string _artist = string.Empty; + + [ObservableProperty] + private bool _isProcessed; + + [ObservableProperty] + private bool _isSuccessful; + + [ObservableProperty] + private string? _statusMessage; +} diff --git a/src/ZonyLrcTools.Desktop/ViewModels/HomeViewModel.cs b/src/ZonyLrcTools.Desktop/ViewModels/HomeViewModel.cs index 197da45..23026da 100644 --- a/src/ZonyLrcTools.Desktop/ViewModels/HomeViewModel.cs +++ b/src/ZonyLrcTools.Desktop/ViewModels/HomeViewModel.cs @@ -1,15 +1,89 @@ +using System.Reflection; using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using ZonyLrcTools.Desktop.Infrastructure.Localization; +using ZonyLrcTools.Desktop.Services; namespace ZonyLrcTools.Desktop.ViewModels; +/// +/// The landing page. Its only job is to point people at the three things this app actually does, +/// so every quick-action button navigates for real instead of sitting there looking clickable. +/// public partial class HomeViewModel : ViewModelBase { - [ObservableProperty] - private string _welcomeMessage = "Welcome to ZonyLrcTools X"; + private readonly INavigationService _navigation; + private readonly IUILocalizationService _localization; + + public HomeViewModel(INavigationService navigation, IUILocalizationService localization) + { + _navigation = navigation; + _localization = localization; + _localization.LanguageChanged += OnLanguageChanged; + _version = ResolveVersion(); + } [ObservableProperty] - private string _description = "A cross-platform tool for downloading lyrics and album covers for your music collection."; + private string _version; - [ObservableProperty] - private string _version = "4.0.3"; + public string WelcomeMessage => _localization["Home_Welcome"]; + public string Description => _localization["Home_Description"]; + public string QuickActionsTitle => _localization["Home_QuickActions"]; + public string FeaturesTitle => _localization["Home_Features"]; + public string FeatureFormats => _localization["Home_FeatureFormats"]; + public string FeatureParallel => _localization["Home_FeatureParallel"]; + public string FeatureEncodings => _localization["Home_FeatureEncodings"]; + public string FeatureCrossPlatform => _localization["Home_FeatureCrossPlatform"]; + public string VersionLabel => _localization["Home_Version"]; + + // Card copy: title + one-line explanation, both localized. + public string LyricsCardTitle => _localization["Home_DownloadLyrics"]; + public string LyricsCardDescription => _localization["Home_DownloadLyricsHint"]; + public string AlbumCardTitle => _localization["Home_DownloadAlbumCover"]; + public string AlbumCardDescription => _localization["Home_DownloadAlbumCoverHint"]; + public string DecryptCardTitle => _localization["Home_Decrypt"]; + public string DecryptCardDescription => _localization["Home_DecryptHint"]; + + [RelayCommand] + private void OpenLyrics() => _navigation.NavigateRoot(); + + [RelayCommand] + private void OpenAlbum() => _navigation.NavigateRoot(); + + [RelayCommand] + private void OpenDecrypt() => _navigation.NavigateRoot(); + + private void OnLanguageChanged(object? sender, EventArgs e) + { + OnPropertyChanged(nameof(WelcomeMessage)); + OnPropertyChanged(nameof(Description)); + OnPropertyChanged(nameof(QuickActionsTitle)); + OnPropertyChanged(nameof(FeaturesTitle)); + OnPropertyChanged(nameof(FeatureFormats)); + OnPropertyChanged(nameof(FeatureParallel)); + OnPropertyChanged(nameof(FeatureEncodings)); + OnPropertyChanged(nameof(FeatureCrossPlatform)); + OnPropertyChanged(nameof(VersionLabel)); + OnPropertyChanged(nameof(LyricsCardTitle)); + OnPropertyChanged(nameof(LyricsCardDescription)); + OnPropertyChanged(nameof(AlbumCardTitle)); + OnPropertyChanged(nameof(AlbumCardDescription)); + OnPropertyChanged(nameof(DecryptCardTitle)); + OnPropertyChanged(nameof(DecryptCardDescription)); + } + + private static string ResolveVersion() + { + var assembly = typeof(HomeViewModel).Assembly; + var informational = assembly.GetCustomAttribute()?.InformationalVersion; + + if (!string.IsNullOrWhiteSpace(informational)) + { + // Strip the "+" suffix that SourceLink appends. + var plusIndex = informational.IndexOf('+'); + return plusIndex > 0 ? informational[..plusIndex] : informational; + } + + return assembly.GetName().Version?.ToString(3) ?? "4.0.3"; + } } diff --git a/src/ZonyLrcTools.Desktop/Views/Pages/AlbumDownloadPage.axaml b/src/ZonyLrcTools.Desktop/Views/Pages/AlbumDownloadPage.axaml index 0cbb072..9f1571b 100644 --- a/src/ZonyLrcTools.Desktop/Views/Pages/AlbumDownloadPage.axaml +++ b/src/ZonyLrcTools.Desktop/Views/Pages/AlbumDownloadPage.axaml @@ -40,7 +40,7 @@ + + @@ -61,36 +100,35 @@ BoxShadow="0 2 8 0 #0D000000" Padding="24"> - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - + +