diff --git a/components/AndroidWidget/InfoWidget.jsx b/components/AndroidWidget/InfoWidget.jsx new file mode 100644 index 0000000..f70c8cc --- /dev/null +++ b/components/AndroidWidget/InfoWidget.jsx @@ -0,0 +1,114 @@ +import React from "react"; +import { + FlexWidget, + TextWidget, + ListWidget, +} from "react-native-android-widget"; +import dayjs from "dayjs"; +import { ToastAndroid } from "react-native"; + +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 function InfoWidget({ time, text }) { + return ( + + + + + + + + {text ? ( + + + + ) : ( + + )} + + + ); +} + +const FlexText = ({ flex, text }) => ( + + + +); diff --git a/components/AndroidWidget/TraInfoEXWidget.jsx b/components/AndroidWidget/TraInfoEXWidget.jsx index d55cb43..3cfc161 100644 --- a/components/AndroidWidget/TraInfoEXWidget.jsx +++ b/components/AndroidWidget/TraInfoEXWidget.jsx @@ -4,7 +4,25 @@ import { TextWidget, ListWidget, } from "react-native-android-widget"; +import dayjs from "dayjs"; +import { ToastAndroid } from "react-native"; +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 function TraInfoEXWidget({ time, delayString }) { return ( { - // 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 widgetInfo = props.widgetInfo; - const Widget = nameToWidget[widgetInfo.widgetName]; + const { + widgetInfo, + widgetAction, + renderWidget, + clickAction, + clickActionData, + } = props; + const WidgetName = await AS.getItem( + `widgetType/${widgetInfo.widgetId}` + ).catch((e) => "JR_shikoku_train_info"); ToastAndroid.show( - `Widget Action: ${JSON.stringify(props.widgetInfo.widgetId)}`, + `Widget Action: ${JSON.stringify(widgetInfo.widgetId)}`, ToastAndroid.SHORT ); - const { time, delayString } = await getDelayData(); - switch (props.widgetAction) { + ToastAndroid.show(`Widget Name: ${WidgetName}`, ToastAndroid.SHORT); + switch (widgetAction) { case "WIDGET_ADDED": case "WIDGET_UPDATE": case "WIDGET_CLICK": case "WIDGET_RESIZED": - // Not needed for now - props.renderWidget(); + switch (WidgetName) { + case "Info_Widget": { + const { time, text } = await getInfoString(); + renderWidget(); + break; + } + case "JR_shikoku_train_info": + default: { + const { time, delayString } = await getDelayData(); + renderWidget( + + ); + break; + } + } break; case "WIDGET_DELETED": - // Not needed for now + AS.removeItem(`widgetType/${widgetInfo.widgetId}`); break; default: break; diff --git a/components/Settings/WidgetSettings.js b/components/Settings/WidgetSettings.js index 5b08b0c..c4fb501 100644 --- a/components/Settings/WidgetSettings.js +++ b/components/Settings/WidgetSettings.js @@ -4,38 +4,38 @@ import { SwitchArea } from "../atom/SwitchArea"; 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 { - getDelayData, - nameToWidget, -} from "../AndroidWidget/widget-task-handler"; +import { getDelayData } from "../AndroidWidget/TraInfoEXWidget"; +import { getInfoString } from "../AndroidWidget/InfoWidget"; +import { AS } from "../../storageControl"; +import { nameToWidget } from "../AndroidWidget/widget-task-handler"; import { ListItem } from "native-base"; export const WidgetSettings = ({ navigate }) => { + const { JR_shikoku_train_info, Info_Widget } = nameToWidget; + const [time, setTime] = useState(); + const [delayString, setDelayString] = useState(); + const [trainInfo, setTrainInfo] = useState(); const [widgetList, setWidgetList] = useState([]); - useEffect(() => { + const reload = async () => { const d = []; - Object.keys(nameToWidget).forEach((element) => { - const widgetInfo = getWidgetInfo(element); - widgetInfo.then((s) => { - if (s.length > 0) { - s.forEach((elem) => { - console.log(elem); - d.push(elem); - }); - } - }); + const data = await getWidgetInfo("JR_shikoku_train_info"); + data.forEach((elem) => { + d.push(elem.widgetId); }); setWidgetList(d); - }, []); + }; + + useEffect(reload, []); - const [time, setTime] = useState(time); - const [delayString, setDelayString] = useState(delayString); useEffect(() => { getDelayData().then(({ time, delayString }) => { setTime(time); setDelayString(delayString); }); + getInfoString().then(({ time, text }) => { + setTime(time); + setTrainInfo(text); + }); }, []); return ( @@ -90,18 +90,29 @@ 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,32 +140,8 @@ export const WidgetSettings = ({ navigate }) => { 名前 - {widgetList.map((widget) => ( - - - {widget.widgetId} - - - {widget.widgetName} - - + {widgetList.map((id) => ( + ))} ( /> ); +const WidgetList = ({ id }) => { + const [widgetConfig, setWidgetConfig] = useState(""); + const reload = () => { + AS.getItem(`widgetType/${id}`) + .then((widgetType) => { + setWidgetConfig(widgetType); + }) + .catch((e) => { + setWidgetConfig("JR_shikoku_train_info"); + }); + }; + useEffect(reload, [id]); + return ( + { + //widget.widgetNameで定義されてないもう一つのウィジェットを選択する + if (widgetConfig === "Info_Widget") { + AS.setItem(`widgetType/${id}`, "JR_shikoku_train_info"); + } else { + AS.setItem(`widgetType/${id}`, "Info_Widget"); + } + reload(); + }} + > + + {id} + + + {widgetConfig} + + + ); +}; diff --git a/storageControl.js b/storageControl.js index 4f43140..939a943 100644 --- a/storageControl.js +++ b/storageControl.js @@ -11,4 +11,5 @@ export const AS = { // if set to null, then it will never expire. expires: null, }), + removeItem: (key) => storage.remove({ key }), };