This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget-task-handler.tsx
61 lines (55 loc) · 2.02 KB
/
widget-task-handler.tsx
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
import React from 'react'
import type { WidgetTaskHandlerProps } from 'react-native-android-widget'
import { GradesWidget } from './widgets/GradesWidget'
import { getGradebook, isWidgetDarkTheme } from './util/Widget'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { Appearance } from 'react-native'
const nameToWidget = {
Grades: GradesWidget
}
export async function widgetTaskHandler(props: WidgetTaskHandlerProps) {
const widgetInfo = props.widgetInfo
const Widget = nameToWidget[widgetInfo.widgetName as keyof typeof nameToWidget]
const dark = await isWidgetDarkTheme()
switch (props.widgetAction) {
case 'WIDGET_ADDED':
if (widgetInfo.widgetName === 'Grades') {
props.renderWidget(<Widget dark={dark} />)
try {
const gradebook = await getGradebook()
props.renderWidget(
<Widget gradebook={gradebook} dark={Appearance.getColorScheme() === 'dark'} />
)
} catch (e) {
props.renderWidget(<Widget error={e.message} dark={dark} />)
}
}
break
case 'WIDGET_RESIZED':
// Not needed for now
break
case 'WIDGET_CLICK':
if (props.clickAction === 'REFRESH') {
props.renderWidget(<Widget dark={dark} />)
try {
const gradebook = await getGradebook()
props.renderWidget(<Widget gradebook={gradebook} dark={dark} />)
} catch (e) {
props.renderWidget(<Widget error={e.message} dark={dark} />)
}
} else if (props.clickAction === 'TOGGLE_THEME') {
const newIsDark: boolean = !props.clickActionData.dark
props.renderWidget(<Widget dark={newIsDark} />)
try {
const gradebook = await getGradebook()
props.renderWidget(<Widget gradebook={gradebook} dark={newIsDark} />)
await AsyncStorage.setItem('WidgetThemeIsDark', `${newIsDark}`)
} catch (e) {
props.renderWidget(<Widget error={e.message} dark={newIsDark} />)
}
}
break
default:
break
}
}