Skip to content

Commit

Permalink
Use OnPropertyChanged for logic instead of setters
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed May 13, 2021
1 parent a0334f7 commit f2c0f17
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>true</UseWPF>
<StartupObject>GenshinLyreMidiPlayer.WPF.App</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>1.9.5</Version>
<Version>1.10.1</Version>
<ApplicationIcon>item_windsong_lyre.ico</ApplicationIcon>
<Nullable>enable</Nullable>
<RepositoryUrl>https://github.com/sabihoshi/GenshinLyreMidiPlayer</RepositoryUrl>
Expand Down
82 changes: 41 additions & 41 deletions GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -186,6 +185,7 @@ public void UpdateButtons()

NotifyOfPropertyChange(() => PlayPauseIcon);
NotifyOfPropertyChange(() => MaximumTime);
NotifyOfPropertyChange(() => CurrentTime);

if (Controls is not null && Display is not null)
{
Expand All @@ -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();
Expand Down Expand Up @@ -239,7 +239,7 @@ public void CloseFile()
}

MidiTracks.Clear();
MoveSlider(0);
MoveSlider(TimeSpan.Zero);

Playback = null;
Playlist.OpenedFile = null;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand All @@ -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();
}
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion GenshinLyreMidiPlayer.WPF/Views/LyrePlayerView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
IsMoveToPointEnabled="True"
IsEnabled="{Binding CanHitPlayPause}"

Value="{Binding SongSlider}"
Value="{Binding SongPosition}"
Maximum="{Binding Path=MaximumTime.TotalSeconds}" />
<TextBlock Grid.Column="2" VerticalAlignment="Center"
Text="{Binding MaximumTime, StringFormat=m\\:ss}" />
Expand Down

0 comments on commit f2c0f17

Please sign in to comment.