Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removed an unused function, and added further comments to the code.

  • Loading branch information...
commit eed67764be8c29112c4809bcef2831f66963900e 1 parent 133c502
blapthorn authored February 08, 2012
2  CountdownTimer/CountdownTimer.csproj
@@ -70,7 +70,6 @@
70 70
       <Generator>MSBuild:Compile</Generator>
71 71
       <SubType>Designer</SubType>
72 72
     </ApplicationDefinition>
73  
-    <Compile Include="Models\EnumExtension.cs" />
74 73
     <Compile Include="Models\ISettingsModel.cs" />
75 74
     <Compile Include="Models\ITimerModel.cs" />
76 75
     <Compile Include="Models\SettingsModelFactory.cs" />
@@ -80,6 +79,7 @@
80 79
     <Compile Include="Models\TimerModel.cs" />
81 80
     <Compile Include="Models\TimerModelEventArgs.cs" />
82 81
     <Compile Include="Models\TimerModelFactory.cs" />
  82
+    <Compile Include="Models\TimerState.cs" />
83 83
     <Compile Include="ViewModels\AboutViewModel.cs" />
84 84
     <Compile Include="ViewModels\SettingsViewModel.cs" />
85 85
     <Compile Include="ViewModels\TimerViewModel.cs" />
37  CountdownTimer/MainWindow.xaml.cs
@@ -21,15 +21,16 @@
21 21
 namespace Btl
22 22
 {
23 23
     /// <summary>
24  
-    /// Interaction logic for MainWindow.xaml
  24
+    /// Most of the cruft in this class is to manage the taskbar item preview
  25
+    /// and the window title.
25 26
     /// </summary>
26 27
     public partial class MainWindow : Window
27 28
     {
28  
-
29 29
         public MainWindow()
30 30
         {
31 31
             InitializeComponent();
32 32
 
  33
+            //  Reposition the window to where it last was.
33 34
             LoadWindowPosition();
34 35
 
35 36
             //  Consume any TaskbarItemMessages that are being dispatched around
@@ -37,11 +38,11 @@ public MainWindow()
37 38
             Messenger.Default.Register<TaskbarItemMessage>(this, ConsumeTaskbarItemMessage);
38 39
             Messenger.Default.Register<SimpleMessage>(this, ConsumeSimpleMessage);
39 40
 
40  
-            WindowTitle = this.Title;
  41
+            WindowTitle = Title;
41 42
         }
42 43
 
43 44
         /// <summary>
44  
-        /// Update the TaskbarItemInfo values with whatever is specifed in the
  45
+        /// Update the TaskbarItemInfo values with whatever is specified in the
45 46
         /// message.
46 47
         /// </summary>
47 48
         /// <param name="message"></param>
@@ -50,35 +51,43 @@ void ConsumeTaskbarItemMessage(TaskbarItemMessage message)
50 51
             if (message == null)
51 52
                 return;
52 53
 
53  
-            this.TaskbarItemInfo.ProgressState = message.State;
  54
+            TaskbarItemInfo.ProgressState = message.State;
54 55
 
  56
+            //  if the taskbar item message carried a (percentage) value,
  57
+            //  update the taskbar progressvalue with it.
55 58
             if (message.HasValue)
56  
-                this.TaskbarItemInfo.ProgressValue = message.Value;
  59
+                TaskbarItemInfo.ProgressValue = message.Value;
57 60
         }
58 61
 
  62
+        /// <summary>
  63
+        /// If the user has changed the settings, the only thing that affects
  64
+        /// the MainWindow state is its TopMost value.
  65
+        /// </summary>
59 66
         private void OnSettingsChanged()
60 67
         {
61 68
             var settings = SettingsModelFactory.GetNewSettings();
62  
-            this.Topmost = settings.TopMost;
  69
+            Topmost = settings.TopMost;
63 70
         }
64 71
 
65 72
         /// <summary>
66  
-        /// Only respond to settings that directly affect this main window.
  73
+        /// Only respond to settings that directly affect this main window,
  74
+        /// and do not handle the rest.
67 75
         /// </summary>
68 76
         /// <param name="message"></param>
69 77
         private void ConsumeSimpleMessage(SimpleMessage message)
70 78
         {
71 79
             switch (message.Type)
72 80
             {
  81
+                case SimpleMessage.MessageType.TimerTick:
  82
+                    //  this happens the most so put it first
  83
+                    Title = message.Message;
  84
+                    break;
73 85
                 case SimpleMessage.MessageType.SettingsChanged:
74 86
                     OnSettingsChanged();
75 87
                     break;
76 88
                 case SimpleMessage.MessageType.TimerStop:
77 89
                     Title = WindowTitle;
78 90
                     break;
79  
-                case SimpleMessage.MessageType.TimerTick:
80  
-                    Title = message.Message;
81  
-                    break;
82 91
                 case SimpleMessage.MessageType.TimerReset:
83 92
                     //  restore window title if we reset.
84 93
                     Title = WindowTitle;
@@ -87,12 +96,13 @@ private void ConsumeSimpleMessage(SimpleMessage message)
87 96
         }
88 97
 
89 98
         /// <summary>
90  
-        /// Window closing event.  
  99
+        /// Window closing event.  Save the window position.  
91 100
         /// </summary>
92 101
         /// <param name="sender"></param>
93 102
         /// <param name="e"></param>
94 103
         private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
95 104
         {
  105
+            //  Persist the window position.
96 106
             SaveWindowPosition();
97 107
         }
98 108
 
@@ -129,6 +139,9 @@ private void LoadWindowPosition()
129 139
             this.Topmost = settings.TopMost;
130 140
         }
131 141
 
  142
+        /// <summary>
  143
+        /// Store the window title away as we change it later.
  144
+        /// </summary>
132 145
         private string WindowTitle { get; set; }
133 146
 
134 147
     }
