summaryrefslogtreecommitdiff
path: root/contents/ui/ActivityView.qml
blob: f35f89d41bfe85526d025fb96fe67685778d4917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.plasma.components 3.0 as PlasmaComponents3
import org.kde.plasma.extras 2.0 as PlasmaExtras

RowLayout {
    id: activityView
    width: parent.width
    Layout.fillWidth: true

    function sameDay(due, now) {
        return (
            due.getFullYear() == now.getFullYear() &&
            due.getMonth() == now.getMonth() &&
            due.getDate() == now.getDate()
        )
    }

    function dayDiff(due, now, stopAt) {
        // due and now are both Date objects
        // if due has the same day as now (i.e. due is today), return 0
        // if due is tomorrow, return 1, the day after that 2, yesterday -1, etc.
        // when the absolute value of diff reaches stopAt, return stopAt
        let diff = 0
        const step = (due > now) ? 1 : -1
        while (!sameDay(due, now)) {
            now.setDate(now.getDate() + step)
            diff += step
            if (Math.abs(diff) >= stopAt) {
                return stopAt
            }
        }
        return diff
    }

    function humanDue(isoDue) {
        // converts ISO 8601 datetime string to relative time, e.g. tomorrow midnight
        const daysOfWeek = [
            i18n("Sunday"),
            i18n("Monday"),
            i18n("Tuesday"),
            i18n("Wednesday"),
            i18n("Thursday"),
            i18n("Friday"),
            i18n("Saturday"),
        ]

        try {
            let due = new Date(isoDue)
            if (due.toString() == "Invalid Date") {
                console.warn(`Cannot parse due date: ${isoDue}`)
                return isoDue
            }

            let now = new Date()
            const pastDue = (due <= now)
            const dayDifference = dayDiff(due, now, 8)
            let humanDueString = ""

            if (dayDifference <= -2) {
                humanDateString = Qt.formatDate(due)
            } else if (dayDifference == -1) {
                humanDueString = i18n("Yesterday")
            } else if (dayDifference == 0) {
                humanDueString = i18n("Today")
            } else if (dayDifference == 1) {
                humanDueString = i18n("Tomorrow")
            } else if (dayDifference <= 6) {
                humanDueString = daysOfWeek[due.getDay()]
            } else {
                humanDueString = Qt.formatDate(due)
            }

            humanDueString += " " + Qt.formatTime(due)

            if (pastDue) {
                humanDueString += " " + i18n("(missed)")
            }
            return humanDueString
        } catch (e) {
            return isoDue // screw it
        }
    }

    RowLayout {
        Layout.fillWidth: true
        PlasmaComponents3.CheckBox {
            id: activityCheckbox
            onToggled: () => {
                activityLabel.font.strikeout = (checkState == Qt.Checked)
            }
        }

        ColumnLayout {
            PlasmaComponents3.Label {
                id: activityLabel
                text: `[${course}] ${title}`
                font.bold: important
                color: important ? PlasmaCore.Theme.negativeTextColor : PlasmaCore.Theme.textColor
                elide: Text.ElideRight
                Layout.fillWidth: true

            }

            PlasmaComponents3.Label {
                id: dueLabel
                visible: type == "assignment"
                text: dueAt ? ("Due: " + humanDue(dueAt)) : ""
                color: activityLabel.color
                Layout.fillWidth: true
            }

            MouseArea {
                anchors.fill: parent
                onClicked: () => {
                    activityCheckbox.toggle()  // toggle checkbox
                    activityCheckbox.toggled() // and trigger the onToggled effects
                }
            }
        }

    }

    RowLayout {
        Layout.alignment: Qt.AlignRight

        PlasmaComponents3.ToolButton {
            icon.name: "view-visible"
            onClicked: () => {
                Qt.openUrlExternally(url)
            }
        }

        PlasmaComponents3.ToolButton {
            icon.name: "emblem-important-symbolic"
            checked: important

            onClicked: () => {
                important = !important
                activityLabel.font.bold = important
                activityLabel.color = important ? PlasmaCore.Theme.negativeTextColor : PlasmaCore.Theme.textColor

                const configKeys = {
                    announcement: "importantAnnouncements",
                    assignment: "importantAssignments",
                }

                let importantActivities = plasmoid.configuration[configKeys[type]]
                if (important) {
                    if (!importantActivities.includes(activityId)) {
                        importantActivities.push(activityId)
                    }
                } else {
                    // remove activityId from list
                    importantActivities.splice(importantActivities.indexOf(activityId), 1)
                }
                // save config
                plasmoid.configuration[configKeys[type]] = importantActivities
            }
        }
    }
}