Files
jrshikoku/components/AndroidWidget/TraInfoEXWidget.tsx

121 lines
3.4 KiB
TypeScript

import React from "react";
import {
FlexWidget,
TextWidget,
ListWidget,
} from "react-native-android-widget";
import dayjs from "dayjs";
import { WidgetColors, widgetLightColors } from "./widget-theme";
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/AKfycbw-0RDLAu8EQAEWA860tk4KVW6VOr3iIU900AcWEfqIP16gtNUG1XO_A3oBfAGiNeCf/exec"
)
.then((response) => response.text())
.then((data) => {
if (data !== "") {
return data.split("^");
}
return null;
});
//ToastAndroid.show(`${delayString}`, ToastAndroid.SHORT);
return { time, delayString };
};
export function TraInfoEXWidget({ time, delayString, colors = widgetLightColors }: { time: string; delayString: string[] | null; colors?: WidgetColors }) {
return (
<FlexWidget
style={{
height: "match_parent",
width: "match_parent",
justifyContent: "center",
alignItems: "center",
backgroundColor: colors.background,
borderRadius: 16,
}}
clickAction="OPEN_URI"
clickActionData={{ uri: "jrshikoku://open/traininfo" }}
>
<FlexWidget
style={{
justifyContent: "center",
alignItems: "center",
backgroundColor: colors.headerBg,
width: "100%" as any,
flexDirection: "row",
paddingTop: 10,
paddingBottom: 10,
}}
>
<TextWidget
text={"列車遅延速報EX"}
style={{
fontSize: 30,
fontWeight: "bold",
fontFamily: "Inter",
color: colors.headerText,
textAlign: "left",
marginLeft: 10,
}}
/>
<FlexWidget style={{ flex: 1 }} />
<TextWidget
text={time}
style={{
fontSize: 30,
fontFamily: "Inter",
color: colors.headerText,
textAlign: "right",
marginRight: 10,
}}
/>
</FlexWidget>
<ListWidget
style={{
flex: 1,
backgroundColor: colors.background,
width: "match_parent",
height: "match_parent",
padding: 10,
} as any}
>
{delayString ? (
delayString.map((d) => {
let data = d.split(" ");
return (
<FlexWidget
style={{
flexDirection: "row",
width: "match_parent",
backgroundColor: colors.background,
flex: 1,
}}
key={data[1]}
>
<FlexText flex={3} text={data[0].replace("\n", "")} color={colors.text} />
<FlexText flex={1} text={data[1]} color={colors.text} />
<FlexText flex={1} text={data[3]} color={colors.text} />
</FlexWidget>
);
})
) : (
<TextWidget
style={{
color: colors.text,
fontSize: 20,
}}
text="現在、5分以上の遅れはありません。"
/>
)}
</ListWidget>
</FlexWidget>
);
}
const FlexText = ({ flex, text, color }: { flex: number; text: string; color: `#${string}` }) => (
<FlexWidget style={{ flex }}>
<TextWidget style={{ fontSize: 20, color }} text={text} />
</FlexWidget>
);