1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="CountdownAlertViewModel.cs" company="HandBrake Project (http://handbrake.fr)">
3 //   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
4 // </copyright>
5 // <summary>
6 //   The Countdown Alert View Model
7 // </summary>
8 // --------------------------------------------------------------------------------------------------------------------
9 
10 namespace HandBrakeWPF.ViewModels
11 {
12     using System;
13     using System.Windows.Threading;
14 
15     using HandBrakeWPF.Model.Options;
16     using HandBrakeWPF.Properties;
17     using HandBrakeWPF.Utilities;
18     using HandBrakeWPF.ViewModels.Interfaces;
19 
20     /// <summary>
21     ///  The Countdown Alert View Model
22     /// </summary>
23     public class CountdownAlertViewModel : ViewModelBase, ICountdownAlertViewModel
24     {
25         #region Private Fields
26 
27         /// <summary>
28         /// The countdown time.
29         /// </summary>
30         private const int CountdownTime = 60;
31 
32         /// <summary>
33         /// The action.
34         /// </summary>
35         private string action;
36 
37         /// <summary>
38         /// The timer.
39         /// </summary>
40         private DispatcherTimer timer;
41 
42         #endregion
43 
44         #region Constructors and Destructors
45 
46         /// <summary>
47         /// Initializes a new instance of the <see cref="CountdownAlertViewModel"/> class.
48         /// </summary>
CountdownAlertViewModel()49         public CountdownAlertViewModel()
50         {
51             this.IsCancelled = false;
52 
53             Caliburn.Micro.Execute.OnUIThread(
54                 () =>
55                 {
56                     timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
57                     timer.Tick += this.timer_Tick;
58 
59                     timer.Start();
60                 });
61         }
62 
63         #endregion
64 
65         #region Public Properties
66 
67         /// <summary>
68         /// Gets or sets a value indicating whether is cancelled.
69         /// </summary>
70         public bool IsCancelled { get; set; }
71 
72         /// <summary>
73         /// Gets the notice message.
74         /// </summary>
75         public string NoticeMessage
76         {
77             get
78             {
79                 return string.Format(Resources.CountdownAlertViewModel_NoticeMessage, action, CountdownTime - this.Ticks);
80             }
81         }
82 
83         /// <summary>
84         /// Gets or sets the ticks.
85         /// </summary>
86         public int Ticks { get; set; }
87 
88         #endregion
89 
90         #region Public Methods and Operators
91 
92         /// <summary>
93         /// The cancel.
94         /// </summary>
Cancel()95         public void Cancel()
96         {
97             this.IsCancelled = true;
98             timer.Stop();
99             this.Ticks = 0;
100             this.TryCloseAsync();
101         }
102 
103         /// <summary>
104         /// The proceed.
105         /// </summary>
Proceed()106         public void Proceed()
107         {
108             this.IsCancelled = false;
109             timer.Stop();
110             this.Ticks = 0;
111             this.TryCloseAsync();
112         }
113 
114         /// <summary>
115         /// The set action.
116         /// </summary>
117         /// <param name="actionMsg">
118         /// The action.
119         /// </param>
SetAction(WhenDone actionMsg)120         public void SetAction(WhenDone actionMsg)
121         {
122             this.IsCancelled = false;
123             this.Ticks = 0;
124             timer.Start();
125             this.action = EnumHelper<WhenDone>.GetDisplay(actionMsg);
126         }
127 
128         #endregion
129 
130         #region Methods
131 
132         /// <summary>
133         /// The timer_ tick.
134         /// </summary>
135         /// <param name="sender">
136         /// The sender.
137         /// </param>
138         /// <param name="e">
139         /// The e.
140         /// </param>
timer_Tick(object sender, EventArgs e)141         private void timer_Tick(object sender, EventArgs e)
142         {
143             this.Ticks = this.Ticks + 1;
144             this.NotifyOfPropertyChange(() => this.NoticeMessage);
145             if (this.Ticks > CountdownTime)
146             {
147                 timer.Stop();
148                 this.Ticks = 0;
149                 this.TryCloseAsync();
150             }
151         }
152 
153         #endregion
154     }
155 }