From fdbe718c127242d4b524ec15b87c9fd3c6f13593 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Mon, 24 Jul 2023 17:31:58 +0800 Subject: Display announcements in same order as course list --- contents/ui/main.qml | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/contents/ui/main.qml b/contents/ui/main.qml index 0c4d57f..e685044 100644 --- a/contents/ui/main.qml +++ b/contents/ui/main.qml @@ -53,27 +53,40 @@ Item { return {id: line.slice(0, spaceIndex), code: line.slice(spaceIndex + 1)} } ) + let courseIndices = {} // reverse look-up table to sort by courses + for (let i = 0; i < courses.length; i++) { + courseIndices[courses[i].id] = i + } const showSubmittedAssignments = plasmoid.configuration.showSubmittedAssignments // we need user id to check submission status callApi("/users/self", 0, user => { - syncCourses(courses, showSubmittedAssignments, user.id) + syncCourses(courses, courseIndices, showSubmittedAssignments, user.id) }) } - function syncCourses(courses, showSubmittedAssignments, userId) { + // fetch asynchronously, but display in this order: + // important -> normal -> finished + // when an activity is both important and finished, important takes priority + function syncCourses(courses, courseIndices, showSubmittedAssignments, userId) { announcementsModel.clear() assignmentsModel.clear() - let importantCount = {announcements: 0, assignments: 0} + let announcementIndices = { + important: 0, // actually constant, kept for symmetry + normal: 0, + finished: 0, + } for (let course of courses) { + const courseIdx = courseIndices[course.id] callApi(`/announcements?context_codes[]=course_${course.id}`, 50, announcements => { announcements.forEach(announcement => { const info = { type: "announcement", activityId: announcement.id, + courseId: course.id, course: course.code, title: announcement.title, url: announcement.html_url, @@ -81,11 +94,32 @@ Item { finished: plasmoid.configuration.finishedAnnouncements.includes(announcement.id.toString()), } + // figure out where we insert it into list + let idx = 0; + let endIdx = 0; // actually past the end if (info.important) { - announcementsModel.insert(importantCount.announcements++, info) // place above unimportant ones + idx = announcementIndices.important + endIdx = announcementIndices.normal + announcementIndices.normal++ + announcementIndices.finished++ + } else if (!info.finished) { + idx = announcementIndices.normal + endIdx = announcementIndices.finished + announcementIndices.finished++ } else { - announcementsModel.append(info) + idx = announcementIndices.normal + endIdx = announcementsModel.count + } + + for (; idx < endIdx; idx++) { + const annc = announcementsModel.get(idx) + if (courseIndices[course.id] < courseIndices[annc.courseId]) { + // we are just past the end of this course + // insert this announcement here + break + } } + announcementsModel.insert(idx, info) }) }) @@ -95,6 +129,7 @@ Item { const info = { type: "assignment", activityId: assignment.id, + courseId: course.id, course: course.code, title: assignment.name, dueAt: assignment.due_at || "", // if null, use empty string to suppress errors -- cgit v1.2.3