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 () {
148 localStorage.setItem('config', JSON.stringify(this.config));
150 if (e.code === 22) //QuotaExceededError
151 this.ui.showErrors([{name: 'Not enough memory. Please remove unnecessary files'}]);
155 ExercisePlanner.prototype.stopTraining = function () {
156 this.removeAllAlarms();
157 this.ui.setStatusRun(this.config.trainingEnabled);
160 ExercisePlanner.prototype.startTraining = function () {
161 this.ui.setStatusRun(this.config.trainingEnabled);
166 * Toggle start/stop alarms for workouts
168 ExercisePlanner.prototype.appStartStop = function () {
169 this.config.trainingEnabled = !this.config.trainingEnabled;
170 if (this.config.trainingEnabled) {
171 this.startTraining();
179 * Closing application with the configuration data saving
181 ExercisePlanner.prototype.exit = function () {
184 tizen.application.getCurrentApplication().exit();
188 * Sets frequency value and calculates new alarms
191 ExercisePlanner.prototype.setFrequency = function (value) {
192 this.config.frequency[this.config.currentTypeOfPeriods] = parseInt(value, 10);
195 this.generateAlarms();
196 this.updateGraph(this.config.currentTypeOfPeriods);
197 this.showNextAlarm();
204 ExercisePlanner.prototype.setStrength = function (value) {
205 this.config.strength[this.config.currentTypeOfPeriods] = parseInt(value, 10);
210 * Sending array of exercises to UI for update
212 ExercisePlanner.prototype.updateExercises = function () {
213 this.ui.fillExercises(this.config.exercises);
217 * Sending array of time ranges to UI for update
218 * & update graph schedule
220 ExercisePlanner.prototype.updateTimesRanges = function () {
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) {
234 for (i = 0, l = newData.length; i < l; i += 1) {
235 this.config.exercises[i].enabled = newData[i].checked;
237 this.generateNearestExercise();
243 * When will earliest workout
246 ExercisePlanner.prototype.showNextAlarm = function showNextAlarm() {
248 currentDate = new Date();
250 if (this.alarms.everyday.length > 0) {
251 alarms = this.alarms.everyday;
253 alarms = (this.todayIsWorkday()) ? this.alarms.workday : this.alarms.weekend;
256 alarms = alarms.filter(function (item) {
257 return (item.getTime() > currentDate.getTime());
258 }).sort(function (a, b) {
259 return a.date - b.date;
262 if (this.config.availableExercises.length > 0) {
263 this.ui.showAlarmInMonitor({
265 exerciseName: this.config.availableExercises[this.config.nearestExercise].name,
266 numberOfTimes: this.getStrength(this.config.strength.workday, this.config.count)
273 * Change type of periods [workday/weekend] and update graph
276 ExercisePlanner.prototype.changeTypeOfPeriods = function changeTypeOfPeriods(type) {
277 this.config.currentTypeOfPeriods = type;
278 this.updateGraph(this.config.currentTypeOfPeriods);
282 * Check new exercise name for duplication in existings;
286 ExercisePlanner.prototype.checkExerciseName = function (name) {
290 for (i = 0, l = this.config.exercises.length; i < l; i += 1) {
291 if (this.config.exercises[i].name === name) {
300 * Add new exercise sent from UI
304 ExercisePlanner.prototype.addExercise = function (name) {
305 if (this.checkExerciseName(name) === true) {
306 this.config.exercises.push({
311 this.ui.fillExercises(this.config.exercises);
315 this.ui.showErrors([{name: 'Given exercise already exists!'}]);
320 * Get number of workouts by frequency
324 ExercisePlanner.prototype.getNumberOfWorkoutsByFrequency = function getNumberOfWorkoutsByFrequency(value) {
325 var iMap = [1, 2, 4, 8, 16, 24, 150],
326 // -- times per 24h; proportion to set periods of available time;
327 numberOfWorkouts = iMap[value];
329 return numberOfWorkouts || 2;
333 * Get number of exercises in workout by strength value and optional ;
338 ExercisePlanner.prototype.getStrength = function strengthMap(value, count) {
339 var sMap = [1, 1, 2, 4, 10, 20],
340 base = sMap[value] || 2;
343 return Math.round(base * (count / 10 + 1));
347 * Generate name of exercise for nearest workout
349 ExercisePlanner.prototype.generateNearestExercise = function () {
350 this.config.availableExercises = this.config.exercises.filter(function (item) {
353 this.config.nearestExercise = parseInt(Math.random() * this.config.availableExercises.length, 10);
358 * If user want change work days this method will changing;
361 ExercisePlanner.prototype.todayIsWorkday = function todayIsWorkday() {
362 var day = (new Date()).getDay();
363 return (day >= 1 && day <= 5);
367 * Activate alarms in API.
369 ExercisePlanner.prototype.startAlarms = function startAlarms() {
371 this.removeAllAlarms();
374 this.addAlarmsAllWeek(this.alarms);
376 this.generateNearestExercise();
377 this.showNextAlarm();
381 * Update Graph object
382 * @param {String} typeOfPeriods ['workday'|'weekend']
384 ExercisePlanner.prototype.updateGraph = function updateGraph(typeOfPeriods) {
386 if (!this.ui.graphSchedule) {
388 message: 'graph schedule not exists.'
392 typeOfPeriods = typeOfPeriods || ((this.todayIsWorkday()) ? 'workday' : 'weekend');
394 if (typeOfPeriods === 'workday') {
395 alarms = this.alarms.workday;
397 alarms = this.alarms.weekend;
399 if (alarms.length === 0) {
400 alarms = this.alarms.everyday;
402 this.ui.graphSchedule.setTimeRanges(this.periodsWeekToBoolArray());
403 this.ui.graphSchedule.pushTimeOfFlags(alarms);
404 this.ui.graphSchedule.showFlags();
408 * Callback function on visibility change;
410 ExercisePlanner.prototype.onVisibilityChange = function () {
412 switch (document.webkitVisibilityState) {
414 this.applicationStartTime = new Date();
415 this.currentAlarm = this.findCurrentAlarm();
416 if (this.currentAlarm.length > 0) {
417 this.ui.showWaitOk();
425 * Turn off all alarms today
427 ExercisePlanner.prototype.todayOffAll = function todayOffAll() {
428 // set begin date to tomorrow;
429 this.beginDate = new Date();
430 this.beginDate.setDate(this.beginDate.getDate() + 1);
432 this.generateAlarms();
436 // Initialize function
437 ExercisePlanner.prototype.init = function init() {
438 var onUiInitialize = function onUiInitialize() {
439 // register watcher on visibility change;
440 document.addEventListener('webkitvisibilitychange', this.onVisibilityChange.bind(this));
441 this.showNextAlarm();
442 this.onVisibilityChange();
445 this.selfId = tizen.application.getAppContext().appId;
450 this.config.currentTypeOfPeriods = (this.todayIsWorkday()) ? 'workday' : 'weekend';
452 this.generateAlarms();
453 this.ui.initialize(onUiInitialize);