Ocarina2/Ocarina2/Converter/SongMetadataConverter.cs

51 lines
1.9 KiB
C#

using System;
using System.Globalization;
using Avalonia.Data;
using Avalonia.Data.Converters;
using Ocarina2.Models;
namespace Ocarina2.Converter;
public class SongMetadataConverter:IValueConverter
{
/*
OCARINA2 Open-source music player and library manager
Copyright (C) 2023 Matyáš Caras
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if(!int.TryParse(parameter as string,out var p)) return new BindingNotification(new InvalidCastException(), BindingErrorType.Error); // get int param
// for null return placeholders
if (value == null && p == 1) return "Nothing";
if (value == null && p == 2) return "Start playing something...";
if (value is MusicFile m)
{
if (p == 1)
{
return m.Metadata.Tag.Title;
}
return m.Metadata.Tag.FirstPerformer == ""?"Unknown":m.Metadata.Tag.FirstPerformer;
}
return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}