From 9e3ce2f9eb7b3366956c5afa9ffd9c1b90c56e1a Mon Sep 17 00:00:00 2001 From: Arthur Araujo Date: Thu, 14 May 2020 22:09:11 -0300 Subject: [PATCH] Calendar - Adding a year waiver storage in memory --- js/calendar.js | 8 ++++++++ js/classes/Calendar.js | 36 ++++++++++++++++++++++++++---------- main.js | 3 +-- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/js/calendar.js b/js/calendar.js index 7c6ea0050..a739c9bef 100644 --- a/js/calendar.js +++ b/js/calendar.js @@ -25,6 +25,14 @@ ipcRenderer.on('PREFERENCE_SAVED', function(event, inputs) { applyTheme(preferences.theme); }); +/* + * Get nofified when preferences has been updated. + */ +ipcRenderer.on('WAIVER_SAVED', function() { + calendar.loadYearWaiverData(); + calendar.redraw(); +}); + /* * Returns true if the notification is enabled in preferences. */ diff --git a/js/classes/Calendar.js b/js/classes/Calendar.js index 0a1cacc05..f7590ef82 100644 --- a/js/classes/Calendar.js +++ b/js/classes/Calendar.js @@ -37,6 +37,7 @@ class Calendar { this.year = this.today.getFullYear(); this.workingDays = 0; this.loadYearData(this.year); + this.loadYearWaiverData(); this.updatePreferences(preferences); this._initCalendar(); } @@ -209,8 +210,7 @@ class Calendar { weekDay = currentDay.getDay(), today = new Date(), isToday = (today.getDate() === day && today.getMonth() === month && today.getFullYear() === year), - trID = ('tr-' + year + '-' + month + '-' + day), - dateStr = getDateStr(currentDay); + trID = ('tr-' + year + '-' + month + '-' + day); if (!this.showDay(year, month, day)) { if (!this.hideNonWorkingDays) { @@ -224,8 +224,8 @@ class Calendar { } } - if (waivedWorkdays.has(dateStr)) { - var waivedInfo = waivedWorkdays.get(dateStr); + var waivedInfo = this.internalWaiverStore[month][day]; + if (waivedInfo !== undefined) { var summaryStr = 'Waived day: ' + waivedInfo['reason']; var waivedLineHtmlCode = '' + @@ -374,6 +374,7 @@ class Calendar { this.year += 1; this.loadYearData(this.year); + this.loadYearWaiverData(); } else { this.month += 1; } @@ -389,6 +390,7 @@ class Calendar { this.year -= 1; this.loadYearData(this.year); + this.loadYearWaiverData(); } else { this.month -= 1; } @@ -460,6 +462,23 @@ class Calendar { } } + /** + * Stores waiver data in memory to make operations faster + */ + loadYearWaiverData() { + this.internalWaiverStore = {}; + + for (let month = 0; month < 12; ++month) { + let monthLength = getMonthLength(this.year, month); + this.internalWaiverStore[month] = {}; + + for (let day = 1; day <= monthLength; ++day) { + let dayKey = getDateStr(new Date(this.year, month, day)); + this.internalWaiverStore[month][day] = waivedWorkdays.get(dayKey, undefined); + } + } + } + /* * Calls showDay from user-preferences.js passing the last preferences set. */ @@ -560,14 +579,11 @@ class Calendar { continue; } - var currentDay = new Date(this.year, this.month, day), - dateStr = getDateStr(currentDay); - var dayTotal = null; var dayStr = this.year + '-' + this.month + '-' + day + '-'; - if (waivedWorkdays.has(dateStr)) { - var waivedInfo = waivedWorkdays.get(dateStr); + var waivedInfo = this.internalWaiverStore[this.month][day]; + if (waivedInfo !== undefined) { var waivedDayTotal = waivedInfo['hours']; $('#' + dayStr + 'day-total').val(waivedDayTotal); dayTotal = waivedDayTotal; @@ -620,7 +636,7 @@ class Calendar { if (!this.showDay(this.today.getFullYear(), this.today.getMonth(), this.today.getDate()) || this.today.getMonth() !== this.getMonth() || this.today.getFullYear() !== this.getYear() || - waivedWorkdays.has(getDateStr(this.today))) { + this.internalWaiverStore[this.month][this.today.getDate()]) { return; } var [dayBegin, lunchBegin, lunchEnd, dayEnd] = this.getDaysEntries(this.today.getMonth(), this.today.getDate()); diff --git a/main.js b/main.js index b187f016d..201ec842a 100644 --- a/main.js +++ b/main.js @@ -141,8 +141,7 @@ function createWindow() { waiverWindow.show(); waiverWindow.on('close', function() { waiverWindow = null; - // Reload only the calendar itself to avoid a flash - win.webContents.executeJavaScript('calendar.redraw()'); + win.webContents.send('WAIVER_SAVED'); }); }, },