summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/ViewModels/CountdownAlertViewModel.cs
blob: a6d3ae2c7372bec3528dc53fa063909a38433615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CountdownAlertViewModel.cs" company="HandBrake Project (http://handbrake.fr)">
//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
//   The Countdown Alert View Model
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrakeWPF.ViewModels
{
    using System;
    using System.Windows.Threading;

    using HandBrakeWPF.Model.Options;
    using HandBrakeWPF.Properties;
    using HandBrakeWPF.Utilities;
    using HandBrakeWPF.ViewModels.Interfaces;

    /// <summary>
    ///  The Countdown Alert View Model
    /// </summary>
    public class CountdownAlertViewModel : ViewModelBase, ICountdownAlertViewModel
    {
        #region Private Fields

        /// <summary>
        /// The countdown time.
        /// </summary>
        private const int CountdownTime = 60;

        /// <summary>
        /// The action.
        /// </summary>
        private string action;

        /// <summary>
        /// The timer.
        /// </summary>
        private DispatcherTimer timer;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CountdownAlertViewModel"/> class.
        /// </summary>
        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

        /// <summary>
        /// Gets or sets a value indicating whether is cancelled.
        /// </summary>
        public bool IsCancelled { get; set; }

        /// <summary>
        /// Gets the notice message.
        /// </summary>
        public string NoticeMessage
        {
            get
            {
                return string.Format(Resources.CountdownAlertViewModel_NoticeMessage, action, CountdownTime - this.Ticks);
            }
        }

        /// <summary>
        /// Gets or sets the ticks.
        /// </summary>
        public int Ticks { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The cancel.
        /// </summary>
        public void Cancel()
        {
            this.IsCancelled = true;
            timer.Stop();
            this.Ticks = 0;
            this.TryClose();
        }

        /// <summary>
        /// The proceed.
        /// </summary>
        public void Proceed()
        {
            this.IsCancelled = false;
            timer.Stop();
            this.Ticks = 0;
            this.TryClose();
        }

        /// <summary>
        /// The set action.
        /// </summary>
        /// <param name="actionMsg">
        /// The action.
        /// </param>
        public void SetAction(WhenDone actionMsg)
        {
            this.IsCancelled = false;
            this.Ticks = 0;
            timer.Start();
            this.action = EnumHelper<WhenDone>.GetDisplay(actionMsg);    
        }

        #endregion

        #region Methods

        /// <summary>
        /// The timer_ tick.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        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
    }
}