Skip to content

Commit

Permalink
Calendar - Adding a year waiver storage in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoarthur0 committed May 15, 2020
1 parent 3b06e32 commit 9e3ce2f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
8 changes: 8 additions & 0 deletions js/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
36 changes: 26 additions & 10 deletions js/classes/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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 = '<b>Waived day: </b>' + waivedInfo['reason'];
var waivedLineHtmlCode =
'<tr'+ (isToday ? ' class="isToday"' : '') + ' id="' + trID + '">' +
Expand Down Expand Up @@ -374,6 +374,7 @@ class Calendar {
this.year += 1;

this.loadYearData(this.year);
this.loadYearWaiverData();
} else {
this.month += 1;
}
Expand All @@ -389,6 +390,7 @@ class Calendar {
this.year -= 1;

this.loadYearData(this.year);
this.loadYearWaiverData();
} else {
this.month -= 1;
}
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
3 changes: 1 addition & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
},
},
Expand Down

0 comments on commit 9e3ce2f

Please sign in to comment.