2 /*global tizen, $, app, localStorage, Audio, document, unlockScreen, UI */
3 var ExercisePlanner = function () {
10 ExercisePlanner.prototype = {
12 * Definition of time for sleep
17 * Definition one day in hours
22 * Stored time of application start
24 applicationStartTime: new Date(),
27 * Cofiguration data will saved for next launch;
28 * There are default values after install;
33 workday: 3, // 6 for test
44 * - timeRanges:style [ everyday, weekend, workday ]; ( workday : mon-fri )
58 * List of generate available exercises;
60 availableExercises: [],
62 // deprecated for this version;
63 increasingStrength: true,
88 trainingEnabled: false
97 * Used for update GraphSchedule;
98 * [ workday / weekend ]
100 currentTypeOfPeriods: 'workday',
102 * Use on form to edit time period;
104 currentEditingTimePeriod: null,
105 currentEditingTimePeriodId: -1,
108 * Date when alarm will start generating
113 * use store temporary data for alarms;
118 * HTML5 audio element for play audio when alarm is called
123 * Instance of User Interface
129 * Load configuration of application
132 ExercisePlanner.prototype.loadConfig = function () {
133 var configStr = localStorage.getItem('config');
135 this.config = JSON.parse(configStr);
137 this.removeAllAlarms();
138 this.sortTimeRanges();
143 * Save configuration of application
146 ExercisePlanner.prototype.saveConfig = function () {
147 localStorage.setItem('config', JSON.stringify(this.config));
150 ExercisePlanner.prototype.stopTraining = function () {
151 this.removeAllAlarms();
152 this.ui.setStatusRun(this.config.trainingEnabled);
155 ExercisePlanner.prototype.startTraining = function () {
156 this.ui.setStatusRun(this.config.trainingEnabled);
161 * Toggle start/stop alarms for workouts
163 ExercisePlanner.prototype.appStartStop = function () {
164 this.config.trainingEnabled = !this.config.trainingEnabled;
165 if (this.config.trainingEnabled) {
166 this.startTraining();
174 * Closing application with the configuration data saving
176 ExercisePlanner.prototype.exit = function () {
179 tizen.application.getCurrentApplication().exit();
183 * Sets frequency value and calculates new alarms
186 ExercisePlanner.prototype.setFrequency = function (value) {
187 this.config.frequency[this.config.currentTypeOfPeriods] = parseInt(value, 10);
190 this.generateAlarms();
191 this.updateGraph(this.config.currentTypeOfPeriods);
192 this.showNextAlarm();
199 ExercisePlanner.prototype.setStrength = function (value) {
200 this.config.strength[this.config.currentTypeOfPeriods] = parseInt(value, 10);
205 * Sending array of exercises to UI for update
207 ExercisePlanner.prototype.updateExercises = function () {
208 this.ui.fillExercises(this.config.exercises);
212 * Sending array of time ranges to UI for update
213 * & update graph schedule
215 ExercisePlanner.prototype.updateTimesRanges = function () {
217 this.ui.fillTimesRanges(this.config.timesRanges);
218 this.ui.graphSchedule.updateTimeRanges();
222 * Store exercises in config (and save)
225 ExercisePlanner.prototype.saveExercises = function (newData) {
229 for (i = 0, l = newData.length; i < l; i += 1) {
230 this.config.exercises[i].enabled = newData[i].checked;
232 this.generateNearestExercise();
238 * When will earliest workout
241 ExercisePlanner.prototype.showNextAlarm = function showNextAlarm() {
243 currentDate = new Date();
245 if (this.alarms.everyday.length > 0) {
246 alarms = this.alarms.everyday;
248 alarms = (this.todayIsWorkday()) ? this.alarms.workday : this.alarms.weekend;
251 alarms = alarms.filter(function (item) {
252 return (item.getTime() > currentDate.getTime());
253 }).sort(function (a, b) {
254 return a.date - b.date;
257 if (this.config.availableExercises.length > 0) {
258 this.ui.showAlarmInMonitor({
260 exerciseName: this.config.availableExercises[this.config.nearestExercise].name,
261 numberOfTimes: this.getStrength(this.config.strength.workday, this.config.count)
268 * Change type of periods [workday/weekend] and update graph
271 ExercisePlanner.prototype.changeTypeOfPeriods = function changeTypeOfPeriods(type) {
272 if (this.config.currentTypeOfPeriods !== type) {
273 this.config.currentTypeOfPeriods = type;
274 this.updateGraph(this.config.currentTypeOfPeriods);
279 * Check new exercise name for duplication in existings;
283 ExercisePlanner.prototype.checkExerciseName = function (name) {
287 for (i = 0, l = this.config.exercises.length; i < l; i += 1) {
288 if (this.config.exercises[i].name === name) {
297 * Add new exercise sent from UI
301 ExercisePlanner.prototype.addExercise = function (name) {
302 if (this.checkExerciseName(name) === true) {
303 this.config.exercises.push({
308 this.ui.fillExercises(this.config.exercises);
312 this.ui.showErrors([{name: 'Given exercise already exists!'}]);
317 * Get number of workouts by frequency
321 ExercisePlanner.prototype.getNumberOfWorkoutsByFrequency = function getNumberOfWorkoutsByFrequency(value) {
322 var iMap = [1, 2, 4, 8, 16, 24, 150],
323 // -- times per 24h; proportion to set periods of available time;
324 numberOfWorkouts = iMap[value];
326 return numberOfWorkouts || 2;
330 * Get number of exercises in workout by strength value and optional ;
335 ExercisePlanner.prototype.getStrength = function strengthMap(value, count) {
336 var sMap = [1, 1, 2, 4, 10, 20],
337 base = sMap[value] || 2;
340 return Math.round(base * (count / 10 + 1));
344 * Generate name of exercise for nearest workout
346 ExercisePlanner.prototype.generateNearestExercise = function () {
347 this.config.availableExercises = this.config.exercises.filter(function (item) {
350 this.config.nearestExercise = parseInt(Math.random() * this.config.availableExercises.length, 10);
355 * If user want change work days this method will changing;
358 ExercisePlanner.prototype.todayIsWorkday = function todayIsWorkday() {
359 var day = (new Date()).getDay();
360 return (day >= 1 && day <= 5);
364 * Activate alarms in API.
366 ExercisePlanner.prototype.startAlarms = function startAlarms() {
368 this.removeAllAlarms();
371 this.addAlarmsAllWeek(this.alarms);
373 this.generateNearestExercise();
374 this.showNextAlarm();
378 * Update Graph object
379 * @param {String} typeOfPeriods ['workday'|'weekend']
381 ExercisePlanner.prototype.updateGraph = function updateGraph(typeOfPeriods) {
383 if (!this.ui.graphSchedule) {
385 message: 'graph schedule not exists.'
389 typeOfPeriods = typeOfPeriods || ((this.todayIsWorkday()) ? 'workday' : 'weekend');
391 if (typeOfPeriods === 'workday') {
392 alarms = this.alarms.workday;
394 alarms = this.alarms.weekend;
396 if (alarms.length === 0) {
397 alarms = this.alarms.everyday;
399 this.ui.graphSchedule.setTimeRanges(this.periodsWeekToBoolArray());
400 this.ui.graphSchedule.pushTimeOfFlags(alarms);
401 this.ui.graphSchedule.showFlags();
405 * Callback function on visibility change;
407 ExercisePlanner.prototype.onVisibilityChange = function () {
409 switch (document.webkitVisibilityState) {
411 this.applicationStartTime = new Date();
412 this.currentAlarm = this.findCurrentAlarm();
413 if (this.currentAlarm.length > 0) {
414 this.ui.showWaitOk();
422 * Turn off all alarms today
424 ExercisePlanner.prototype.todayOffAll = function todayOffAll() {
425 // set begin date to tomorrow;
426 this.beginDate = new Date();
427 this.beginDate.setDate(this.beginDate.getDate() + 1);
429 this.generateAlarms();
433 // Initialize function
434 ExercisePlanner.prototype.init = function init() {
435 var onUiInitialize = function onUiInitialize() {
436 // register watcher on visibility change;
437 document.addEventListener('webkitvisibilitychange', this.onVisibilityChange.bind(this));
438 this.showNextAlarm();
439 this.onVisibilityChange();
442 this.selfId = tizen.application.getAppContext().appId;
447 this.config.currentTypeOfPeriods = (this.todayIsWorkday()) ? 'workday' : 'weekend';
449 this.generateAlarms();
450 this.ui.initialize(onUiInitialize);