Improve exercising view model documentation.
[profile/iot/apps/dotnet/fitness.git] / Fitness / ViewModels / ExercisingViewModel.cs
1 using System;
2 using System.Windows.Input;
3 using Fitness.Models;
4 using Fitness.Services;
5 using Tizen.Multimedia.Vision;
6 using Tizen.NUI.Binding;
7
8 namespace Fitness.ViewModels
9 {
10     /// <summary>
11     /// Class handling the exercising view.
12     /// </summary>
13     public class ExercisingViewModel : ChangeWorkoutViewModel
14     {
15         private WorkoutState state;
16         private IExerciseService squatService;
17         private int score;
18         private int hold;
19         private int repetitions;
20         private Landmark[,] poseLandmarks;
21
22         /// <summary>
23         /// Initializes a new instance of the <see cref="ExercisingViewModel"/> class.
24         /// </summary>
25         /// <param name="workoutViewModel">Workout view model information.</param>
26         public ExercisingViewModel(WorkoutViewModel workoutViewModel)
27         {
28             PauseResumeWorkout = new Command(TriggerPauseResumeWorkout);
29             TryAgain = new Command(ExecuteTryAgain);
30             EndWorkout = new Command(ExecuteEndWorkout);
31             State = WorkoutState.Playing;
32             CurrentWorkout = workoutViewModel;
33             squatService = new SquatService()
34             {
35                 HoldTimeThreshold = 1200,
36             };
37             squatService.ExerciseStateUpdated += SquatService_ExerciseStateUpdated;
38         }
39
40         /// <summary>
41         /// Gets the pose landmark property.
42         /// </summary>
43         public Landmark[,] PoseLandmarks
44         {
45             get => poseLandmarks;
46             private set
47             {
48                 if (value != poseLandmarks)
49                 {
50                     poseLandmarks = value;
51                     RaisePropertyChanged();
52                 }
53             }
54         }
55
56         /// <summary>
57         /// Gets the property specifying the correctness of the exercise - values ranging from 0 to 100.
58         /// </summary>
59         public int Score
60         {
61             get => score;
62             private set
63             {
64                 if (value != score)
65                 {
66                     score = value;
67                     RaisePropertyChanged();
68                 }
69             }
70         }
71
72         /// <summary>
73         /// Gets the property specifying the number of repetitions of the exercise.
74         /// </summary>
75         public int Repetitions
76         {
77             get => repetitions;
78             private set
79             {
80                 if (value != repetitions)
81                 {
82                     repetitions = value;
83                     RaisePropertyChanged();
84                 }
85             }
86         }
87
88         /// <summary>
89         /// Gets the property specifying the time of the exercise performed - values ranging from 0 to 5.
90         /// </summary>
91         public int Hold
92         {
93             get => hold;
94             private set
95             {
96                 if (value != hold)
97                 {
98                     hold = value;
99                     RaisePropertyChanged();
100                 }
101             }
102         }
103
104         /// <summary>
105         /// Gets the TimeLeft in workout.
106         /// </summary>
107         public TimeSpan TimeLeft { get; private set; } = TimeSpan.FromSeconds(27);
108
109         /// <summary>
110         /// Gets the current workout state.
111         /// </summary>
112         public WorkoutState State
113         {
114             get => state;
115             private set
116             {
117                 if (state != value)
118                 {
119                     state = value;
120                     RaisePropertyChanged();
121                 }
122             }
123         }
124
125         /// <summary>
126         /// Gets the pause workout command.
127         /// </summary>
128         public ICommand Pause { get; private set; }
129
130         /// <summary>
131         /// Gets the URL to video with workout.
132         /// </summary>
133         public string WorkoutVideoUrl { get; private set; }
134
135         /// <summary>
136         /// Gets the URL to preview from device camera.
137         /// </summary>
138         public string PreviewVideoUrl { get; private set; }
139
140         /// <summary>
141         /// Gets Prev Command.
142         /// </summary>
143         public ICommand Prev { get; private set; }
144
145         /// <summary>
146         /// Gets Next Command.
147         /// </summary>
148         public ICommand Next { get; private set; }
149
150         /// <summary>
151         /// Gets PauseWorkout Command.
152         /// </summary>
153         public ICommand PauseResumeWorkout { get; private set; }
154
155         /// <summary>
156         /// Gets TryAgain Command.
157         /// </summary>
158         public ICommand TryAgain { get; private set; }
159
160         /// <summary>
161         /// Gets EndWorkout Command.
162         /// </summary>
163         public ICommand EndWorkout { get; private set; }
164
165         /// <summary>
166         /// Gets the pause/resume label.
167         /// </summary>
168         public string PauseResumeLabel { get; private set; } = "Pause";
169
170         /// <summary>
171         /// Gets the <see cref="SquatService"/> property.
172         /// </summary>
173         public IExerciseService SquatService { get; private set; }
174
175         /// <inheritdoc />
176         protected override void GoPrevious()
177         {
178             if (State == WorkoutState.Playing)
179             {
180                 State = WorkoutState.Paused;
181             }
182
183             Services.NavigationService.Instance.NavigateToSummaryView(SummaryType.ChangeToPreviousWorkout, CurrentWorkout);
184         }
185
186         /// <inheritdoc />
187         protected override void GoNext()
188         {
189             if (State == WorkoutState.Playing)
190             {
191                 State = WorkoutState.Paused;
192             }
193
194             Services.NavigationService.Instance.NavigateToSummaryView(SummaryType.ChangeToNextWorkout, CurrentWorkout);
195         }
196
197         private void SquatService_ExerciseStateUpdated(object sender, ExerciseEventArgs e)
198         {
199             PoseLandmarks = e.PoseLandmarks;
200             Score = e.Score;
201             Repetitions = e.Count;
202             Hold = e.Hold;
203         }
204
205         private void TriggerPauseResumeWorkout()
206         {
207             if (State == WorkoutState.Paused)
208             {
209                 State = WorkoutState.Playing;
210             }
211             else if (State == WorkoutState.Playing)
212             {
213                 State = WorkoutState.Paused;
214             }
215         }
216
217         private void ExecuteTryAgain()
218         {
219             Services.NavigationService.Instance.NavigateToSummaryView(SummaryType.TryAgain, CurrentWorkout);
220         }
221
222         private void ExecuteEndWorkout()
223         {
224             Services.NavigationService.Instance.NavigateToSummaryView(SummaryType.EndWorkout, CurrentWorkout);
225         }
226     }
227 }