From f85c79ab2c511e0c63c89b50b49ce95f74f889bb Mon Sep 17 00:00:00 2001 From: harukin-expo-dev-env Date: Tue, 21 May 2024 10:06:08 +0000 Subject: [PATCH] =?UTF-8?q?=E3=82=A6=E3=82=A3=E3=82=B8=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=83=88=E3=81=AE=E3=82=B3=E3=82=A2=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/AndroidWidget/InfoWidget.jsx | 95 +++++++++++++++ .../AndroidWidget/widget-task-handler.jsx | 37 +++++- components/Settings/WidgetSettings.js | 111 ++++++++++++------ 3 files changed, 201 insertions(+), 42 deletions(-) create mode 100644 components/AndroidWidget/InfoWidget.jsx diff --git a/components/AndroidWidget/InfoWidget.jsx b/components/AndroidWidget/InfoWidget.jsx new file mode 100644 index 0000000..8badfd2 --- /dev/null +++ b/components/AndroidWidget/InfoWidget.jsx @@ -0,0 +1,95 @@ +import React from "react"; +import { + FlexWidget, + TextWidget, + ListWidget, +} from "react-native-android-widget"; + +export function InfoWidget({ time, delayString }) { + return ( + + + + + + + + {delayString ? ( + + + + ) : ( + + )} + + + ); +} + +const FlexText = ({ flex, text }) => ( + + + +); diff --git a/components/AndroidWidget/widget-task-handler.jsx b/components/AndroidWidget/widget-task-handler.jsx index 5b7ae14..5f81036 100644 --- a/components/AndroidWidget/widget-task-handler.jsx +++ b/components/AndroidWidget/widget-task-handler.jsx @@ -2,10 +2,30 @@ 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 = { // Hello will be the **name** with which we will reference our widget. 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 () => { @@ -25,20 +45,29 @@ export const getDelayData = async () => { return { time, delayString }; }; export async function widgetTaskHandler(props) { - const widgetInfo = props.widgetInfo; - const Widget = nameToWidget[widgetInfo.widgetName]; + 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 ); - const { time, delayString } = await getDelayData(); + ToastAndroid.show(`Widget Name: ${WidgetName}`, ToastAndroid.SHORT); switch (props.widgetAction) { case "WIDGET_ADDED": case "WIDGET_UPDATE": case "WIDGET_CLICK": case "WIDGET_RESIZED": // Not needed for now - props.renderWidget(); + if (WidgetName === "JR_shikoku_train_info") { + const Widget = nameToWidget[WidgetName]; + const { time, delayString } = await getDelayData(); + props.renderWidget(); + } else if (WidgetName === "Info_Widget") { + const Widget = nameToWidget[WidgetName]; + const { time, text } = await getInfoString(); + props.renderWidget(); + } break; diff --git a/components/Settings/WidgetSettings.js b/components/Settings/WidgetSettings.js index 5b08b0c..38633b2 100644 --- a/components/Settings/WidgetSettings.js +++ b/components/Settings/WidgetSettings.js @@ -5,13 +5,16 @@ import { CheckBox } from "react-native-elements"; import { TripleSwitchArea } from "../atom/TripleSwitchArea"; import { getWidgetInfo, WidgetPreview } from "react-native-android-widget"; import { TraInfoEXWidget } from "../AndroidWidget/TraInfoEXWidget"; +import { AS } from "../../storageControl"; import { getDelayData, nameToWidget, + getInfoString, } from "../AndroidWidget/widget-task-handler"; import { ListItem } from "native-base"; export const WidgetSettings = ({ navigate }) => { + const { JR_shikoku_train_info, Info_Widget } = nameToWidget; const [widgetList, setWidgetList] = useState([]); useEffect(() => { const d = []; @@ -28,14 +31,18 @@ export const WidgetSettings = ({ navigate }) => { }); setWidgetList(d); }, []); - const [time, setTime] = useState(time); const [delayString, setDelayString] = useState(delayString); + const [trainInfo, setTrainInfo] = useState(); useEffect(() => { getDelayData().then(({ time, delayString }) => { setTime(time); setDelayString(delayString); }); + getInfoString().then(({ time, text }) => { + setTime(time); + setTrainInfo(text); + }); }, []); return ( @@ -90,18 +97,31 @@ export const WidgetSettings = ({ navigate }) => { margin: 10, }} > - {Object.keys(nameToWidget).map((Name) => { - const Data = nameToWidget[Name]; - return ( - ( - - )} - width={400} - height={250} - /> - ); - })} + ( + + )} + width={400} + height={250} + /> + + + ( + + )} + width={400} + height={250} + /> @@ -129,33 +149,48 @@ export const WidgetSettings = ({ navigate }) => { 名前 - {widgetList.map((widget) => ( - - { + return ( + { + //widget.widgetNameで定義されてないもう一つのウィジェットを選択する + if (widget.widgetName === "Info_Widget") { + AS.setItem( + `widgetType/${widget.widgetId}`, + "JR_shikoku_train_info" + ); + } else { + AS.setItem(`widgetType/${widget.widgetId}`, "Info_Widget"); + } }} > - {widget.widgetId} - - - {widget.widgetName} - - - ))} + + {widget.widgetId} + + + {widget.widgetName} + + + ); + })}