Disconnect some more UI -> Model

This commit is contained in:
Matt Nadareski
2021-07-03 15:00:51 -07:00
parent c2b063301a
commit 83fc88ff6a
4 changed files with 55 additions and 62 deletions

View File

@@ -90,7 +90,7 @@ namespace RedBookPlayer.GUI
Closing += (e, f) => Closing += (e, f) =>
{ {
PlayerView.Player.Stop(); ((PlayerView)ContentControl.Content).StopButton_Click(this, null);
}; };
AddHandler(DragDrop.DropEvent, MainWindow_Drop); AddHandler(DragDrop.DropEvent, MainWindow_Drop);

View File

@@ -71,7 +71,8 @@ namespace RedBookPlayer.GUI
public async Task<bool> LoadImage(string path) public async Task<bool> LoadImage(string path)
{ {
// If the player is currently running, stop it // If the player is currently running, stop it
if(Player.Playing) Player.Stop(); if((DataContext as PlayerViewModel).Playing != true)
(DataContext as PlayerViewModel).Playing = null;
bool result = await Task.Run(() => bool result = await Task.Run(() =>
{ {
@@ -233,13 +234,13 @@ namespace RedBookPlayer.GUI
await LoadImage(path); await LoadImage(path);
} }
public void PlayButton_Click(object sender, RoutedEventArgs e) => Player.TogglePlayPause(true); public void PlayButton_Click(object sender, RoutedEventArgs e) => (DataContext as PlayerViewModel).Playing = true;
public void PauseButton_Click(object sender, RoutedEventArgs e) => Player.TogglePlayPause(false); public void PauseButton_Click(object sender, RoutedEventArgs e) => (DataContext as PlayerViewModel).Playing = false;
public void PlayPauseButton_Click(object sender, RoutedEventArgs e) => Player.TogglePlayPause(!Player.Playing); public void PlayPauseButton_Click(object sender, RoutedEventArgs e) => (DataContext as PlayerViewModel).Playing = !(DataContext as PlayerViewModel).Playing;
public void StopButton_Click(object sender, RoutedEventArgs e) => Player.Stop(); public void StopButton_Click(object sender, RoutedEventArgs e) => (DataContext as PlayerViewModel).Playing = null;
public void NextTrackButton_Click(object sender, RoutedEventArgs e) => Player.NextTrack(); public void NextTrackButton_Click(object sender, RoutedEventArgs e) => Player.NextTrack();
@@ -253,29 +254,29 @@ namespace RedBookPlayer.GUI
public void RewindButton_Click(object sender, RoutedEventArgs e) => Player.Rewind(); public void RewindButton_Click(object sender, RoutedEventArgs e) => Player.Rewind();
public void VolumeUpButton_Click(object sender, RoutedEventArgs e) => App.Settings.Volume++; public void VolumeUpButton_Click(object sender, RoutedEventArgs e) => (DataContext as PlayerViewModel).Volume++;
public void VolumeDownButton_Click(object sender, RoutedEventArgs e) => App.Settings.Volume--; public void VolumeDownButton_Click(object sender, RoutedEventArgs e) => (DataContext as PlayerViewModel).Volume--;
public void MuteToggleButton_Click(object sender, RoutedEventArgs e) public void MuteToggleButton_Click(object sender, RoutedEventArgs e)
{ {
if (_lastVolume == null) if (_lastVolume == null)
{ {
_lastVolume = App.Settings.Volume; _lastVolume = (DataContext as PlayerViewModel).Volume;
App.Settings.Volume = 0; (DataContext as PlayerViewModel).Volume = 0;
} }
else else
{ {
App.Settings.Volume = _lastVolume.Value; (DataContext as PlayerViewModel).Volume = _lastVolume.Value;
_lastVolume = null; _lastVolume = null;
} }
} }
public void EnableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => Player.ToggleDeEmphasis(true); public void EnableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => (DataContext as PlayerViewModel).ApplyDeEmphasis = true;
public void DisableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => Player.ToggleDeEmphasis(false); public void DisableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => (DataContext as PlayerViewModel).ApplyDeEmphasis = false;
public void EnableDisableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => Player.ToggleDeEmphasis(!Player.ApplyDeEmphasis); public void EnableDisableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => (DataContext as PlayerViewModel).ApplyDeEmphasis = !(DataContext as PlayerViewModel).ApplyDeEmphasis;
#endregion #endregion
} }

View File

@@ -6,13 +6,31 @@ namespace RedBookPlayer.GUI
{ {
#region Player Status #region Player Status
private bool _playing; private bool? _playing;
public bool Playing public bool? Playing
{ {
get => _playing; get => _playing;
set => this.RaiseAndSetIfChanged(ref _playing, value); set => this.RaiseAndSetIfChanged(ref _playing, value);
} }
private int _volume;
public int Volume
{
get => _volume;
set => this.RaiseAndSetIfChanged(ref _volume, value);
}
private bool _applyDeEmphasis;
public bool ApplyDeEmphasis
{
get => _applyDeEmphasis;
set => this.RaiseAndSetIfChanged(ref _applyDeEmphasis, value);
}
#endregion
#region Model-Provided Playback Information
private ulong _currentSector; private ulong _currentSector;
public ulong CurrentSector public ulong CurrentSector
{ {
@@ -35,20 +53,6 @@ namespace RedBookPlayer.GUI
public int TotalSeconds => (int)(_totalSectors / 75 % 60); public int TotalSeconds => (int)(_totalSectors / 75 % 60);
public int TotalMinutes => (int)(_totalSectors % 75); public int TotalMinutes => (int)(_totalSectors % 75);
private int _volume;
public int Volume
{
get => _volume;
set => this.RaiseAndSetIfChanged(ref _volume, value);
}
private bool _applyDeEmphasis;
public bool ApplyDeEmphasis
{
get => _applyDeEmphasis;
set => this.RaiseAndSetIfChanged(ref _applyDeEmphasis, value);
}
#endregion #endregion
#region Disc Flags #region Disc Flags

View File

@@ -90,36 +90,29 @@ namespace RedBookPlayer.Hardware
#region Playback #region Playback
/// <summary> /// <summary>
/// Toggle audio playback /// Set the current audio playback state
/// </summary> /// </summary>
/// <param name="start">True to start playback, false to pause</param> /// <param name="start">True to start playback, false to pause, null to stop</param>
public void TogglePlayPause(bool start) private void SetPlayingState(bool? start)
{ {
if(_opticalDisc == null || !_opticalDisc.Initialized) if(_opticalDisc == null || !_opticalDisc.Initialized)
return; return;
if(start) if(start == true)
{ {
_soundOutput.Play(); _soundOutput.Play();
_opticalDisc.SetTotalIndexes(); _opticalDisc.SetTotalIndexes();
} }
else if(start == false)
{
_soundOutput.Stop();
}
else else
{ {
_soundOutput.Stop(); _soundOutput.Stop();
}
}
/// <summary>
/// Stop the current audio playback
/// </summary>
public void Stop()
{
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
_soundOutput.Stop();
_opticalDisc.LoadFirstTrack(); _opticalDisc.LoadFirstTrack();
} }
}
/// <summary> /// <summary>
/// Move to the next playable track /// Move to the next playable track
@@ -130,13 +123,13 @@ namespace RedBookPlayer.Hardware
return; return;
bool wasPlaying = Playing; bool wasPlaying = Playing;
if(wasPlaying) TogglePlayPause(false); if(wasPlaying) SetPlayingState(false);
_opticalDisc.NextTrack(); _opticalDisc.NextTrack();
if(_opticalDisc is CompactDisc compactDisc) if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.ApplyDeEmphasis = compactDisc.TrackHasEmphasis; _soundOutput.ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
if(wasPlaying) TogglePlayPause(true); if(wasPlaying) SetPlayingState(true);
} }
/// <summary> /// <summary>
@@ -148,13 +141,13 @@ namespace RedBookPlayer.Hardware
return; return;
bool wasPlaying = Playing; bool wasPlaying = Playing;
if(wasPlaying) TogglePlayPause(false); if(wasPlaying) SetPlayingState(false);
_opticalDisc.PreviousTrack(); _opticalDisc.PreviousTrack();
if(_opticalDisc is CompactDisc compactDisc) if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.ApplyDeEmphasis = compactDisc.TrackHasEmphasis; _soundOutput.ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
if(wasPlaying) TogglePlayPause(true); if(wasPlaying) SetPlayingState(true);
} }
/// <summary> /// <summary>
@@ -167,13 +160,13 @@ namespace RedBookPlayer.Hardware
return; return;
bool wasPlaying = Playing; bool wasPlaying = Playing;
if(wasPlaying) TogglePlayPause(false); if(wasPlaying) SetPlayingState(false);
_opticalDisc.NextIndex(changeTrack); _opticalDisc.NextIndex(changeTrack);
if(_opticalDisc is CompactDisc compactDisc) if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.ApplyDeEmphasis = compactDisc.TrackHasEmphasis; _soundOutput.ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
if(wasPlaying) TogglePlayPause(true); if(wasPlaying) SetPlayingState(true);
} }
/// <summary> /// <summary>
@@ -186,13 +179,13 @@ namespace RedBookPlayer.Hardware
return; return;
bool wasPlaying = Playing; bool wasPlaying = Playing;
if(wasPlaying) TogglePlayPause(false); if(wasPlaying) SetPlayingState(false);
_opticalDisc.PreviousIndex(changeTrack); _opticalDisc.PreviousIndex(changeTrack);
if(_opticalDisc is CompactDisc compactDisc) if(_opticalDisc is CompactDisc compactDisc)
_soundOutput.ApplyDeEmphasis = compactDisc.TrackHasEmphasis; _soundOutput.ApplyDeEmphasis = compactDisc.TrackHasEmphasis;
if(wasPlaying) TogglePlayPause(true); if(wasPlaying) SetPlayingState(true);
} }
/// <summary> /// <summary>
@@ -255,12 +248,6 @@ namespace RedBookPlayer.Hardware
return string.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2))); return string.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2)));
} }
/// <summary>
/// Toggle de-emphasis processing
/// </summary>
/// <param name="enable">True to apply de-emphasis, false otherwise</param>
public void ToggleDeEmphasis(bool enable) => _soundOutput?.ToggleDeEmphasis(enable);
/// <summary> /// <summary>
/// Update the data context for the frontend /// Update the data context for the frontend
/// </summary> /// </summary>
@@ -303,8 +290,9 @@ namespace RedBookPlayer.Hardware
if(!Initialized || dataContext == null) if(!Initialized || dataContext == null)
return; return;
SetPlayingState(dataContext.Playing);
App.Settings.Volume = dataContext.Volume; App.Settings.Volume = dataContext.Volume;
ToggleDeEmphasis(dataContext.ApplyDeEmphasis); _soundOutput?.ToggleDeEmphasis(dataContext.ApplyDeEmphasis);
} }
/// <summary> /// <summary>