5  CountdownTimer/Models/SimpleMessage.cs
@@ -17,6 +17,11 @@
17 17
 
18 18
 namespace Btl.Models
19 19
 {
  20
+    /// <summary>
  21
+    /// A simple message class that we use to pass messages around the application
  22
+    /// via the Messenger singleton, but keep the parts sufficiently decoupled
  23
+    /// for the MVVM style of work.
  24
+    /// </summary>
20 25
     public class SimpleMessage
21 26
     {
22 27
         public SimpleMessage() : this(MessageType.SwitchToTimerView)
7  CountdownTimer/Models/TimerModel.cs
@@ -19,13 +19,6 @@
19 19
 
20 20
 namespace Btl.Models
21 21
 {
22  
-    public enum TimerState
23  
-    {
24  
-        Running,
25  
-        Paused,
26  
-        Complete
27  
-    }
28  
-
29 22
     internal class TimerModel : ITimerModel
30 23
     {
31 24
         #region Members
1  CountdownTimer/Models/TimerModelFactory.cs
@@ -14,7 +14,6 @@
14 14
 //
15 15
 // You are free to fork this via github:  https://github.com/barrylapthorn/countdown_timer
16 16
 
17  
-
18 17
 namespace Btl.Models
19 18
 {
20 19
     public class TimerModelFactory
111  CountdownTimer/ViewModels/TimerViewModel.cs
@@ -35,7 +35,7 @@ public class TimerViewModel : ViewModelBase
35 35
         private FontFamily _fontFamily = null;
36 36
         private double _fontSize = 0d;
37 37
         private Color _statusColor;
38  
-        readonly ITimerModel _timer = TimerModelFactory.GetTimer();
  38
+        readonly ITimerModel _timer = TimerModelFactory.GetNewTimer();
39 39
         readonly ISettingsModel _settings = SettingsModelFactory.GetNewSettings();
40 40
         #endregion
41 41
 
@@ -72,26 +72,39 @@ public TimerViewModel()
72 72
 
73 73
         #region Settings Command
74 74
 
  75
+        /// <summary>
  76
+        /// Executed when we want to switch to the settings view.
  77
+        /// </summary>
75 78
         private void SettingsExecute()
76 79
         {
  80
+            StopTimerExecute();
77 81
             Messenger.Default.Send(new SimpleMessage { Type = SimpleMessage.MessageType.SwitchToSettingsView });
78 82
         }
79 83
 
  84
+        /// <summary>
  85
+        /// Executed when we want to switch to the about view
  86
+        /// </summary>
80 87
         private void AboutExecute()
81 88
         {
82 89
             Messenger.Default.Send(new SimpleMessage { Type = SimpleMessage.MessageType.SwitchToAboutView });
83 90
         }
84  
-
85  
-
  91
+        
86 92
         #endregion
87 93
 
88 94
         #region Start Command
89 95
 
  96
+        /// <summary>
  97
+        /// Start the underlying timer
  98
+        /// </summary>
90 99
         void StartTimerExecute()
91 100
         {
92 101
             _timer.Start();
93 102
         }
94 103
 
  104
+        /// <summary>
  105
+        /// can we start the underlying timer?
  106
+        /// </summary>
  107
+        /// <returns></returns>
95 108
         bool CanStartTimerExecute()
96 109
         {
97 110
             return !_timer.Complete && _timer.Status != TimerState.Running;
@@ -101,11 +114,18 @@ bool CanStartTimerExecute()
101 114
 
102 115
         #region Stop Command
103 116
 
  117
+        /// <summary>
  118
+        /// Stop the underlying timer.
  119
+        /// </summary>
104 120
         void StopTimerExecute()
105 121
         {
106 122
             _timer.Stop();
107 123
         }
108 124
 
  125
+        /// <summary>
  126
+        /// Can the timer be stopped?
  127
+        /// </summary>
  128
+        /// <returns></returns>
109 129
         bool CanStopTimerExecute()
110 130
         {
111 131
             return !_timer.Complete && _timer.Status == TimerState.Running;
@@ -115,24 +135,22 @@ bool CanStopTimerExecute()
115 135
 
116 136
         #region Reset Command
117 137
 
  138
+        /// <summary>
  139
+        /// Reset the timer and update corresponding values.
  140
+        /// </summary>
118 141
         void ResetTimerExecute()
119 142
         {
120 143
             _timer.Reset();
121  
-            UpdateTimerValues(_timer.Remaining);
122  
-        }
123  
-
124  
-        bool CanResetTimerExecute()
125  
-        {
126  
-            return true;
  144
+            UpdateTimerValues();
127 145
         }
128  
-
  146
+        
129 147
         private void BindCommands()
130 148
         {
131 149
             Settings = new RelayCommand(() => SettingsExecute());
132 150
             About = new RelayCommand(() => AboutExecute());
133 151
             StartTimer = new RelayCommand(() => StartTimerExecute(), CanStartTimerExecute);
134 152
             StopTimer = new RelayCommand(() => StopTimerExecute(), CanStopTimerExecute);
135  
-            ResetTimer = new RelayCommand(() => ResetTimerExecute(), CanResetTimerExecute);
  153
+            ResetTimer = new RelayCommand(() => ResetTimerExecute());
136 154
         }
137 155
         #endregion
138 156
 
@@ -181,6 +199,9 @@ public double PercentElapsed
181 199
             }
182 200
         }
183 201
 
  202
+        /// <summary>
  203
+        /// The background colour of the clock
  204
+        /// </summary>
184 205
         public Color StatusColor
185 206
         {
186 207
             get
@@ -198,6 +219,9 @@ public Color StatusColor
198 219
             }
199 220
         }
200 221
 
  222
+        /// <summary>
  223
+        /// The timer duration.
  224
+        /// </summary>
201 225
         public TimeSpan Duration
202 226
         {
203 227
             get
@@ -214,6 +238,9 @@ public TimeSpan Duration
214 238
             }
215 239
         }
216 240
 
  241
+        /// <summary>
  242
+        /// The number of times the countdown has completed successfully.
  243
+        /// </summary>
217 244
         public int CompletedCount
218 245
         {
219 246
             get
@@ -229,6 +256,9 @@ private set
229 256
             }
230 257
         }
231 258
 
  259
+        /// <summary>
  260
+        /// The brush that we paint the clock with.
  261
+        /// </summary>
232 262
         Brush statusBrush = new SolidColorBrush();
233 263
         public Brush StatusBrush
234 264
         {
@@ -247,27 +277,9 @@ private set
247 277
             }
248 278
         }
249 279
 
250  
-        private TaskbarItemInfo taskbarItemInfo = null;
251  
-
252 280
         /// <summary>
253  
-        /// Get or set a reference to the TaskbarItemInfo of the application
254  
-        /// so that the view model can update the progress bar in the taskbar
255  
-        /// application icon.
  281
+        /// The clock font family
256 282
         /// </summary>
257  
-        public TaskbarItemInfo TaskbarItemInfo
258  
-        {
259  
-            get
260  
-            {
261  
-                return taskbarItemInfo;
262  
-            }
263  
-            set
264  
-            {
265  
-                if (taskbarItemInfo == value)
266  
-                    return;
267  
-                taskbarItemInfo = value;
268  
-            }
269  
-        }
270  
-
271 283
         public FontFamily FontFamily
272 284
         {
273 285
             get
@@ -283,6 +295,9 @@ public FontFamily FontFamily
283 295
             }
284 296
         }
