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 var configStr = localStorage.getItem('config');
130 this.config = JSON.parse(configStr);
132 this.removeAllAlarms();
133 this.sortTimeRanges();
138 * Save configuration of application
141 ExercisePlanner.prototype.saveConfig = function () {
142 localStorage.setItem('config', JSON.stringify(this.config));
145 ExercisePlanner.prototype.stopTraining = function () {
146 this.removeAllAlarms();
147 this.ui.setStatusRun(this.config.trainingEnabled);
150 ExercisePlanner.prototype.startTraining = function () {
151 this.ui.setStatusRun(this.config.trainingEnabled);
156 * Toggle start/stop alarms for workouts
158 ExercisePlanner.prototype.appStartStop = function () {
159 this.config.trainingEnabled = !this.config.trainingEnabled;
160 if (this.config.trainingEnabled) {
161 this.startTraining();
169 * Closing application with the configuration data saving
171 ExercisePlanner.prototype.exit = function () {
174 tizen.application.getCurrentApplication().exit();
178 * Sets frequency value and calculates new alarms
181 ExercisePlanner.prototype.setFrequency = function (value) {
182 this.config.frequency[this.config.currentTypeOfPeriods] = parseInt(value, 10);
185 this.generateAlarms();
186 this.updateGraph(this.config.currentTypeOfPeriods);
187 this.showNextAlarm();
194 ExercisePlanner.prototype.setStrength = function (value) {
195 this.config.strength[this.config.currentTypeOfPeriods] = parseInt(value, 10);
200 * Sending array of exercises to UI for update
202 ExercisePlanner.prototype.updateExercises = function () {
203 this.ui.fillExercises(this.config.exercises);
207 * Sending array of time ranges to UI for update
208 * & update graph schedule
210 ExercisePlanner.prototype.updateTimesRanges = function () {
212 this.ui.fillTimesRanges(this.config.timesRanges);
213 this.ui.graphSchedule.updateTimeRanges();
217 * Store exercises in config (and save)
220 ExercisePlanner.prototype.saveExercises = function (newData) {
224 for (i = 0, l = newData.length; i < l; i += 1) {
225 this.config.exercises[i].enabled = newData[i].checked;
227 this.generateNearestExercise();
233 * When will earliest workout
236 ExercisePlanner.prototype.showNextAlarm = function showNextAlarm() {
238 currentDate = new Date();
240 if (this.alarms.everyday.length > 0) {
241 alarms = this.alarms.everyday;
243 alarms = (this.todayIsWorkday()) ? this.alarms.workday : this.alarms.weekend;
246 alarms = alarms.filter(function (item) {
247 return (item.getTime() > currentDate.getTime());
248 }).sort(function (a, b) {
249 return a.date - b.date;
252 if (this.config.nearestExercise > -1) {
253 this.ui.showAlarmInMonitor({
255 exerciseName: this.config.exercises[this.config.nearestExercise].name,
256 numberOfTimes: this.getStrength(this.config.strength.workday, this.config.count)
259 this.config.count += 1;
265 * Change type of periods [workday/weekend] and update graph
268 ExercisePlanner.prototype.changeTypeOfPeriods = function changeTypeOfPeriods(type) {
269 if (this.config.currentTypeOfPeriods !== type) {
270 this.config.currentTypeOfPeriods = type;
271 this.updateGraph(this.config.currentTypeOfPeriods);
276 * Check new exercise name for duplication in existings;
280 ExercisePlanner.prototype.checkExerciseName = function (name) {
284 for (i = 0, l = this.config.exercises.length; i < l; i += 1) {
285 if (this.config.exercises[i].name === name) {
295 * Add new exercise sent from UI
299 ExercisePlanner.prototype.addExercise = function (name) {
300 if (this.checkExerciseName(name) < 0) {
301 this.config.exercises.push({
306 this.ui.fillExercises(this.config.exercises);
309 this.ui.showErrors([{name: 'Element exists!'}]);
314 * Get number of workouts by frequency
318 ExercisePlanner.prototype.getNumberOfWorkoutsByFrequency = function getNumberOfWorkoutsByFrequency(value) {
319 var iMap = [1, 2, 4, 8, 16, 24, 150],
320 // -- times per 24h; proportion to set periods of available time;
321 numberOfWorkouts = iMap[value];
323 return numberOfWorkouts || 2;
327 * Get number of exercises in workout by strength value and optional ;
332 ExercisePlanner.prototype.getStrength = function strengthMap(value, count) {
333 var sMap = [1, 1, 2, 4, 10, 20],
334 base = sMap[value] || 2;
337 return Math.round(base * (count / 10 + 1));
341 * Generate name of exercise for nearest workout
343 ExercisePlanner.prototype.generateNearestExercise = function () {
344 var tmp = this.config.exercises.filter(function (item) {
347 this.config.nearestExercise = parseInt(Math.random() * tmp.length, 10);
352 * If user want change work days this method will changing;
355 ExercisePlanner.prototype.todayIsWorkday = function todayIsWorkday() {
356 var day = (new Date()).getDay();
357 return (day >= 1 && day <= 5);
361 * Activate alarms in API.
363 ExercisePlanner.prototype.startAlarms = function startAlarms() {
365 this.removeAllAlarms();
368 this.addAlarmsAllWeek(this.alarms);
370 this.generateNearestExercise();
371 this.showNextAlarm();
375 * Update Graph object
376 * @param {String} typeOfPeriods ['workday'|'weekend']
378 ExercisePlanner.prototype.updateGraph = function updateGraph(typeOfPeriods) {
380 if (!this.ui.graphSchedule) {
382 message: 'graph schedule not exists.'
386 typeOfPeriods = typeOfPeriods || ((this.todayIsWorkday()) ? 'workday' : 'weekend');
388 if (typeOfPeriods === 'workday') {
389 alarms = this.alarms.workday;
391 alarms = this.alarms.weekend;
393 if (alarms.length === 0) {
394 alarms = this.alarms.everyday;
396 this.ui.graphSchedule.setTimeRanges(this.periodsWeekToBoolArray());
397 this.ui.graphSchedule.pushTimeOfFlags(alarms);
398 this.ui.graphSchedule.showFlags();
402 * Callback function on visibility change;
404 ExercisePlanner.prototype.onVisibilityChange = function () {
406 switch (document.webkitVisibilityState) {
408 this.applicationStartTime = new Date();
409 this.currentAlarm = this.findCurrentAlarm();
410 if (this.currentAlarm.length > 0) {
411 this.ui.showWaitOk();
419 * Turn off all alarms today
421 ExercisePlanner.prototype.todayOffAll = function todayOffAll() {
422 // set begin date to tomorrow;
423 this.beginDate = new Date();
424 this.beginDate.setDate(this.beginDate.getDate() + 1);
426 this.generateAlarms();
430 // Initialize function
431 ExercisePlanner.prototype.init = function init() {
432 var onUiInitialize = function onUiInitialize() {
433 // register watcher on visibility change;
434 document.addEventListener('webkitvisibilitychange', this.onVisibilityChange.bind(this));
435 this.showNextAlarm();
436 this.onVisibilityChange();
439 this.selfId = tizen.application.getAppContext().appId;
444 this.config.currentTypeOfPeriods = (this.todayIsWorkday()) ? 'workday' : 'weekend';
446 this.generateAlarms();
448 this.ui.initialize(onUiInitialize);