LD-KickWatch/src/KickStreamHub/Converters/StringToVisibilityConverter.cs

257 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Concurrent;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WpfColor = System.Windows.Media.Color;
using WpfColors = System.Windows.Media.Colors;
using WpfColorConverter = System.Windows.Media.ColorConverter;
namespace KickStreamHub.Converters;
public class BoolToVisibilityInverseConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? Visibility.Collapsed : Visibility.Visible;
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class LiveTextConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? "● CANLI YAYINDA" : "ÇEVRİMDIŞI";
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class ChatConnectionTextConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? "Bağlı" : "Bağlanıyor...";
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class LiveToBrushConverter : IValueConverter
{
private static readonly SolidColorBrush LiveBrush = new(WpfColor.FromRgb(0, 231, 1));
private static readonly SolidColorBrush OfflineBrush = new(WpfColor.FromRgb(51, 65, 85));
static LiveToBrushConverter() { LiveBrush.Freeze(); OfflineBrush.Freeze(); }
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
var isLive = value is true;
if (targetType == typeof(WpfColor))
return isLive ? WpfColors.LimeGreen : WpfColors.SlateGray;
return isLive ? LiveBrush : OfflineBrush;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class PauseTextConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? "▶ Devam" : "⏸ Duraklat";
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class ViewerFormatConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is long v)
return v >= 1000 ? $"{v / 1000.0:0.0}B" : v.ToString();
return "0";
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class PageVisibilityConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string current && parameter is string target)
return current.Equals(target, StringComparison.OrdinalIgnoreCase)
? Visibility.Visible : Visibility.Collapsed;
return Visibility.Collapsed;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class ActivePageMatchConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string current && parameter is string target)
return current.Equals(target, StringComparison.OrdinalIgnoreCase);
return false;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class NonEmptyToVisibilityConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
bool hasText = value is string s && !string.IsNullOrWhiteSpace(s);
bool invert = parameter is string p && p.Equals("invert", StringComparison.OrdinalIgnoreCase);
if (invert) hasText = !hasText;
return hasText ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class NullToVisibilityConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
bool isNotNull = value is not null;
bool invert = parameter is string p && p.Equals("invert", StringComparison.OrdinalIgnoreCase);
if (invert) isNotNull = !isNotNull;
return isNotNull ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class PlayButtonTextConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? "⏹ YAYINI DURDUR" : "▶ YAYINI İZLE";
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class TheaterButtonTextConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? "🗗 Normal" : "🗖 Sinema Modu";
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class FullScreenButtonTextConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? "🗗 Çık (Esc)" : "⛶ Tam Ekran";
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class MuteButtonTextConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? "🔇 Sesi Aç" : "🔊 Sesi Kapat";
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class MultiLayoutTextConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? "▯▯ 2'li Yan Yana Mod" : "⊞ 4'lü (2x2) Izgara Mod";
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class SoloAudioTextConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is true ? "🔇 Bu Ekranı Dinle" : "🔊 Ses Açık";
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class InitialsConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string s && !string.IsNullOrWhiteSpace(s))
{
var clean = s.Trim().TrimStart('@');
if (clean.Length == 0) return "?";
return clean.Length >= 2 ? clean[..2].ToUpperInvariant() : clean[..1].ToUpperInvariant();
}
return "?";
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
/// <summary>
/// Diskteki avatar dosyasını kilitlemeden (BitmapCacheOption.OnLoad) belleğe yükler.
/// Böylece AvatarCacheService dosyayı güncellerken IOException (file lock) oluşmaz.
/// </summary>
public class AvatarImageConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not string path || string.IsNullOrWhiteSpace(path) || !File.Exists(path))
return null;
try
{
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bitmap.DecodePixelWidth = 140;
bitmap.StreamSource = stream;
bitmap.EndInit();
bitmap.Freeze();
return bitmap;
}
catch
{
return null;
}
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}
public class StringToBrushConverter : IValueConverter
{
private static readonly ConcurrentDictionary<string, SolidColorBrush> BrushCache = new(StringComparer.OrdinalIgnoreCase);
private static readonly SolidColorBrush DefaultBrush = CreateFrozenBrush(WpfColor.FromRgb(0, 231, 1));
private static SolidColorBrush CreateFrozenBrush(WpfColor color)
{
var brush = new SolidColorBrush(color);
brush.Freeze();
return brush;
}
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string hex && !string.IsNullOrWhiteSpace(hex))
{
return BrushCache.GetOrAdd(hex.Trim(), key =>
{
try
{
var color = (WpfColor)WpfColorConverter.ConvertFromString(key);
return CreateFrozenBrush(color);
}
catch
{
return DefaultBrush;
}
});
}
return DefaultBrush;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
throw new NotSupportedException();
}