285 297
 
  298
+        /// <summary>
  299
+        /// The clock font size.
  300
+        /// </summary>
286 301
         public double FontSize
287 302
         {
288 303
             get
@@ -309,10 +324,11 @@ private void UpdateMembersFromSettings()
309 324
             _settings.Reload();
310 325
 
311 326
             Duration = _settings.Duration;
  327
+            
312 328
             FontSize = _settings.FontSize;
313 329
             FontFamily = _settings.FontFamily;
314 330
 
315  
-            UpdateTimerValues(_settings.Duration);
  331
+            UpdateTimerValues();
316 332
         }
317 333
 
318 334
         /// <summary>
@@ -341,13 +357,14 @@ void ConsumeMessage(SimpleMessage message)
341 357
                     break;
342 358
             }
343 359
         }
  360
+
344 361
         /// <summary>
345 362
         /// Update the timer view model properties based on the time span passed in.
346 363
         /// </summary>
347 364
         /// <param name="t"></param>
348 365
         private void UpdateTimer(TimeSpan t, TimerModelEventArgs e)
349 366
         {
350  
-            UpdateTimerValues(t);
  367
+            UpdateTimerValues();
351 368
             UpdateTimerStatusColor(e);
352 369
         }
353 370
 
@@ -384,8 +401,9 @@ private void UpdateTimerStatusColor(TimerModelEventArgs e)
384 401
         /// Update the timer view model properties based on the time span passed in.
