// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // The Countdown Alert View Model // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrakeWPF.ViewModels { using System; using System.Windows.Threading; using HandBrakeWPF.Properties; using HandBrakeWPF.ViewModels.Interfaces; /// /// The Countdown Alert View Model /// public class CountdownAlertViewModel : ViewModelBase, ICountdownAlertViewModel { #region Private Fields /// /// The countdown time. /// private const int CountdownTime = 60; /// /// The action. /// private string action; /// /// The timer. /// private DispatcherTimer timer; #endregion #region Constructors and Destructors /// /// Initializes a new instance of the class. /// public CountdownAlertViewModel() { this.IsCancelled = false; Caliburn.Micro.Execute.OnUIThread( () => { timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; timer.Tick += this.timer_Tick; timer.Start(); }); } #endregion #region Public Properties /// /// Gets or sets a value indicating whether is cancelled. /// public bool IsCancelled { get; set; } /// /// Gets the notice message. /// public string NoticeMessage { get { return string.Format(Resources.CountdownAlertViewModel_NoticeMessage, action, CountdownTime - this.Ticks); } } /// /// Gets or sets the ticks. /// public int Ticks { get; set; } #endregion #region Public Methods and Operators /// /// The cancel. /// public void Cancel() { this.IsCancelled = true; timer.Stop(); this.Ticks = 0; this.TryClose(); } /// /// The proceed. /// public void Proceed() { this.IsCancelled = false; timer.Stop(); this.Ticks = 0; this.TryClose(); } /// /// The set action. /// /// /// The action. /// public void SetAction(string actionMsg) { this.IsCancelled = false; this.Ticks = 0; timer.Start(); this.action = actionMsg; } #endregion #region Methods /// /// The timer_ tick. /// /// /// The sender. /// /// /// The e. /// private void timer_Tick(object sender, EventArgs e) { this.Ticks = this.Ticks + 1; this.NotifyOfPropertyChange(() => this.NoticeMessage); if (this.Ticks > CountdownTime) { timer.Stop(); this.Ticks = 0; this.TryClose(); } } #endregion } }