jrshikoku/components/AndroidWidget/widget-task-handler.jsx

79 lines
2.4 KiB
JavaScript

import React from "react";
import { TraInfoEXWidget } from "./TraInfoEXWidget";
import dayjs from "dayjs";
import { ToastAndroid } from "react-native";
import { InfoWidget } from "./InfoWidget";
import { AS } from "../../storageControl";
export const nameToWidget = {
JR_shikoku_train_info: TraInfoEXWidget,
Info_Widget: InfoWidget,
};
export const getInfoString = async () => {
// Fetch data from the server
const time = dayjs().format("HH:mm");
const text = await fetch(
"https://script.google.com/macros/s/AKfycbz80LcaEUrhnlEsLkJy0LG2IRO3DBVQhfNmN1d_0f_HvtsujNQpxM90SrV9yKWH_JG1Ww/exec"
)
.then((response) => response.text())
.then((data) => {
if (data !== "") {
return data.split("^");
}
return null;
});
ToastAndroid.show(`${text}`, ToastAndroid.SHORT);
return { time, text };
};
export const getDelayData = async () => {
// Fetch data from the server
const time = dayjs().format("HH:mm");
const delayString = await fetch(
"https://script.google.com/macros/s/AKfycbyKxch7z7l8e07LXulRHqxjVoIiB13kcgvoToLE-rqlxLmLSKdlmqz0FI1F2EuA7Zfg/exec"
)
.then((response) => response.text())
.then((data) => {
if (data !== "") {
return data.split("^");
}
return null;
});
ToastAndroid.show(`${delayString}`, ToastAndroid.SHORT);
return { time, delayString };
};
export async function widgetTaskHandler(props) {
const WidgetName = await AS.getItem(
`widgetType/${props.widgetInfo.widgetId}`
).catch((e) => "JR_shikoku_train_info");
ToastAndroid.show(
`Widget Action: ${JSON.stringify(props.widgetInfo.widgetId)}`,
ToastAndroid.SHORT
);
ToastAndroid.show(`Widget Name: ${WidgetName}`, ToastAndroid.SHORT);
switch (props.widgetAction) {
case "WIDGET_ADDED":
case "WIDGET_UPDATE":
case "WIDGET_CLICK":
case "WIDGET_RESIZED":
if (WidgetName === "JR_shikoku_train_info") {
const { time, delayString } = await getDelayData();
props.renderWidget(
<TraInfoEXWidget time={time} delayString={delayString} />
);
} else if (WidgetName === "Info_Widget") {
const { time, text } = await getInfoString();
props.renderWidget(<InfoWidget time={time} delayString={text} />);
}
break;
case "WIDGET_DELETED":
AS.removeItem(`widgetType/${props.widgetInfo.widgetId}`);
break;
default:
break;
}
}