From f2c0f17925a81badd84a4d909499f9c9fda68496 Mon Sep 17 00:00:00 2001 From: sabihoshi Date: Thu, 13 May 2021 19:27:25 +0800 Subject: [PATCH] Use OnPropertyChanged for logic instead of setters --- .../GenshinLyreMidiPlayer.WPF.csproj | 2 +- .../ViewModels/LyrePlayerViewModel.cs | 82 +++++++++---------- .../Views/LyrePlayerView.xaml | 2 +- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj b/GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj index f64cea7..bc3edaf 100644 --- a/GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj +++ b/GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj @@ -6,7 +6,7 @@ true GenshinLyreMidiPlayer.WPF.App app.manifest - 1.9.5 + 1.10.1 item_windsong_lyre.ico enable https://github.com/sabihoshi/GenshinLyreMidiPlayer diff --git a/GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs b/GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs index 096849c..8c2931e 100644 --- a/GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs +++ b/GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs @@ -34,8 +34,7 @@ public class LyrePlayerViewModel : Screen, private readonly PlaybackCurrentTimeWatcher _timeWatcher; private bool _ignoreSliderChange; private InputDevice? _inputDevice; - private MidiInput? _selectedMidiInput; - private double _songSlider; + private TimeSpan _songPosition; public LyrePlayerViewModel(IContainer ioc, SettingsPageViewModel settings, PlaylistViewModel playlist) @@ -101,40 +100,15 @@ public bool CanHitPlayPause } } - public bool CanHitPrevious => CurrentTime > TimeSpan.FromSeconds(3) || Playlist.History.Count > 1; + public bool CanHitPrevious => _songPosition > TimeSpan.FromSeconds(3) || Playlist.History.Count > 1; - public double SongSlider + public double SongPosition { - get => _songSlider; - set - { - if (!_ignoreSliderChange && Playback is { IsRunning: true }) - Playback.Stop(); - - SetAndNotify(ref _songSlider, value); - - _ignoreSliderChange = false; - } + get => _songPosition.TotalSeconds; + set => SetAndNotify(ref _songPosition, TimeSpan.FromSeconds(value)); } - public MidiInput? SelectedMidiInput - { - get => _selectedMidiInput; - set - { - SetAndNotify(ref _selectedMidiInput, value); - - _inputDevice?.Dispose(); - - if (_selectedMidiInput?.DeviceName != null && _selectedMidiInput.DeviceName != "None") - { - _inputDevice = InputDevice.GetByName(_selectedMidiInput.DeviceName); - - _inputDevice.EventReceived += OnNoteEvent; - _inputDevice.StartEventsListening(); - } - } - } + public MidiInput? SelectedMidiInput { get; set; } private MusicDisplayProperties? Display => _player?.SystemMediaTransportControls.DisplayUpdater.MusicProperties; @@ -152,7 +126,7 @@ public MidiInput? SelectedMidiInput private SystemMediaTransportControls? Controls => _player?.SystemMediaTransportControls; - public TimeSpan CurrentTime => TimeSpan.FromSeconds(SongSlider); + public TimeSpan CurrentTime => _songPosition; public TimeSpan MaximumTime => Playlist.OpenedFile?.Duration ?? TimeSpan.Zero; @@ -178,6 +152,31 @@ public async void Handle(MidiFile file) public async void Handle(SettingsPageViewModel message) { await InitializePlayback(); } + public void OnSongPositionChanged() + { + if (!_ignoreSliderChange && Playback is { IsRunning: true }) + { + Playback.Stop(); + Playback.MoveToTime(new MetricTimeSpan(_songPosition)); + + if (Settings.UseSpeakers) + Playback.Start(); + } + } + + public void OnSelectedMidiInputChanged() + { + _inputDevice?.Dispose(); + + if (SelectedMidiInput?.DeviceName != null && SelectedMidiInput.DeviceName != "None") + { + _inputDevice = InputDevice.GetByName(SelectedMidiInput.DeviceName); + + _inputDevice.EventReceived += OnNoteEvent; + _inputDevice.StartEventsListening(); + } + } + public void UpdateButtons() { NotifyOfPropertyChange(() => CanHitNext); @@ -186,6 +185,7 @@ public void UpdateButtons() NotifyOfPropertyChange(() => PlayPauseIcon); NotifyOfPropertyChange(() => MaximumTime); + NotifyOfPropertyChange(() => CurrentTime); if (Controls is not null && Display is not null) { @@ -207,7 +207,7 @@ public void UpdateButtons() var position = $"{file.Position}/{Playlist.GetPlaylist().Count}"; Display.Title = file.Title; - Display.Artist = $"Playing {position} {CurrentTime:mm\\:ss}"; + Display.Artist = $"Playing {position} {SongPosition:mm\\:ss}"; } Controls.DisplayUpdater.Update(); @@ -239,7 +239,7 @@ public void CloseFile() } MidiTracks.Clear(); - MoveSlider(0); + MoveSlider(TimeSpan.Zero); Playback = null; Playlist.OpenedFile = null; @@ -330,10 +330,10 @@ private int ApplyNoteSettings(int noteId) public void Previous() { - if (CurrentTime > TimeSpan.FromSeconds(3)) + if (_songPosition > TimeSpan.FromSeconds(3)) { Playback!.MoveToStart(); - MoveSlider(0); + MoveSlider(TimeSpan.Zero); } else Playlist.Previous(); @@ -365,7 +365,7 @@ public async Task PlayPause() { Playback.Loop = Playlist.Loop == Track; - var time = (MetricTimeSpan) TimeSpan.FromSeconds(SongSlider); + var time = new MetricTimeSpan(CurrentTime); Playback.PlaybackStart = time; Playback.MoveToTime(time); @@ -390,7 +390,7 @@ public void OnSongTick(object? sender, PlaybackCurrentTimeChangedEventArgs e) foreach (var playbackTime in e.Times) { TimeSpan time = (MetricTimeSpan) playbackTime.Time; - MoveSlider(time.TotalSeconds); + MoveSlider(time); UpdateButtons(); } @@ -445,10 +445,10 @@ private void PlayNote(NoteEvent noteEvent) } } - private void MoveSlider(double value) + private void MoveSlider(TimeSpan value) { _ignoreSliderChange = true; - SongSlider = value; + SongPosition = value.TotalSeconds; } public void RefreshDevices() diff --git a/GenshinLyreMidiPlayer.WPF/Views/LyrePlayerView.xaml b/GenshinLyreMidiPlayer.WPF/Views/LyrePlayerView.xaml index ed58796..2660a01 100644 --- a/GenshinLyreMidiPlayer.WPF/Views/LyrePlayerView.xaml +++ b/GenshinLyreMidiPlayer.WPF/Views/LyrePlayerView.xaml @@ -79,7 +79,7 @@ IsMoveToPointEnabled="True" IsEnabled="{Binding CanHitPlayPause}" - Value="{Binding SongSlider}" + Value="{Binding SongPosition}" Maximum="{Binding Path=MaximumTime.TotalSeconds}" />