unison/Views/Shuffle.xaml.cs

366 lines
12 KiB
C#
Raw Normal View History

using MpcNET.Commands.Database;
2021-10-06 20:54:55 +00:00
using MpcNET.Tags;
using MpcNET.Types;
using MpcNET.Types.Filters;
2021-10-06 20:54:55 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;
2021-10-05 12:23:23 +00:00
using System.Windows;
using System.Windows.Controls;
2021-10-05 12:23:23 +00:00
using System.Windows.Interop;
using System.Windows.Media;
using System.Linq;
using System.Windows.Input;
using System.Text.RegularExpressions;
2021-10-05 12:23:23 +00:00
namespace unison
{
public partial class Shuffle : Window
{
2021-10-06 20:54:55 +00:00
private MPDHandler _mpd;
private ShuffleHandler _shuffle;
2021-10-06 20:54:55 +00:00
bool _continuous = false;
List<string> _genreList { get; }
List<string> _folderList { get; }
List<IFilter> _filters { get; }
2021-10-05 12:23:23 +00:00
public Shuffle()
{
InitializeComponent();
_genreList = new();
_folderList = new();
_filters = new();
SongFilterNumber.Text = "0";
2021-10-06 20:54:55 +00:00
_mpd = (MPDHandler)Application.Current.Properties["mpd"];
_shuffle = (ShuffleHandler)Application.Current.Properties["shuffle"];
}
public void Initialize()
2021-10-06 20:54:55 +00:00
{
ListGenre();
ListFolder();
2021-10-06 20:54:55 +00:00
}
public async void ListGenre()
2021-10-06 20:54:55 +00:00
{
if (_genreList.Count != 0)
2021-10-07 20:14:09 +00:00
return;
2021-10-06 20:54:55 +00:00
List<string> Response = await _mpd.SafelySendCommandAsync(new ListCommand(MpdTags.Genre, null, null));
2021-10-06 20:54:55 +00:00
if (Response == null)
return;
2021-10-06 20:54:55 +00:00
foreach (string genre in Response)
_genreList.Add(genre);
2021-10-06 20:54:55 +00:00
}
public async void ListFolder()
2021-10-06 20:54:55 +00:00
{
if (_folderList.Count != 0)
return;
2021-10-07 20:14:09 +00:00
IEnumerable<IMpdFilePath> Response = await _mpd.SafelySendCommandAsync(new LsInfoCommand(""));
if (Response == null)
return;
foreach (IMpdFilePath folder in Response)
_folderList.Add(folder.Name);
2021-10-06 20:54:55 +00:00
}
private bool IsFilterEmpty()
2021-10-06 20:54:55 +00:00
{
if (_filters.Count() == 0)
return true;
return false;
2021-10-05 12:23:23 +00:00
}
private void Window_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
WindowState = WindowState.Minimized;
Hide();
}
public void InitHwnd()
{
WindowInteropHelper helper = new(this);
helper.EnsureHandle();
}
private bool IsOnMainThread()
2021-10-06 20:54:55 +00:00
{
return App.Current.Dispatcher.Thread == System.Threading.Thread.CurrentThread;
2021-10-06 20:54:55 +00:00
}
private T FindParent<T>(DependencyObject child) where T : DependencyObject
2021-10-06 20:54:55 +00:00
{
var parent = VisualTreeHelper.GetParent(child);
if (parent == null)
return null;
if (parent is T)
return parent as T;
2021-10-06 20:54:55 +00:00
else
return FindParent<T>(parent);
}
private void AddFilter_Clicked(object sender, RoutedEventArgs e)
{
FilterPanel.Children.Add(new ContentPresenter { ContentTemplate = (DataTemplate)FindResource("FilterPanel") });
SongFilterNumber.Text = "0";
2021-10-06 20:54:55 +00:00
}
private void RemoveFilter_Clicked(object sender, RoutedEventArgs e)
2021-10-07 20:14:09 +00:00
{
if (FilterPanel.Children.Count > 1)
FilterPanel.Children.Remove(FindParent<ContentPresenter>(sender as Button));
else
Reset_Clicked(null, null);
SongFilterNumber.Text = "0";
}
2021-10-07 20:14:09 +00:00
private void Reset_Clicked(object sender, RoutedEventArgs e)
{
FilterPanel.Children.RemoveRange(0, FilterPanel.Children.Count);
FilterPanel.Children.Add(new ContentPresenter { ContentTemplate = (DataTemplate)FindResource("FilterPanel") });
SongFilterNumber.Text = "0";
_shuffle._songList.Clear();
}
2021-10-07 20:14:09 +00:00
private ITag FilterEquivalence_Type(string value)
{
if (value == "Song")
return MpdTags.Title;
else if (value == "Artist")
return MpdTags.Artist;
else if (value == "Album")
return MpdTags.Album;
else if (value == "Year")
return MpdTags.Date;
else if (value == "Genre")
return MpdTags.Genre;
return MpdTags.Title;
}
2021-10-07 20:14:09 +00:00
private FilterOperator FilterEquivalence_Operator(string value)
{
if (value == "contains")
return FilterOperator.Contains;
else if (value == "is")
return FilterOperator.Equal;
else if (value == "is not")
return FilterOperator.Different;
return FilterOperator.Equal;
2021-10-07 20:14:09 +00:00
}
2021-10-06 20:54:55 +00:00
private async void UpdateFilter_Clicked(object sender, RoutedEventArgs e)
2021-10-06 20:54:55 +00:00
{
await UpdateFilter();
}
private async Task UpdateFilter()
{
Debug.WriteLine("update filter => start");
_filters.Clear();
Debug.WriteLine("is on main thread => " + IsOnMainThread());
2021-10-06 20:54:55 +00:00
foreach (ContentPresenter superChild in FilterPanel.Children)
2021-10-06 20:54:55 +00:00
{
ITag tag = MpdTags.Title;
FilterOperator op = FilterOperator.None;
string value = "";
bool isDir = false;
2021-10-06 20:54:55 +00:00
StackPanel stackPanel = VisualTreeHelper.GetChild(superChild, 0) as StackPanel;
foreach (TextBox child in stackPanel.Children.OfType<TextBox>())
{
if (child.Name == "FilterValue")
value = child.Text;
}
foreach (ComboBox child in stackPanel.Children.OfType<ComboBox>())
{
if (child.Name == "FilterType")
{
if (child.SelectedItem.ToString() == "Directory")
isDir = true;
else
tag = FilterEquivalence_Type(child.SelectedItem.ToString());
}
if (child.Name == "FilterOperator")
op = FilterEquivalence_Operator(child.SelectedItem.ToString());
if (child.Name == "FilterList" && child.Visibility == Visibility.Visible)
value = child.SelectedItem.ToString();
}
2021-10-06 20:54:55 +00:00
if (value != "")
2021-10-06 20:54:55 +00:00
{
if (!isDir)
_filters.Add(new FilterTag(tag, value, op));
else
_filters.Add(new FilterBase(value, FilterOperator.None));
2021-10-06 20:54:55 +00:00
await _shuffle.GetSongsFromFilter(_filters);
SongFilterPanel.Visibility = Visibility.Visible;
SongFilterNumber.Text = _shuffle._songList.Count.ToString();
2021-10-06 20:54:55 +00:00
}
}
Debug.WriteLine("update filter => stop");
}
private void FilterType_Change(object sender, string Operator, List<string> Listing)
{
ComboBox comboBox = sender as ComboBox;
StackPanel stackPanel = comboBox.Parent as StackPanel;
foreach (ComboBox child in stackPanel.Children.OfType<ComboBox>())
{
if (child.Name == "FilterOperator")
{
child.ItemsSource = (Array)FindResource(Operator);
child.SelectedItem = child.Items[0];
}
if (child.Name == "FilterList")
{
child.Visibility = Visibility.Visible;
child.ItemsSource = Listing;
child.SelectedItem = child.Items[0];
}
}
foreach (TextBox child in stackPanel.Children.OfType<TextBox>())
{
if (child.Name == "FilterValue")
child.Visibility = Visibility.Collapsed;
}
SongFilterNumber.Text = "0";
}
private void FilterType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string item = e.AddedItems[0].ToString();
if (item == "Genre")
FilterType_Change(sender, "OperatorTypeB", _genreList);
else if (item == "Directory")
FilterType_Change(sender, "OperatorTypeC", _folderList);
else
{
ComboBox combobox = sender as ComboBox;
StackPanel stackpanel = combobox.Parent as StackPanel;
foreach (ComboBox child in stackpanel.Children.OfType<ComboBox>())
{
if (child.Name == "FilterOperator")
{
child.ItemsSource = (Array)FindResource("OperatorTypeA");
child.SelectedItem = child.Items[0];
}
if (child.Name == "FilterList")
child.Visibility = Visibility.Collapsed;
}
foreach (TextBox child in stackpanel.Children.OfType<TextBox>())
{
if (child.Name == "FilterValue")
child.Visibility = Visibility.Visible;
}
}
SongFilterNumber.Text = "0";
}
private void OperatorType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("selection changed => operator type");
SongFilterNumber.Text = "0";
2021-10-06 20:54:55 +00:00
}
private void QueueValidationTextBox(object sender, TextCompositionEventArgs e)
2021-10-06 20:54:55 +00:00
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
private void QueueValidationNumber()
{
if (int.Parse(SongNumber.Text) < 1)
SongNumber.Text = "1";
if (int.Parse(SongNumber.Text) > 1000)
SongNumber.Text = "1000";
}
private async void AddToQueue_Clicked(object sender, RoutedEventArgs e)
{
QueueValidationNumber();
if (_mpd.GetStats() == null)
return;
NumberAddedSongs.Text = "0";
SearchStatus.Visibility = Visibility.Visible;
// start dispatcher
// write _shuffle.AddedSongs in dispatcher
await AddToQueue(int.Parse(SongNumber.Text));
Debug.WriteLine("add to queue finished");
2021-10-06 20:54:55 +00:00
SearchStatus.Visibility = Visibility.Collapsed;
}
private async Task AddToQueue(int NumberToAdd)
{
await UpdateFilter();
2021-10-06 20:54:55 +00:00
Debug.WriteLine("check filters");
if (IsFilterEmpty())
await _shuffle.AddToQueueRandom(NumberToAdd);
2021-10-06 20:54:55 +00:00
else
{
Debug.WriteLine("add to queue filter - before");
await _shuffle.AddToQueueFilter(NumberToAdd);
Debug.WriteLine("add to queue filter - after");
2021-10-06 20:54:55 +00:00
}
Debug.WriteLine("add to queue finished");
}
public bool GetContinuous()
{
return _continuous;
2021-10-06 20:54:55 +00:00
}
public async Task HandleContinuous()
2021-10-05 12:23:23 +00:00
{
if (!_continuous)
2022-04-18 23:17:48 +00:00
{
Debug.WriteLine("continuous return nothing!");
2021-10-06 20:54:55 +00:00
return;
2022-04-18 23:17:48 +00:00
}
2021-10-05 12:23:23 +00:00
2022-04-18 23:17:48 +00:00
Debug.WriteLine("continuous __before__ add to queue");
await AddToQueue(5);
2022-04-18 23:17:48 +00:00
Debug.WriteLine("continuous __after__ add to queue");
}
private async void ContinuousShuffle_Checked(object sender, RoutedEventArgs e)
{
if (ContinuousShuffle.IsChecked == true)
_continuous = true;
2021-10-06 20:54:55 +00:00
else
_continuous = false;
2022-04-18 23:17:48 +00:00
if (_mpd.GetStatus().PlaylistLength < 5)
await HandleContinuous();
2021-10-05 12:23:23 +00:00
}
}
}