MPD moved to its own class

This commit is contained in:
2021-08-16 02:13:47 +02:00
parent a2f2af87cc
commit f85eae1354
6 changed files with 190 additions and 138 deletions

View File

@ -2,31 +2,19 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using MPDCtrl.Models;
using System.Windows.Interop;
using System.Diagnostics;
namespace unison
{
public partial class MainWindow : Window
{
private readonly MPC _mpd = new();
private bool _connected;
public int _currentVolume;
private bool _currentRandom;
private bool _currentRepeat;
private bool _currentSingle;
private bool _currentConsume;
private double _currentElapsed;
private readonly Settings SettingsWindow = new Settings();
private string _mpdHost = "192.168.1.13";
private int _mpdPort = 6600;
private string _mpdPassword = null;
Settings SettingsWindow = new Settings();
private readonly MPDHandler mpd;
Thickness SelectedThickness;
Thickness BaseThickness;
@ -38,7 +26,9 @@ namespace unison
WindowState = WindowState.Minimized;
ConnectToMPD();
mpd = (MPDHandler)Application.Current.Properties["mpd"];
mpd.Connect();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(0.2);
timer.Tick += Timer_Tick;
@ -49,57 +39,14 @@ namespace unison
BaseThickness.Left = BaseThickness.Right = BaseThickness.Top = BaseThickness.Bottom = 0.0f;
}
public async void ConnectToMPD()
{
_connected = await _mpd.MpdCommandConnectionStart(_mpdHost, _mpdPort, _mpdPassword);
if (_connected)
{
await _mpd.MpdQueryStatus();
await Task.Delay(5);
_currentVolume = _mpd.MpdStatus.MpdVolume;
_currentRandom = _mpd.MpdStatus.MpdRandom;
_currentRepeat = _mpd.MpdStatus.MpdRepeat;
_currentSingle = _mpd.MpdStatus.MpdSingle;
_currentConsume = _mpd.MpdStatus.MpdConsume;
_currentElapsed = _mpd.MpdStatus.MpdSongElapsed;
}
}
private void Timer_Tick(object sender, EventArgs e)
{
LoopMPD();
MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"];
mpd.Loop();
UpdateInterface();
}
public void CheckStatus<T>(ref T a, T b)
{
if (Comparer<T>.Default.Compare(a, b) != 0)
a = b;
}
public async void LoopMPD()
{
if (!_connected)
return;
var status = await _mpd.MpdQueryStatus();
//Trace.WriteLine(status.ResultText);
await Task.Delay(5);
if (status != null)
{
CheckStatus(ref _currentVolume, _mpd.MpdStatus.MpdVolume);
CheckStatus(ref _currentRandom, _mpd.MpdStatus.MpdRandom);
CheckStatus(ref _currentRepeat, _mpd.MpdStatus.MpdRepeat);
CheckStatus(ref _currentSingle, _mpd.MpdStatus.MpdSingle);
CheckStatus(ref _currentConsume, _mpd.MpdStatus.MpdConsume);
CheckStatus(ref _currentElapsed, _mpd.MpdStatus.MpdSongElapsed);
}
await _mpd.MpdQueryCurrentSong();
await Task.Delay(5);
}
public void UpdateButton(ref Border border, bool b)
{
if (b)
@ -116,31 +63,33 @@ namespace unison
public void UpdateInterface()
{
if (_mpd.MpdCurrentSong != null)
if (mpd.GetCurrentSong() != null)
{
SongTitle.Text = _mpd.MpdCurrentSong.Title;
SongTitle.ToolTip = _mpd.MpdCurrentSong.File;
SongArtist.Text = _mpd.MpdCurrentSong.Artist;
SongAlbum.Text = _mpd.MpdCurrentSong.Album + " (" + _mpd.MpdCurrentSong.Date + ")";
Bitrate.Text = _mpd.MpdCurrentSong.File.Substring(_mpd.MpdCurrentSong.File.LastIndexOf(".") + 1);
Bitrate.Text += " ";
Bitrate.Text += _mpd.MpdStatus.MpdBitrate + "kbps";
SongTitle.Text = mpd.GetCurrentSong().Title;
SongTitle.ToolTip = mpd.GetCurrentSong().File;
SongArtist.Text = mpd.GetCurrentSong().Artist;
SongAlbum.Text = mpd.GetCurrentSong().Album;
if (mpd.GetCurrentSong().Date.Length > 0)
SongAlbum.Text += $" ({ mpd.GetCurrentSong().Date})";
Bitrate.Text = mpd.GetCurrentSong().File.Substring(mpd.GetCurrentSong().File.LastIndexOf(".") + 1) + " ";
Bitrate.Text += mpd.GetStatus().MpdBitrate + "kbps";
CurrentTime.Text = FormatSeconds(_currentElapsed);
EndTime.Text = FormatSeconds(_mpd.MpdStatus.MpdSongTime);
CurrentTime.Text = FormatSeconds(mpd._currentElapsed);
EndTime.Text = FormatSeconds(mpd.GetStatus().MpdSongTime);
TimeSlider.Value = _currentElapsed / _mpd.MpdCurrentSong.TimeSort * 100;
if (!System.Double.IsNaN(mpd.GetCurrentSong().TimeSort))
TimeSlider.Value = mpd._currentElapsed / mpd.GetCurrentSong().TimeSort * 100;
}
if (VolumeSlider.Value != _currentVolume)
if (VolumeSlider.Value != mpd._currentVolume)
{
VolumeSlider.Value = _currentVolume;
VolumeSlider.ToolTip = _currentVolume;
VolumeSlider.Value = mpd._currentVolume;
VolumeSlider.ToolTip = mpd._currentVolume;
}
if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Play)
if (mpd.IsPlaying())
PauseButtonEmoji.Text = "⏸️";
else if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Pause)
else
PauseButtonEmoji.Text = "▶️";
SnapcastHandler snapcast = (SnapcastHandler)Application.Current.Properties["snapcast"];
@ -149,56 +98,22 @@ namespace unison
else
SnapcastText.Text = "Start Snapcast";
Connection.Text = (_connected ? "✔️" : "❌") + _mpd.MpdHost + ":" + _mpd.MpdPort;
Connection.Text = (mpd._connected ? "✔️" : "❌") + $"{Properties.Settings.Default.mpd_host}:{Properties.Settings.Default.mpd_port}";
UpdateButton(ref BorderRandom, _currentRandom);
UpdateButton(ref BorderRepeat, _currentRepeat);
UpdateButton(ref BorderSingle, _currentSingle);
UpdateButton(ref BorderConsume, _currentConsume);
UpdateButton(ref BorderRandom, mpd._currentRandom);
UpdateButton(ref BorderRepeat, mpd._currentRepeat);
UpdateButton(ref BorderSingle, mpd._currentSingle);
UpdateButton(ref BorderConsume, mpd._currentConsume);
}
public async void Pause_Clicked(object sender, RoutedEventArgs e)
{
if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Play)
await _mpd.MpdPlaybackPause();
else if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Pause)
await _mpd.MpdPlaybackPlay(_currentVolume);
}
public async void Previous_Clicked(object sender, RoutedEventArgs e)
{
await _mpd.MpdPlaybackPrev(_currentVolume);
}
public async void Next_Clicked(object sender, RoutedEventArgs e)
{
await _mpd.MpdPlaybackNext(_currentVolume);
}
public async void Random_Clicked(object sender, RoutedEventArgs e)
{
await _mpd.MpdSetRandom(!_currentRandom);
}
private async void Repeat_Clicked(object sender, RoutedEventArgs e)
{
await _mpd.MpdSetRepeat(!_currentRepeat);
}
private async void Single_Clicked(object sender, RoutedEventArgs e)
{
await _mpd.MpdSetSingle(!_currentSingle);
}
private async void Consume_Clicked(object sender, RoutedEventArgs e)
{
await _mpd.MpdSetConsume(!_currentConsume);
}
public async void ChangeVolume(int value)
{
await _mpd.MpdSetVolume(value);
}
public void Pause_Clicked(object sender, RoutedEventArgs e) => mpd.PlayPause();
public void Previous_Clicked(object sender, RoutedEventArgs e) => mpd.Prev();
public void Next_Clicked(object sender, RoutedEventArgs e) => mpd.Next();
public void Random_Clicked(object sender, RoutedEventArgs e) => mpd.Random();
public void Repeat_Clicked(object sender, RoutedEventArgs e) => mpd.Repeat();
public void Single_Clicked(object sender, RoutedEventArgs e) => mpd.Single();
public void Consume_Clicked(object sender, RoutedEventArgs e) => mpd.Consume();
public void ChangeVolume(int value) => mpd.SetVolume(value);
public void Snapcast_Clicked(object sender, RoutedEventArgs e)
{

View File

@ -34,7 +34,7 @@
<TextBlock Text="Password" TextWrapping="Wrap" Margin="5,0,0,0"/>
<TextBox x:Name="MpdPassword" TextWrapping="Wrap" Width="250" Margin="10,2,0,0"/>
</StackPanel>
<Button Content="Connect" Margin="0,10,0,0" Width="120"/>
<Button Content="Connect" Margin="0,10,0,0" Width="120" Click="MPDConnect_Clicked"/>
</StackPanel>
</Grid>
</GroupBox>

View File

@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
@ -65,6 +66,17 @@ namespace unison
e.Handled = true;
}
private void MPDConnect_Clicked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.mpd_host = MpdHost.Text;
Properties.Settings.Default.mpd_port = int.Parse(MpdPort.Text, CultureInfo.InvariantCulture);
Properties.Settings.Default.mpd_password = MpdPassword.Text;
Properties.Settings.Default.Save();
var mpd = (MPDHandler)Application.Current.Properties["mpd"];
mpd.Connect();
}
private void SnapcastReset_Clicked(object sender, RoutedEventArgs e)
{
SnapcastPath.Text = defaultSnapcastPath;
@ -76,11 +88,11 @@ namespace unison
e.Cancel = true;
Properties.Settings.Default.mpd_host = MpdHost.Text;
Properties.Settings.Default.mpd_port = int.Parse(MpdPort.Text);
Properties.Settings.Default.mpd_port = int.Parse(MpdPort.Text, CultureInfo.InvariantCulture);
Properties.Settings.Default.mpd_password = MpdPassword.Text;
Properties.Settings.Default.snapcast_startup = (bool)SnapcastStartup.IsChecked;
Properties.Settings.Default.snapcast_path = SnapcastPath.Text;
Properties.Settings.Default.snapcast_port = int.Parse(SnapcastPort.Text);
Properties.Settings.Default.snapcast_port = int.Parse(SnapcastPort.Text, CultureInfo.InvariantCulture);
Properties.Settings.Default.Save();
WindowState = WindowState.Minimized;