mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-06 03:34:40 +00:00
refactor: clean up CheckCurrentLesson function
This commit is contained in:
+38
-84
@@ -1447,99 +1447,53 @@ function GetLightDarkModeString(darkmodetoggle: boolean) {
|
|||||||
return tooltipstring
|
return tooltipstring
|
||||||
}
|
}
|
||||||
|
|
||||||
function CheckCurrentLesson(lesson: any, num: number) {
|
async function CheckCurrentLesson(lesson: any, num: number) {
|
||||||
var startTime = lesson.from
|
const { from: startTime, until: endTime, code, description, room, staff } = lesson;
|
||||||
var endTime = lesson.until
|
const currentDate = new Date();
|
||||||
// Gets current time
|
|
||||||
let currentDate = new Date()
|
|
||||||
|
|
||||||
// Takes start time of current lesson and makes it into a Date function for comparison
|
// Create Date objects for start and end times
|
||||||
let startDate = new Date(currentDate.getTime())
|
const [startHour, startMinute] = startTime.split(':').map(Number);
|
||||||
startDate.setHours(startTime.split(':')[0])
|
const [endHour, endMinute] = endTime.split(':').map(Number);
|
||||||
startDate.setMinutes(startTime.split(':')[1])
|
|
||||||
startDate.setSeconds(parseInt('00'))
|
|
||||||
|
|
||||||
// Takes end time of current lesson and makes it into a Date function for comparison
|
const startDate = new Date(currentDate);
|
||||||
let endDate = new Date(currentDate.getTime())
|
startDate.setHours(startHour, startMinute, 0);
|
||||||
endDate.setHours(endTime.split(':')[0])
|
|
||||||
endDate.setMinutes(endTime.split(':')[1])
|
|
||||||
endDate.setSeconds(parseInt('00'))
|
|
||||||
|
|
||||||
// Gets the difference between the start time and current time
|
const endDate = new Date(currentDate);
|
||||||
var difference = startDate.getTime() - currentDate.getTime()
|
endDate.setHours(endHour, endMinute, 0);
|
||||||
// Converts the difference into minutes
|
|
||||||
var minutes = Math.floor(difference / 1000 / 60)
|
|
||||||
|
|
||||||
// Checks if current time is between the start time and end time of current tested lesson
|
// Check if the current time is within the lesson time range
|
||||||
let valid = startDate < currentDate && endDate > currentDate
|
const isValidTime = startDate < currentDate && endDate > currentDate;
|
||||||
|
|
||||||
let id = lesson.code + num
|
const elementId = `${code}${num}`;
|
||||||
const date = new Date()
|
const element = document.getElementById(elementId);
|
||||||
|
|
||||||
var elementA = document.getElementById(id)
|
if (!element) {
|
||||||
if (!elementA) {
|
clearInterval(LessonInterval);
|
||||||
clearInterval(LessonInterval)
|
return;
|
||||||
} else {
|
}
|
||||||
if (
|
|
||||||
currentSelectedDate.toLocaleDateString('en-au') ==
|
const isCurrentDate = currentSelectedDate.toLocaleDateString('en-au') === currentDate.toLocaleDateString('en-au');
|
||||||
date.toLocaleDateString('en-au')
|
|
||||||
) {
|
if (isCurrentDate) {
|
||||||
if (valid) {
|
if (isValidTime) {
|
||||||
// Apply the activelesson class to increase the box-shadow of current lesson
|
element.classList.add('activelesson');
|
||||||
elementA.classList.add('activelesson')
|
} else {
|
||||||
} else {
|
element.classList.remove('activelesson');
|
||||||
// Removes the activelesson class to ensure only the active lesson have the class
|
|
||||||
if (elementA != null) {
|
|
||||||
elementA.classList.remove('activelesson')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If 5 minutes before the start of another lesson:
|
const minutesUntilStart = Math.floor((startDate.getTime() - currentDate.getTime()) / 60000);
|
||||||
if (minutes == 5) {
|
|
||||||
const result = browser.storage.local.get('lessonalert')
|
if (minutesUntilStart !== 5 || settingsState.lessonalert || !window.Notification) return;
|
||||||
function open (result: any) {
|
|
||||||
if (result.lessonalert) {
|
if (Notification.permission !== 'granted') await Notification.requestPermission();
|
||||||
// Checks if notifications are supported
|
|
||||||
if (!window.Notification) {
|
try {
|
||||||
console.log('Browser does not support notifications.')
|
new Notification('Next Lesson in 5 Minutes:', {
|
||||||
} else {
|
body: `Subject: ${description}${room ? `\nRoom: ${room}` : ''}${staff ? `\nTeacher: ${staff}` : ''}`,
|
||||||
// check if permission is already granted
|
});
|
||||||
if (Notification.permission === 'granted') {
|
} catch (error) {
|
||||||
new Notification('Next Lesson in 5 Minutes:', {
|
console.error(error);
|
||||||
body:
|
|
||||||
`Subject: ${lesson.description}` +
|
|
||||||
(lesson.room ? `\nRoom: ${lesson.room}` : '') +
|
|
||||||
(lesson.staff ? `\nTeacher: ${lesson.staff}` : ''),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
// request permission from user
|
|
||||||
Notification.requestPermission()
|
|
||||||
.then(function (p) {
|
|
||||||
if (p === 'granted') {
|
|
||||||
// show notification here
|
|
||||||
new Notification('Next Lesson in 5 Minutes:', {
|
|
||||||
body:
|
|
||||||
'Subject: ' +
|
|
||||||
lesson.description +
|
|
||||||
' \nRoom: ' +
|
|
||||||
lesson.room +
|
|
||||||
' \nTeacher: ' +
|
|
||||||
lesson.staff,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
console.log('User blocked notifications.')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(function (err) {
|
|
||||||
console.error(err)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result.then(open, onError)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user