Background event refresh

This commit is contained in:
Ian Gulliver
2020-09-10 21:40:55 +00:00
parent d03d5741a3
commit 89fa672172
2 changed files with 36 additions and 24 deletions

View File

@@ -1,8 +1,10 @@
let events = [];
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
function findPrevious(events) {
function findPrevious() {
const now = Date.now();
let max = null;
let maxTime = 0;
@@ -18,7 +20,7 @@ function findPrevious(events) {
return max;
}
function findCurrent(events) {
function findCurrent() {
const now = Date.now();
for (const event of events) {
const start = new Date(event.start.dateTime).getTime();
@@ -31,8 +33,8 @@ function findCurrent(events) {
return null;
}
function anyRemaining(events) {
const current = findCurrent(events);
function anyRemaining() {
const current = findCurrent();
const currentEnd = current ? new Date(current.end.dateTime).getTime() : null;
const now = Date.now();
@@ -47,8 +49,8 @@ function anyRemaining(events) {
return false;
}
function findNext(events) {
const current = findCurrent(events);
function findNext() {
const current = findCurrent();
const currentEnd = current ? new Date(current.end.dateTime).getTime() : null;
const now = Date.now();
@@ -107,16 +109,16 @@ function timezoneOffset() {
return `${offset > 0 ? '-' : '+'}${Math.floor(offset / 60).toString().padStart(2, '0')}:${(offset % 60).toString().padStart(2, '0')}`;
}
function render(events) {
function render() {
const curElem = document.getElementById("current");
const curName = document.getElementById("currentName");
const curLeft = document.getElementById("currentLeft");
const nextElem = document.getElementById("next");
const nextName = document.getElementById("nextName");
const current = findCurrent(events);
const prev = findPrevious(events);
const next = findNext(events);
const current = findCurrent();
const prev = findPrevious();
const next = findNext();
if (current) {
// Currently in an event
@@ -154,7 +156,7 @@ function render(events) {
} else if (current) {
nextElem.classList.remove("event");
nextElem.classList.add("break");
if (anyRemaining(events)) {
if (anyRemaining()) {
setText(nextName, "😎 Break");
} else {
setText(nextName, "✅ Done!");
@@ -166,6 +168,20 @@ function render(events) {
}
}
function loadEvents() {
const tz = timezoneOffset();
const date = new Date().toISOString().substring(0,10);
return gapi.client.calendar.events.list({
'calendarId': 'primary',
'timeMin': `${date}T00:00:00${tz}`,
'timeMax': `${date}T23:59:59${tz}`,
'showDeleted': false,
'singleEvents': true,
'maxResults': 250,
'orderBy': 'startTime',
});
}
function initClient() {
gapi.client.init({
apiKey: 'AIzaSyDzNMBbLqQoSCMiug_7UbUrgaAvnoyzYYU',
@@ -175,23 +191,19 @@ function initClient() {
ux_mode: 'redirect',
}).then(() => {
if (gapi.auth2.getAuthInstance().isSignedIn.get()) {
const tz = timezoneOffset();
const date = new Date().toISOString().substring(0,10);
return gapi.client.calendar.events.list({
'calendarId': 'primary',
'timeMin': `${date}T00:00:00${tz}`,
'timeMax': `${date}T23:59:59${tz}`,
'showDeleted': false,
'singleEvents': true,
'maxResults': 250,
'orderBy': 'startTime',
});
return loadEvents();
} else {
gapi.auth2.getAuthInstance().signIn();
return;
}
}).then((response) => {
setInterval(() => render(response.result.items), 250);
events = response.result.items;
setInterval(() => render(), 250);
setInterval(() => {
loadEvents().then((response) => {
events = response.result.items;
});
}, 30 * 60 * 1000);
});
}

View File

@@ -3,7 +3,7 @@
<head>
<title>EasyCal</title>
<meta charset="utf-8">
<meta http-equiv="refresh" content="3603">
<meta http-equiv="refresh" content="50000">
<link rel="stylesheet" href="easycal.css">
</head>
<body>