コード整理

This commit is contained in:
harukin-expo-dev-env 2024-05-21 10:33:41 +00:00
parent 9df3d0c62a
commit 1fafe1649c
4 changed files with 44 additions and 52 deletions

View File

@ -4,6 +4,25 @@ import {
TextWidget, TextWidget,
ListWidget, ListWidget,
} from "react-native-android-widget"; } 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 }) { export function InfoWidget({ time, text }) {
return ( return (

View File

@ -4,7 +4,25 @@ import {
TextWidget, TextWidget,
ListWidget, ListWidget,
} from "react-native-android-widget"; } 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 }) { export function TraInfoEXWidget({ time, delayString }) {
return ( return (
<FlexWidget <FlexWidget

View File

@ -1,8 +1,7 @@
import React from "react"; import React from "react";
import { TraInfoEXWidget } from "./TraInfoEXWidget"; import { TraInfoEXWidget, getDelayData } from "./TraInfoEXWidget";
import dayjs from "dayjs";
import { ToastAndroid } from "react-native"; import { ToastAndroid } from "react-native";
import { InfoWidget } from "./InfoWidget"; import { InfoWidget, getInfoString } from "./InfoWidget";
import { AS } from "../../storageControl"; import { AS } from "../../storageControl";
export const nameToWidget = { export const nameToWidget = {
@ -10,39 +9,6 @@ export const nameToWidget = {
Info_Widget: InfoWidget, 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) { export async function widgetTaskHandler(props) {
const { const {
widgetInfo, widgetInfo,
@ -65,18 +31,12 @@ export async function widgetTaskHandler(props) {
case "WIDGET_CLICK": case "WIDGET_CLICK":
case "WIDGET_RESIZED": case "WIDGET_RESIZED":
switch (WidgetName) { switch (WidgetName) {
case "JR_shikoku_train_info": {
const { time, delayString } = await getDelayData();
renderWidget(
<TraInfoEXWidget time={time} delayString={delayString} />
);
break;
}
case "Info_Widget": { case "Info_Widget": {
const { time, text } = await getInfoString(); const { time, text } = await getInfoString();
renderWidget(<InfoWidget time={time} text={text} />); renderWidget(<InfoWidget time={time} text={text} />);
break; break;
} }
case "JR_shikoku_train_info":
default: { default: {
const { time, delayString } = await getDelayData(); const { time, delayString } = await getDelayData();
renderWidget( renderWidget(

View File

@ -4,13 +4,10 @@ import { SwitchArea } from "../atom/SwitchArea";
import { CheckBox } from "react-native-elements"; import { CheckBox } from "react-native-elements";
import { TripleSwitchArea } from "../atom/TripleSwitchArea"; import { TripleSwitchArea } from "../atom/TripleSwitchArea";
import { getWidgetInfo, WidgetPreview } from "react-native-android-widget"; import { getWidgetInfo, WidgetPreview } from "react-native-android-widget";
import { TraInfoEXWidget } from "../AndroidWidget/TraInfoEXWidget"; import { getDelayData } from "../AndroidWidget/TraInfoEXWidget";
import { getInfoString } from "../AndroidWidget/InfoWidget";
import { AS } from "../../storageControl"; import { AS } from "../../storageControl";
import { import { nameToWidget } from "../AndroidWidget/widget-task-handler";
getDelayData,
nameToWidget,
getInfoString,
} from "../AndroidWidget/widget-task-handler";
import { ListItem } from "native-base"; import { ListItem } from "native-base";
export const WidgetSettings = ({ navigate }) => { export const WidgetSettings = ({ navigate }) => {
@ -116,9 +113,7 @@ export const WidgetSettings = ({ navigate }) => {
}} }}
> >
<WidgetPreview <WidgetPreview
renderWidget={() => ( renderWidget={() => <Info_Widget time={time} text={trainInfo} />}
<Info_Widget time={time} delayString={trainInfo} />
)}
width={400} width={400}
height={250} height={250}
/> />