From 81bf6ebe3f4d5ebb1b841817eaa6d011df72309d Mon Sep 17 00:00:00 2001 From: real-zony Date: Sun, 30 Jun 2024 17:51:53 +0800 Subject: [PATCH] refactor: Adjusted the view model to support reactive programming. --- src/ZonyLrcTools.Desktop/Pages/HomePage.axaml | 6 +-- .../Pages/HomePage.axaml.cs | 13 +++++ .../ViewModels/HomeViewModel.cs | 53 ++++++++++++++++--- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/src/ZonyLrcTools.Desktop/Pages/HomePage.axaml b/src/ZonyLrcTools.Desktop/Pages/HomePage.axaml index b84af62..7a5f156 100644 --- a/src/ZonyLrcTools.Desktop/Pages/HomePage.axaml +++ b/src/ZonyLrcTools.Desktop/Pages/HomePage.axaml @@ -6,13 +6,13 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="ZonyLrcTools.Desktop.Pages.HomePage" x:DataType="viewModels:HomeViewModel"> - + - + - + Songs { get; } = []; + private ObservableCollection _songs = new(); - private double _downloadProgress; + public ObservableCollection 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