1 /*jslint browser: true, devel: true */
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 )
57 // deprecated for this version;
58 increasingStrength: true,
83 trainingEnabled: false
92 * Used for update GraphSchedule;
93 * [ workday / weekend ]
95 currentTypeOfPeriods: 'workday',
97 * Use on form to edit time period;
99 currentEditingTimePeriod: null,
100 currentEditingTimePeriodId: -1,
103 * Date when alarm will start generating
108 * use store temporary data for alarms;
113 * HTML5 audio element for play audio when alarm is called
118 * Instance of User Interface
124 * Load configuration of application
127 ExercisePlanner.prototype.loadConfig = function () {
128 console.log('ExercisePlanner_loadConfig');
129 var configStr = localStorage.getItem('config');
131 this.config = JSON.parse(configStr);
133 this.removeAllAlarms();
134 this.sortTimeRanges();
139 * Save configuration of application
142 ExercisePlanner.prototype.saveConfig = function () {
143 console.log('ExercisePlanner_saveConfig');
144 localStorage.setItem('config', JSON.stringify(this.config));
147 ExercisePlanner.prototype.stopTraining = function () {
148 console.log('ExercisePlanner_stopTraining');
149 this.removeAllAlarms();
150 this.ui.setStatusRun(this.config.trainingEnabled);
153 ExercisePlanner.prototype.startTraining = function () {
154 console.log('ExercisePlanner_startTraining');
155 this.ui.setStatusRun(this.config.trainingEnabled);
160 * Toggle start/stop alarms for workouts
162 ExercisePlanner.prototype.appStartStop = function () {
163 console.log('ExercisePlanner_appStartStop');
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 () {
177 console.log('ExercisePlanner_exit');
180 tizen.application.getCurrentApplication().exit();
184 * Sets frequency value and calculates new alarms
187 ExercisePlanner.prototype.setFrequency = function (value) {
188 console.log('ExercisePlanner_setFrequency', value);
189 this.config.frequency[this.config.currentTypeOfPeriods] = parseInt(value, 10);
192 this.generateAlarms();
193 this.updateGraph(this.config.currentTypeOfPeriods);
194 this.showNextAlarm();
201 ExercisePlanner.prototype.setStrength = function (value) {
202 console.log('ExercisePlanner_setStrength', value);
203 this.config.strength[this.config.currentTypeOfPeriods] = parseInt(value, 10);
208 * Sending array of exercises to UI for update
210 ExercisePlanner.prototype.updateExercises = function () {
211 console.log('ExercisePlanner_updateExercises');
212 this.ui.fillExercises(this.config.exercises);
216 * Sending array of time ranges to UI for update
217 * & update graph schedule
219 ExercisePlanner.prototype.updateTimesRanges = function () {
220 console.log('ExercisePlanner_updateTimesRanges');
222 this.ui.fillTimesRanges(this.config.timesRanges);
223 this.ui.graphSchedule.updateTimeRanges();
227 * Store exercises in config (and save)
230 ExercisePlanner.prototype.saveExercises = function (newData) {
231 console.log('ExercisePlanner_saveExercises', newData);
235 for (i = 0, l = newData.length; i < l; i += 1) {
236 this.config.exercises[i].enabled = newData[i].checked;
238 this.generateNearestExercise();
244 * When will earliest workout
247 ExercisePlanner.prototype.showNextAlarm = function showNextAlarm() {
248 console.log('ExercisePlanner_showNextAlarm');
250 currentDate = new Date();
252 if (this.alarms.everyday.length > 0) {
253 alarms = this.alarms.everyday;
255 alarms = (this.todayIsWorkday()) ? this.alarms.workday : this.alarms.weekend;
258 alarms = alarms.filter(function (item) {
259 return (item.getTime() > currentDate.getTime());
260 }).sort(function (a, b) {
261 return a.date - b.date;
265 if (this.config.nearestExercise > -1) {
266 this.ui.showAlarmInMonitor({
268 exerciseName: this.config.exercises[this.config.nearestExercise].name,
269 numberOfTimes: this.getStrength(this.config.strength.workday, this.config.count)
272 this.config.count += 1;
278 * Change type of periods [workday/weekend] and update graph
281 ExercisePlanner.prototype.changeTypeOfPeriods = function changeTypeOfPeriods(type) {
282 if (this.config.currentTypeOfPeriods !== type) {
283 this.config.currentTypeOfPeriods = type;
284 this.updateGraph(this.config.currentTypeOfPeriods);
289 * Check new exercise name for duplication in existings;
293 ExercisePlanner.prototype.checkExerciseName = function (name) {
294 console.log('ExercisePlanner_checkExerciseName', name);
298 for (i = 0, l = this.config.exercises.length; i < l; i += 1) {
299 if (this.config.exercises[i].name === name) {
308 * Add new exercise sent from UI
312 ExercisePlanner.prototype.addExercise = function (name) {
313 console.log('ExercisePlanner_addExercise', name);
314 if (this.checkExerciseName(name) < 0) {
315 this.config.exercises.push({
320 this.ui.fillExercises(this.config.exercises);
323 this.ui.showErrors({name: 'Element exists!'});
328 * Get number of workouts by frequency
332 ExercisePlanner.prototype.getNumberOfWorkoutsByFrequency = function getNumberOfWorkoutsByFrequency(value) {
333 console.log('ExercisePlanner_frequencyMap', value);
334 var iMap = [1, 2, 4, 8, 16, 24, 150],
335 // -- times per 24h; proportion to set periods of available time;
336 numberOfWorkouts = iMap[value];
338 return numberOfWorkouts || 2;
342 * Get number of exercises in workout by strength value and optional ;
347 ExercisePlanner.prototype.getStrength = function strengthMap(value, count) {
348 console.log('ExercisePlanner_strengthMap', value, count);
349 var sMap = [1, 1, 2, 4, 10, 20],
350 base = sMap[value] || 2;
353 return Math.round(base * (count / 10 + 1));
357 * Generate name of exercise for nearest workout
359 ExercisePlanner.prototype.generateNearestExercise = function () {
360 console.log('ExercisePlanner_generateNearestExercise');
361 var tmp = this.config.exercises.filter(function (item) {
364 this.config.nearestExercise = parseInt(Math.random() * tmp.length, 10);
369 * If user want change work days this method will changing;
372 ExercisePlanner.prototype.todayIsWorkday = function todayIsWorkday() {
373 var day = (new Date()).getDay();
374 return (day >= 1 && day <= 5);
378 * Activate alarms in API.
380 ExercisePlanner.prototype.startAlarms = function startAlarms() {
382 this.removeAllAlarms();
385 this.addAlarmsAllWeek(this.alarms);
387 this.generateNearestExercise();
388 this.showNextAlarm();
392 * Update Graph object
393 * @param {String} typeOfPeriods ['workday'|'weekend']
395 ExercisePlanner.prototype.updateGraph = function updateGraph(typeOfPeriods) {
396 console.log('updateGraph');
398 if (!this.ui.graphSchedule) {
400 message: 'graph schedule not exists.'
404 typeOfPeriods = typeOfPeriods || ((this.todayIsWorkday()) ? 'workday' : 'weekend');
405 console.log('typeOfPeriods: ', typeOfPeriods);
407 if (typeOfPeriods === 'workday') {
408 alarms = this.alarms.workday;
410 alarms = this.alarms.weekend;
412 if (alarms.length === 0) {
413 alarms = this.alarms.everyday;
415 this.ui.graphSchedule.setTimeRanges(this.periodsWeekToBoolArray());
416 this.ui.graphSchedule.pushTimeOfFlags(alarms);
417 this.ui.graphSchedule.showFlags();
421 * Callback function on visibility change;
423 ExercisePlanner.prototype.onVisibilityChange = function () {
425 switch (document.webkitVisibilityState) {
427 this.applicationStartTime = new Date();
428 this.currentAlarm = this.findCurrentAlarm();
429 if (this.currentAlarm.length > 0) {
430 this.ui.showWaitOk();
438 * Turn off all alarms today
440 ExercisePlanner.prototype.todayOffAll = function todayOffAll() {
441 // set begin date to tomorrow;
442 this.beginDate = new Date();
443 this.beginDate.setDate(this.beginDate.getDate() + 1);
445 this.generateAlarms();
449 // Initialize function
450 ExercisePlanner.prototype.init = function init() {
451 var onUiInitialize = function onUiInitialize() {
452 console.log('onUiInitialize');
453 // register watcher on visibility change;
454 document.addEventListener('webkitvisibilitychange', this.onVisibilityChange.bind(this));
455 this.showNextAlarm();
456 this.onVisibilityChange();
459 console.log('ExercisePlanner..init');
460 this.selfId = tizen.application.getAppContext().appId;
465 this.config.currentTypeOfPeriods = (this.todayIsWorkday()) ? 'workday' : 'weekend';
467 this.generateAlarms();
469 this.ui.initialize(onUiInitialize);