Skip to content

Commit

Permalink
Merge pull request #73 from kyoto-u/refactor
Browse files Browse the repository at this point in the history
Refactor Code
  • Loading branch information
das08 authored Jan 12, 2022
2 parents 89175f9 + 0369b28 commit 7bde37c
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 102 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Colors course site tabs according to the assignment due date.
Due date within 14 Days

## Notification Badge
Tells your unchecked latest assignments.
Tells your **unchecked** latest assignments.
Notification badge will appear in the upper left-hand side of a course site tab.

If you open a course site with the notification badge on, the badge will disappear.
Expand All @@ -50,8 +50,8 @@ You can add your custom assignment to miniSakai as `memo` with PLUS button locat
Also check box is available for you to distinguish completed assignments from working assignments.

## Cache
In order to reduce the network load on Sakai LMS, we have implemented a cache function for getting assignments and quizzes from REST API.
The default cache interval is as follows
In order to reduce the network load on Sakai LMS, we have implemented a cache function for fetching assignments and quizzes from REST API.
The default cache interval is as follows:
- Assignment fetching --- 2 minutes
- Quiz fetching --- 10 minutes

Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Comfortable Sakai",
"description": "__MSG_EXTENSION_DESCRIPTION__",
"version": "1.2.0",
"version": "1.2.1",
"manifest_version": 2,
"default_locale": "en",
"icons": {
Expand Down Expand Up @@ -36,7 +36,7 @@
"web_accessible_resources": [
"css/comfortable-sakai.css",
"img/logo.png",
"img/relaxPanda.png",
"img/noAssignment.png",
"img/miniSakaiBtn.png",
"views/minisakai.mustache"
]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "comfortable-sakai",
"version": "1.1.0",
"description": "Comfortable Sakai is a browser extension for managing assignments and quizzes on Sakai LMS.",
"description": "Comfortable Sakai is a Web browser extension for managing assignments and quizzes on Sakai LMS.",
"main": "index.ts",
"scripts": {
"test": "jest",
Expand Down
File renamed without changes
8 changes: 4 additions & 4 deletions public/views/minisakai.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</div>
<div class="cs-quiz-time">
<p class="cs-assignment-time-text">{{titles.quizFetchedTime}}</p>
<p class="cs-assignment-time-text">{{fetchedTime.assignment}}</p>
<p class="cs-assignment-time-text">{{fetchedTime.quiz}}</p>
</div>
{{/subset}}

Expand Down Expand Up @@ -213,12 +213,12 @@
</div>
{{/display.lateSubmit}}

{{#showRelaxPandA}}
{{#noAssignment}}
<div>
<p class="cs-noassignment-p">{{titles.relaxPandA}}</p>
<p class="cs-noassignment-p">{{titles.noAssignment}}</p>
<img alt="logo" class="cs-noassignment-img" src="{{img}}">
</div>
{{/showRelaxPandA}}
{{/noAssignment}}
</div>

{{^subset}}
Expand Down
43 changes: 13 additions & 30 deletions src/content_script.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loadFromLocalStorage, saveToLocalStorage } from "./storage";
import { Assignment, CourseSiteInfo } from "./model";
import { getCourseIDList, getBaseURL, getAssignmentByCourseID, getQuizFromCourseID } from "./network";
import { getCourseIDList, getAssignmentByCourseID, getQuizFromCourseID } from "./network";
import { createMiniSakaiBtn, createFavoritesBarNotification, displayMiniSakai } from "./minisakai";

import {
Expand All @@ -14,26 +14,20 @@ import {
updateIsReadFlag,
useCache,
} from "./utils";
import { Settings, loadSettings } from "./settings";
import { Config, loadConfigs } from "./settings";

export const baseURL = getBaseURL();
export const VERSION = chrome.runtime.getManifest().version;
export let assignmentCacheInterval: number;
export let quizCacheInterval: number;
export let assignmentFetchedTime: number | undefined;
export let quizFetchedTime: number | undefined;
export let courseIDList: Array<CourseSiteInfo>;
export let mergedAssignmentList: Array<Assignment>;
export let mergedAssignmentListNoMemo: Array<Assignment>;
export let CPsettings: Settings;

/**
* Load old assignments/quizzes from storage and fetch new assignments/quizzes from Sakai.
* @param {Config} config
* @param {CourseSiteInfo[]} courseSiteInfos
* @param {boolean} useAssignmentCache
* @param {boolean} useQuizCache
*/
export async function loadAndMergeAssignmentList(courseSiteInfos: Array<CourseSiteInfo>, useAssignmentCache: boolean, useQuizCache: boolean): Promise<Array<Assignment>> {
export async function loadAndMergeAssignmentList(config: Config ,courseSiteInfos: Array<CourseSiteInfo>, useAssignmentCache: boolean, useQuizCache: boolean): Promise<Array<Assignment>> {
// Load old assignments and quizzes from local storage
const oldAssignmentList = convertArrayToAssignment(await loadFromLocalStorage("CS_AssignmentList"));
const oldQuizList = convertArrayToAssignment(await loadFromLocalStorage("CS_QuizList"));
Expand All @@ -48,7 +42,7 @@ export async function loadAndMergeAssignmentList(courseSiteInfos: Array<CourseSi
const pendingList = [];
// Add course sites to fetch-pending list
for (const i of courseSiteInfos) {
pendingList.push(getAssignmentByCourseID(baseURL, i.courseID));
pendingList.push(getAssignmentByCourseID(config.baseURL, i.courseID));
}
// Wait until all assignments are fetched
const result = await (Promise as any).allSettled(pendingList);
Expand All @@ -57,7 +51,7 @@ export async function loadAndMergeAssignmentList(courseSiteInfos: Array<CourseSi
}
// Update assignment fetch timestamp
await saveToLocalStorage("CS_AssignmentFetchTime", nowTime);
assignmentFetchedTime = nowTime;
config.fetchedTime.assignment = nowTime;
}
// Compare assignments with old ones
mergedAssignmentListNoMemo = compareAndMergeAssignmentList(oldAssignmentList, newAssignmentList);
Expand All @@ -73,7 +67,7 @@ export async function loadAndMergeAssignmentList(courseSiteInfos: Array<CourseSi
const pendingList = [];
// Add course sites to fetch-pending list
for (const i of courseSiteInfos) {
pendingList.push(getQuizFromCourseID(baseURL, i.courseID));
pendingList.push(getQuizFromCourseID(config.baseURL, i.courseID));
}
// Wait until all quizzes are fetched
const result = await (Promise as any).allSettled(pendingList);
Expand All @@ -82,7 +76,7 @@ export async function loadAndMergeAssignmentList(courseSiteInfos: Array<CourseSi
}
// Update assignment fetch timestamp
await saveToLocalStorage("CS_QuizFetchTime", nowTime);
quizFetchedTime = nowTime;
config.fetchedTime.quiz = nowTime;
}
// Compare quizzes with old ones
const mergedQuizList = compareAndMergeAssignmentList(oldQuizList, newQuizList);
Expand All @@ -102,18 +96,6 @@ export async function loadAndMergeAssignmentList(courseSiteInfos: Array<CourseSi
return mergedAssignmentList;
}

/**
* Load configurations from local storage
*/
async function loadConfigs() {
CPsettings = await loadSettings();
assignmentCacheInterval = CPsettings.getAssignmentCacheInterval;
quizCacheInterval = CPsettings.getQuizCacheInterval;
CPsettings.displayCheckedAssignment = CPsettings.getDisplayCheckedAssignment;
assignmentFetchedTime = await loadFromLocalStorage("CS_AssignmentFetchTime", "undefined");
quizFetchedTime = await loadFromLocalStorage("CS_QuizFetchTime", "undefined");
}

/**
* Load course site IDs
*/
Expand All @@ -125,16 +107,17 @@ async function loadCourseIDList() {
async function main() {
if (isLoggedIn()) {
createMiniSakaiBtn();
await loadConfigs();
const config = await loadConfigs();
await loadCourseIDList();
mergedAssignmentList = await loadAndMergeAssignmentList(
config,
courseIDList,
useCache(assignmentFetchedTime, assignmentCacheInterval),
useCache(quizFetchedTime, quizCacheInterval)
useCache(config.fetchedTime.assignment, config.cacheInterval.assignment),
useCache(config.fetchedTime.quiz, config.cacheInterval.quiz)
);
// await addBookmarkedCourseSites(baseURL);
await displayMiniSakai(mergedAssignmentList, courseIDList);
createFavoritesBarNotification(courseIDList, mergedAssignmentList);
await createFavoritesBarNotification(courseIDList, mergedAssignmentList);

miniSakaiReady();
updateIsReadFlag(mergedAssignmentListNoMemo);
Expand Down
23 changes: 13 additions & 10 deletions src/eventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { assignmentDiv, miniSakai } from "./dom";
import { loadFromLocalStorage, saveToLocalStorage } from "./storage";
import { CourseSiteInfo, Assignment, AssignmentEntry } from "./model";
import { convertArrayToAssignment, genUniqueID, mergeIntoAssignmentList } from "./utils";
import { CPsettings, courseIDList, loadAndMergeAssignmentList, mergedAssignmentListNoMemo } from "./content_script";
import { DefaultSettings } from "./settings";
import { courseIDList, loadAndMergeAssignmentList, mergedAssignmentListNoMemo } from "./content_script";
import { DefaultSettings, loadConfigs } from "./settings";
import { createFavoritesBarNotification, deleteFavoritesBarNotification, displayMiniSakai } from "./minisakai";

let toggle = false;
Expand Down Expand Up @@ -125,15 +125,17 @@ async function toggleFinishedFlag(event: any): Promise<void> {
*/
async function updateSettings(event: any, type: string): Promise<void> {
const settingsID = event.target.id;
let settingsValue = event.currentTarget.value;
let settingsValue = event.target.value;
const config = await loadConfigs();
const CSsettings = config.CSsettings;

// Type of Settings
switch (type) {
case "check":
settingsValue = event.currentTarget.checked;
settingsValue = event.target.checked;
break;
case "number":
settingsValue = parseInt(event.currentTarget.value);
settingsValue = parseInt(event.target.value);
break;
case "string":
break;
Expand All @@ -146,7 +148,7 @@ async function updateSettings(event: any, type: string): Promise<void> {
];
for (const k of colorList) {
// @ts-ignore
CPsettings[k] = DefaultSettings[k];
CSsettings[k] = DefaultSettings[k];
const q = <HTMLInputElement>document.getElementById(k);
if (q) {
// @ts-ignore
Expand All @@ -155,10 +157,10 @@ async function updateSettings(event: any, type: string): Promise<void> {
}
} else {
// @ts-ignore
CPsettings[settingsID] = settingsValue;
CSsettings[settingsID] = settingsValue;
}

saveToLocalStorage("CS_Settings", CPsettings);
await saveToLocalStorage("CS_Settings", CSsettings);

await redrawFavoritesBar(courseIDList, true);
}
Expand Down Expand Up @@ -253,8 +255,9 @@ async function editFavoritesMessage(): Promise<void> {
*/
async function redrawFavoritesBar(courseIDList: Array<CourseSiteInfo>, useCache: boolean): Promise<void> {
deleteFavoritesBarNotification();
const newAssignmentList = await loadAndMergeAssignmentList(courseIDList, useCache, useCache);
createFavoritesBarNotification(courseIDList, newAssignmentList);
const config = await loadConfigs();
const newAssignmentList = await loadAndMergeAssignmentList(config, courseIDList, useCache, useCache);
await createFavoritesBarNotification(courseIDList, newAssignmentList);
}

/**
Expand Down
Loading

0 comments on commit 7bde37c

Please sign in to comment.