refactor: Adjusted the view model to support reactive programming.

This commit is contained in:
real-zony 2024-06-30 17:51:53 +08:00
parent aa3c45101b
commit 81bf6ebe3f
3 changed files with 63 additions and 9 deletions

View File

@ -10,7 +10,7 @@
<DockPanel>
<StackPanel DockPanel.Dock="Bottom" Margin="10">
<TextBlock Text="下载进度:" Margin="0,0,0,5" />
<ProgressBar Value="{Binding DownloadProgress}" Maximum="100" Height="20" />
<ProgressBar Value="{Binding DownloadProgress}" Maximum="{Binding MaxProgress}" Height="20" />
</StackPanel>
<DataGrid AutoGenerateColumns="False"

View File

@ -1,4 +1,6 @@
using Avalonia;
using Avalonia.Controls;
using ZonyLrcTools.Desktop.ViewModels;
namespace ZonyLrcTools.Desktop.Pages;
@ -8,4 +10,15 @@ public partial class HomePage : UserControl
{
InitializeComponent();
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
if (DataContext is HomeViewModel vm)
{
vm.MaxProgress = 100;
vm.DownloadProgress = 0;
}
}
}

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.ObjectModel;
using ReactiveUI;
using ZonyLrcTools.Common;
@ -6,26 +7,66 @@ namespace ZonyLrcTools.Desktop.ViewModels;
public class HomeViewModel : ViewModelBase
{
public ObservableCollection<SongInfoViewModel> Songs { get; } = [];
private ObservableCollection<SongInfoViewModel> _songs = new();
private double _downloadProgress;
public ObservableCollection<SongInfoViewModel> Songs
{
get => _songs;
set => this.RaiseAndSetIfChanged(ref _songs, value);
}
public double DownloadProgress
private int _downloadProgress;
public int DownloadProgress
{
get => _downloadProgress;
set => this.RaiseAndSetIfChanged(ref _downloadProgress, value);
}
private int _maxProgress;
public int MaxProgress
{
get => _maxProgress;
set => this.RaiseAndSetIfChanged(ref _maxProgress, value);
}
}
public class SongInfoViewModel(MusicInfo info)
public class SongInfoViewModel : ReactiveObject
{
private MusicInfo Info { get; } = info;
public SongInfoViewModel(MusicInfo info)
{
_info = info;
DownloadStatus = DownloadStatus.NotStarted;
this.WhenAnyValue(x => x.Info)
.Subscribe(_ =>
{
this.RaisePropertyChanged(nameof(SongName));
this.RaisePropertyChanged(nameof(ArtistName));
this.RaisePropertyChanged(nameof(FilePath));
});
}
private MusicInfo _info;
public MusicInfo Info
{
get => _info;
set => this.RaiseAndSetIfChanged(ref _info, value);
}
public string SongName => Info.Name;
public string ArtistName => Info.Artist;
public string FilePath => Info.FilePath;
public DownloadStatus DownloadStatus { get; set; } = DownloadStatus.NotStarted;
private DownloadStatus _downloadStatus;
public DownloadStatus DownloadStatus
{
get => _downloadStatus;
set => this.RaiseAndSetIfChanged(ref _downloadStatus, value);
}
}
public enum DownloadStatus