385 402
         /// </summary>
386 403
         /// <param name="t"></param>
387  
-        private void UpdateTimerValues(TimeSpan t)
  404
+        private void UpdateTimerValues()
388 405
         {
  406
+            TimeSpan t = _timer.Remaining;
389 407
             TimerValue = string.Format("{0}:{1}:{2}", t.Hours.ToString("D2"),
390 408
                 t.Minutes.ToString("D2"), t.Seconds.ToString("D2"));
391 409
 
@@ -406,6 +424,12 @@ private void AddEventHandlers()
406 424
         #endregion
407 425
 
408 426
         #region Event handlers
  427
+
  428
+        /// <summary>
  429
+        /// Fires where the timer completes.
  430
+        /// </summary>
  431
+        /// <param name="sender"></param>
  432
+        /// <param name="e"></param>
409 433
         private void OnCompleted(object sender, TimerModelEventArgs e)
410 434
         {
411 435
             UpdateTimer(_timer.Remaining, e);
@@ -420,6 +444,12 @@ private void OnCompleted(object sender, TimerModelEventArgs e)
420 444
             Messenger.Default.Send(new TaskbarItemMessage { State = TaskbarItemProgressState.Normal, Value = 1.0 });
421 445
         }
422 446
 
  447
+        /// <summary>
  448
+        /// Fires when the timer ticks.  Ticks out to be of the order of 
  449
+        /// tenths of a second or so to prevent excessive spamming of this method.
  450
+        /// </summary>
  451
+        /// <param name="sender"></param>
  452
+        /// <param name="e"></param>
423 453
         private void OnTick(object sender, TimerModelEventArgs e)
424 454
         {
425 455
             UpdateTimer(_timer.Remaining, e);
@@ -428,6 +458,11 @@ private void OnTick(object sender, TimerModelEventArgs e)
428 458
             Messenger.Default.Send(new SimpleMessage(SimpleMessage.MessageType.TimerTick, TimerValue));
429 459
         }
430 460
 
  461
+        /// <summary>
  462
+        /// Fires when the timer starts.
  463
+        /// </summary>
  464
+        /// <param name="sender"></param>
  465
+        /// <param name="e"></param>
431 466
         void OnStarted(object sender, TimerModelEventArgs e)
432 467
         {
433 468
             UpdateTimer(_timer.Remaining, e);
@@ -440,6 +475,11 @@ void OnStarted(object sender, TimerModelEventArgs e)
440 475
             Messenger.Default.Send(new TaskbarItemMessage { State = TaskbarItemProgressState.Normal });
441 476
         }
442 477
 
  478
+        /// <summary>
  479
+        /// Fires when the timer stops.
  480
+        /// </summary>
  481
+        /// <param name="sender"></param>
  482
+        /// <param name="e"></param>
443 483
         private void OnStopped(object sender, TimerModelEventArgs e)
444 484
         {
445 485
             UpdateTimer(_timer.Remaining, e);
@@ -447,6 +487,11 @@ private void OnStopped(object sender, TimerModelEventArgs e)
447 487
             Messenger.Default.Send(new TaskbarItemMessage { State = TaskbarItemProgressState.Paused });
448 488
         }
449 489
 
  490
+        /// <summary>
  491
+        /// Fires when the timer resets.
  492
+        /// </summary>
  493
+        /// <param name="sender"></param>
  494
+        /// <param name="e"></param>
450 495
         private void OnReset(object sender, TimerModelEventArgs e)
451 496
         {
452 497
             UpdateTimer(_timer.Remaining, e);

0 notes on commit eed6776

Please sign in to comment.
Something went wrong with that request. Please try again.