Du hast auf den Branch log-test-branch 2026-04-12 20:57:35 +00:00 gepusht
Neuer Pull-Request
Du hast auf den Branch anon-test2 2026-04-12 20:50:35 +00:00 gepusht
Neuer Pull-Request
Dateien
Soft-LTS/ViewModels/PrinterStatusViewModel.cs
2026-04-12 17:39:45 +02:00

113 Zeilen
4.3 KiB
C#

using System.Windows.Media;
using PrinterMonitor.Models;
namespace PrinterMonitor.ViewModels;
public class PrinterStatusViewModel : ViewModelBase
{
// Statische, eingefrorene Pinsel: einmalig allokiert und thread-safe via Freeze().
// new SolidColorBrush(...) pro Update()-Aufruf entfällt komplett.
private static readonly SolidColorBrush BrushOk = MakeFrozenBrush(144, 238, 144);
private static readonly SolidColorBrush BrushWarning = MakeFrozenBrush(255, 215, 0);
private static readonly SolidColorBrush BrushError = MakeFrozenBrush(255, 160, 160);
private static SolidColorBrush MakeFrozenBrush(byte r, byte g, byte b)
{
var brush = new SolidColorBrush(Color.FromRgb(r, g, b));
brush.Freeze();
return brush;
}
private string _name = "";
private string _type = "";
private bool _isOnline;
private string _ltsText = "-";
private string _klappeText = "-";
private string _etikettenText = "-";
private bool _ltsSensor;
private bool _druckerklappe;
private bool _keineEtiketten;
private SolidColorBrush _rowBrush = BrushOk;
private string _errorMessage = "";
/// <summary>Wird aufgerufen wenn ein Sensor-Wert per Checkbox geändert wird.</summary>
public Action<string, bool, bool, bool>? OnSimulationStateChanged { get; set; }
public string Name { get => _name; set => SetProperty(ref _name, value); }
public string Type { get => _type; set => SetProperty(ref _type, value); }
public bool IsOnline { get => _isOnline; set => SetProperty(ref _isOnline, value); }
public string LtsText { get => _ltsText; set => SetProperty(ref _ltsText, value); }
public string KlappeText { get => _klappeText; set => SetProperty(ref _klappeText, value); }
public string EtikettenText { get => _etikettenText; set => SetProperty(ref _etikettenText, value); }
public SolidColorBrush RowBrush { get => _rowBrush; set => SetProperty(ref _rowBrush, value); }
public string ErrorMessage { get => _errorMessage; set => SetProperty(ref _errorMessage, value); }
public bool IsSimulation => string.Equals(Type, "Simulation", StringComparison.OrdinalIgnoreCase);
public bool IsNotSimulation => !IsSimulation;
public bool LtsSensor
{
get => _ltsSensor;
set
{
if (SetProperty(ref _ltsSensor, value) && IsSimulation)
OnSimulationStateChanged?.Invoke(Name, value, _druckerklappe, _keineEtiketten);
}
}
public bool Druckerklappe
{
get => _druckerklappe;
set
{
if (SetProperty(ref _druckerklappe, value) && IsSimulation)
OnSimulationStateChanged?.Invoke(Name, _ltsSensor, value, _keineEtiketten);
}
}
public bool KeineEtiketten
{
get => _keineEtiketten;
set
{
if (SetProperty(ref _keineEtiketten, value) && IsSimulation)
OnSimulationStateChanged?.Invoke(Name, _ltsSensor, _druckerklappe, value);
}
}
public PrinterStatusViewModel(string name, string type)
{
Name = name;
Type = type;
}
public void Update(PrinterStatus status)
{
IsOnline = status.IsOnline;
ErrorMessage = status.ErrorMessage ?? "";
// Bool-Werte für Checkboxen (ohne Callback-Trigger via interne Felder setzen)
_ltsSensor = status.LtsSensor ?? false;
_druckerklappe = status.Druckerklappe ?? false;
_keineEtiketten = status.KeineEtiketten ?? false;
OnPropertyChanged(nameof(LtsSensor));
OnPropertyChanged(nameof(Druckerklappe));
OnPropertyChanged(nameof(KeineEtiketten));
LtsText = FormatBool(status.LtsSensor, "Belegt", "Frei");
KlappeText = FormatBool(status.Druckerklappe, "Offen", "Geschlossen");
EtikettenText = FormatBool(status.KeineEtiketten, "Leer", "OK");
// Statische Pinsel wiederverwenden – keine Heap-Allokation pro Update
if (!status.IsOnline)
RowBrush = BrushError;
else if (status.Druckerklappe == true || status.KeineEtiketten == true)
RowBrush = BrushWarning;
else
RowBrush = BrushOk;
}
private static string FormatBool(bool? value, string trueText, string falseText)
=> value.HasValue ? (value.Value ? trueText : falseText) : "-";
}