-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidget-task-handler.tsx
More file actions
138 lines (120 loc) · 3.88 KB
/
widget-task-handler.tsx
File metadata and controls
138 lines (120 loc) · 3.88 KB
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
import AsyncStorage from "@react-native-async-storage/async-storage";
import React from "react";
import { NativeModules } from "react-native";
import type { WidgetTaskHandlerProps } from "react-native-android-widget";
import { FlexWidget, TextWidget } from "react-native-android-widget";
import { widgetTaskHandler } from "./widgets/QuickCaptureTask";
import { QuickCaptureWidget } from "./widgets/QuickCaptureWidget";
import { getWidgetTemplate } from "./widgets/WidgetConfigurationScreen";
const { SharedStorage } = NativeModules;
const QUICK_CAPTURE_KEY = "__quick_capture__";
const AUTH_STORAGE_KEY = "mova_auth";
const nameToWidget = {
QuickCaptureWidget: QuickCaptureWidget,
};
function ErrorWidget({ message }: { message: string }) {
return (
<FlexWidget
style={{
height: "match_parent",
width: "match_parent",
backgroundColor: "#FF0000",
justifyContent: "center",
alignItems: "center",
padding: 8,
}}
>
<TextWidget text={message} style={{ fontSize: 10, color: "#FFFFFF" }} />
</FlexWidget>
);
}
async function getTemplateName(widgetId: number): Promise<string> {
try {
// First try to read template name directly from SharedPreferences
// (saved by native TemplateConfigActivity)
if (SharedStorage) {
const templateName = await SharedStorage.getItem(
`widget_${widgetId}_template_name`,
);
if (templateName) {
return templateName;
}
}
// Fall back to getting template key and looking up name
const templateKey = await getWidgetTemplate(widgetId);
if (templateKey === QUICK_CAPTURE_KEY) {
return "Quick Capture";
}
// Try to get template name from API
const authData = await AsyncStorage.getItem(AUTH_STORAGE_KEY);
if (authData) {
const { apiUrl, username, password } = JSON.parse(authData);
const response = await fetch(`${apiUrl}/capture-templates`, {
headers: {
Authorization: `Basic ${btoa(`${username}:${password}`)}`,
},
});
if (response.ok) {
const templates = await response.json();
if (templates[templateKey]) {
return templates[templateKey].name;
}
}
}
return "Capture";
} catch {
return "Quick Capture";
}
}
export async function widgetTaskHandlerEntry(props: WidgetTaskHandlerProps) {
const { widgetInfo, widgetAction, clickAction, renderWidget } = props;
console.log("[Widget] widgetTaskHandlerEntry:", {
widgetName: widgetInfo.widgetName,
widgetAction,
clickAction,
});
try {
const Widget =
nameToWidget[widgetInfo.widgetName as keyof typeof nameToWidget];
if (!Widget) {
console.log("[Widget] Unknown widget:", widgetInfo.widgetName);
renderWidget(
<ErrorWidget message={`Unknown: ${widgetInfo.widgetName}`} />,
);
return;
}
// Get the template name for this widget
const templateName = await getTemplateName(widgetInfo.widgetId);
// Handle click actions
if (clickAction) {
const result = await widgetTaskHandler(props);
console.log("[Widget] Task result:", result);
const status =
result.status === "success"
? "success"
: result.status === "queued"
? "offline"
: result.status === "no_auth"
? "error"
: "idle";
renderWidget(
<Widget
status={status}
widgetId={widgetInfo.widgetId}
templateName={templateName}
/>,
);
return;
}
// Default render
renderWidget(
<Widget widgetId={widgetInfo.widgetId} templateName={templateName} />,
);
} catch (error) {
console.error("[Widget] Error:", error);
const errorMessage = error instanceof Error ? error.message : String(error);
renderWidget(
<ErrorWidget message={`Error: ${errorMessage.substring(0, 50)}`} />,
);
}
}