using System; using System.ComponentModel; using TINK.Settings; namespace TINK.ViewModel.Settings { /// Polling relagted parameters public class PollingViewModel : INotifyPropertyChanged { /// Holds the views polling parameters. private PollingParameters m_oPolling = PollingParameters.Default; /// Current polling periode. Used to check whether values were modified or not. private readonly PollingParameters m_oPollingActive; /// Constructs polling object. /// Object to construct from public PollingViewModel(PollingParameters p_oSource) { m_oPollingActive = p_oSource ?? throw new ArgumentException("Can not instantiate polling parameters view model- object. Polling parameter object is null."); m_oPolling = p_oSource; } /// Gets the immutable version of polling parameters. /// Polling parameters object. public PollingParameters ToImmutable() { return m_oPolling; } /// Holds value whether polling is activated or not. public bool IsActivated { get { return m_oPolling.IsActivated; } set { if (value == m_oPolling.IsActivated) { // Nothing to do. return; } m_oPolling = new PollingParameters(m_oPolling.Periode, value); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsActivated))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PeriodeTotalSeconds))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PeriodeTotalSecondsText))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PollingText))); } } /// Gests or sets the polling periode [sec]. public int PeriodeTotalSeconds { get { return (int)m_oPolling.Periode.TotalSeconds; } set { if (value == (int)m_oPolling.Periode.TotalSeconds) { // Nothing to do. return; } m_oPolling = new PollingParameters(new TimeSpan(0, 0, value), IsActivated); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PeriodeTotalSeconds))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PeriodeTotalSecondsText))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PollingText))); } } /// Gets info about polling periode. public string PeriodeTotalSecondsText { get { if (IsActivated) { return $"Polling Periode: {PeriodeTotalSeconds} [Sek.]"; } return $"Polling abgeschalten."; } } /// Gets the text of the polling related controls. public string PollingText { get { if (m_oPolling == m_oPollingActive) { return "Polling"; } return "Polling\r\nAnsicht verlassen um Änderungen anzuwenden."; } } /// Notifies GUI about modified values. public event PropertyChangedEventHandler PropertyChanged; } }