[NUI] Change for Size2D property to own internalSize2D which is cashed to prevent...
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / TimerTest1 / ViewModels / TimerTest1ViewModel.cs
1 using System;
2 using System.ComponentModel;
3 using System.Runtime.CompilerServices;
4 using System.Windows.Input;
5 using Tizen.NUI.Binding;
6 using Tizen.NUI;
7 using System.Diagnostics;
8
9 namespace NUITizenGallery.ViewModels
10 {
11     public class TimerTest1ViewModel : INotifyPropertyChanged, IDisposable
12     {
13         private readonly Timer timer;
14         private readonly Stopwatch stopWatch = new Stopwatch();
15         private bool timerStarted;
16         private string hoursMinutesSeconds;
17         private string miliseconds;
18         private bool disposed = false;
19
20         public ICommand StartTimer
21         {
22             get;
23             private set;
24         }
25
26         public ICommand StopTimer
27         {
28             get;
29             private set;
30         }
31
32         public TimerTest1ViewModel()
33         {
34             timerStarted = false;
35             timer = new Timer(100);
36
37             timer.Tick += OnTimerIntervalElapsed;
38             timer.Stop();
39
40             StartTimer = new Command(ExecuteStartTimer);
41             StopTimer = new Command(ExecuteStopTimer);
42             HoursMinutesSeconds = "00:00";
43             Miliseconds = ".00";
44         }
45
46         private bool OnTimerIntervalElapsed(object sender, Timer.TickEventArgs e)
47         {
48             var ts = stopWatch.Elapsed;
49             HoursMinutesSeconds = ts.Hours == 0 ?
50                 string.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds) :
51                 string.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);
52
53             Miliseconds = string.Format(".{0:00}", ts.Milliseconds / 10);
54
55             return true;
56         }
57
58         public bool TimerStarted
59         {
60             get => timerStarted;
61             set
62             {
63                 if (timerStarted != value)
64                 {
65                     timerStarted = value;
66                     RaisePropertyChagend();
67                 }
68             }
69         }
70
71         public string HoursMinutesSeconds
72         {
73             get => hoursMinutesSeconds;
74             set
75             {
76                 if (hoursMinutesSeconds != value)
77                 {
78                     hoursMinutesSeconds = value;
79                     RaisePropertyChagend();
80                 }
81             }
82         }
83
84         public string Miliseconds
85         {
86             get => miliseconds;
87             set
88             {
89                 if (miliseconds != value)
90                 {
91                     miliseconds = value;
92                     RaisePropertyChagend();
93                 }
94             }
95         }
96
97         private void ExecuteStartTimer()
98         {
99             TimerStarted = true;
100             timer.Start();
101             stopWatch.Start();
102         }
103
104
105         private void ExecuteStopTimer()
106         {
107             TimerStarted = false;
108             timer.Stop();
109             stopWatch.Stop();
110         }
111
112
113         public event PropertyChangedEventHandler PropertyChanged;
114
115         private void RaisePropertyChagend([CallerMemberName] string propertyName = null)
116         {
117             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
118         }
119
120         public void Dispose()
121         {
122             Dispose(true);
123             GC.SuppressFinalize(this);
124         }
125
126         protected virtual void Dispose(bool disposing)
127         {
128             if (!disposed)
129             {
130                 if (disposing)
131                 {
132                     timer.Tick -= OnTimerIntervalElapsed;
133                     timer?.Dispose();
134                 }
135
136                 disposed = true;
137             }
138         }
139     }
140 }