超リファクタリング

This commit is contained in:
harukin-DeskMini
2022-10-10 19:14:03 +09:00
parent 762195e8e8
commit 4d921854c9
2 changed files with 194 additions and 231 deletions

View File

@@ -59,6 +59,7 @@ export default function LED_vision(props) {
const [trainIDSwitch, setTrainIDSwitch] = useState(false);
const [trainDescriptionSwitch, setTrainDescriptionSwitch] = useState(false);
useEffect(() => {
console.log("LED");
fetch(
"https://train.jr-shikoku.co.jp/g?arg1=station&arg2=traintimeinfo&arg3=dia",
HeaderConfig
@@ -66,49 +67,47 @@ export default function LED_vision(props) {
.then((response) => response.text())
.then((d) => {
const val = d.replace("[\r\n", "").split(",\r\n");
let returnData = {};
let trainDiagram = {};
val.forEach((element) => {
try {
let data = JSON.parse(element);
Object.keys(data).forEach((key) => (returnData[key] = data[key]));
Object.keys(data).forEach((key) => (trainDiagram[key] = data[key]));
} catch (e) {}
});
return returnData;
setTrainDiagram(trainDiagram);
return trainDiagram;
})
.then((trainDiagram) => {
let returnData = {};
if (trainDiagram) {
Object.keys(trainDiagram).forEach((key) => {
if (trainDiagram[key].match(props.station.Station_JP)) {
returnData[key] = trainDiagram[key];
}
});
if (!trainDiagram) {
setStationDiagram(returnData);
return;
}
setTrainDiagram(trainDiagram);
Object.keys(trainDiagram).forEach((key) => {
if (trainDiagram[key].match(props.station.Station_JP)) {
returnData[key] = trainDiagram[key];
}
});
setStationDiagram(returnData);
});
}, []);
const getTime = () => {
const returnData = [];
const date = new Date();
Object.keys(stationDiagram).forEach((d) => {
let a = (() => {
let returnData = {};
stationDiagram[d].split("#").forEach((data) => {
if (data.match("")) {
returnData.lastStation = data.split(",着,")[0];
let a = {};
stationDiagram[d].split("#").forEach((data) => {
if (data.match("")) {
a.lastStation = data.split(",着,")[0];
}
if (data.match(props.station.Station_JP)) {
if (data.match(",発,")) {
a.time = data.split(",発,")[1];
} else {
a.time = data.split(",着,")[1];
a.lastStation = "当駅止";
}
if (data.match(props.station.Station_JP)) {
if (data.match(",発,")) {
returnData.time = data.split(",発,")[1];
} else {
returnData.time = data.split(",着,")[1];
returnData.lastStation = "当駅止";
}
}
});
return returnData;
})();
}
});
returnData.push({ train: d, time: a.time, lastStation: a.lastStation });
});
@@ -141,42 +140,26 @@ export default function LED_vision(props) {
.then((d) => d.map((x) => ({ num: x.TrainNum, delay: x.delay })))
.then((d) => setCurrentTrain(d));
getCurrentTrain();
const currentTrainInterval = setInterval(() => getCurrentTrain(), 15000);
const currentTrainInterval = setInterval(getCurrentTrain, 15000);
return () => clearInterval(currentTrainInterval);
}, []);
const filtering = (d) => currentTrain.map((m) => m.num).includes(d.train);
console.log(new Date());
const timeFiltering = (d) => {
const date = new Date();
const newDate = new Date();
let data = d.time.split(":");
let delay = isNaN(
currentTrain.filter((data) => data.num == d.train)[0].delay
)
let delay = isNaN(currentTrain.filter((t) => t.num == d.train)[0].delay)
? 0
: currentTrain.filter((data) => data.num == d.train)[0].delay;
: currentTrain.filter((t) => t.num == d.train)[0].delay;
date.setHours(parseInt(data[0]));
date.setMinutes(parseInt(data[1]) + parseInt(delay));
console.log(date);
console.log(newDate);
if (!(newDate > date)) {
return true;
}
return false;
};
const finalFiltering = (d) => {
if (finalSwitch) {
return true;
} else {
if (d.lastStation == "当駅止") {
return false;
}
return true;
}
};
return (
<View
style={{
@@ -208,122 +191,114 @@ export default function LED_vision(props) {
{trainTimeAndNumber
? currentTrain &&
trainTimeAndNumber
.filter(filtering)
.filter((d) => currentTrain.map((m) => m.num).includes(d.train))
.filter(timeFiltering)
.filter(finalFiltering)
.map((d, index) => [
<View
style={{
alignContent: "center",
alignItems: "center",
width: "94%",
marginVertical: 5,
marginHorizontal: "3%",
backgroundColor: "#000",
flexDirection: "row",
}}
>
<View style={{ flex: 9 }}>
<Text
style={{
fontSize: (() => {
if (
customTrainDataDetector(d.train).trainName.length > 6
) {
return 15;
} else {
return 20;
}
})(),
color: (() => {
switch (customTrainDataDetector(d.train).type) {
case "Rapid":
return "aqua";
case "LTDEXP":
return "red";
case "NightLTDEXP":
return "red";
case "Normal":
return "white";
}
})(),
fontWeight: "bold",
}}
>
{trainIDSwitch
? d.train
: (() => {
switch (customTrainDataDetector(d.train).type) {
.filter((d) => !!finalSwitch || d.lastStation != "当駅止")
.map((d, index) => {
const train = customTrainDataDetector(d.train);
return [
<View
style={{
alignContent: "center",
alignItems: "center",
width: "94%",
marginVertical: 5,
marginHorizontal: "3%",
backgroundColor: "#000",
flexDirection: "row",
}}
>
<View style={{ flex: 9 }}>
<Text
style={{
fontSize: train.trainName.length > 6 ? 15 : 20,
color: (() => {
switch (train.type) {
case "Rapid":
return "快速";
return "aqua";
case "LTDEXP":
return "特急";
return "red";
case "NightLTDEXP":
return "寝台特急";
return "red";
case "Normal":
return "普通列車";
return "white";
}
})() +
" " +
customTrainDataDetector(d.train).trainName +
(() => {
if (
customTrainDataDetector(d.train).trainNumDistance !=
null
) {
return (
parseInt(
})(),
fontWeight: "bold",
}}
>
{trainIDSwitch
? d.train
: (() => {
switch (train.type) {
case "Rapid":
return "快速";
case "LTDEXP":
return "特急";
case "NightLTDEXP":
return "寝台特急";
case "Normal":
return "普通列車";
}
})() +
" " +
train.trainName +
(!train.trainNumDistance
? ""
: parseInt(
d.train.replace("M", "").replace("D", "")
) -
customTrainDataDetector(d.train)
.trainNumDistance +
"号"
);
} else {
return "";
}
})()}
</Text>
</View>
<View style={{ flex: 4, flexDirection: "row" }}>
<Text
style={{
fontSize: d.lastStation.length > 4 ? 15 : 20,
color: "white",
fontWeight: "bold",
}}
>
{d.lastStation}
</Text>
</View>
<View style={{ flex: 3 }}>
<Text
style={{ fontSize: 20, color: "white", fontWeight: "bold" }}
>
{d.time}
</Text>
</View>
<View style={{ flex: 4 }}>
<Text
style={{ fontSize: 20, color: "white", fontWeight: "bold" }}
>
{(() => {
let data = currentTrain.filter(
(data) => data.num == d.train
)[0].delay;
if (isNaN(data)) {
return data;
} else if (data == 0) {
return "定刻通り";
} else {
return data + "分遅れ";
}
})()}
</Text>
</View>
</View>,
Boolean(trainDescriptionSwitch) &&
Boolean(customTrainDataDetector(d.train).info) && (
train.trainNumDistance +
"号")}
</Text>
</View>
<View style={{ flex: 4, flexDirection: "row" }}>
<Text
style={{
fontSize: d.lastStation.length > 4 ? 15 : 20,
color: "white",
fontWeight: "bold",
}}
>
{d.lastStation}
</Text>
</View>
<View style={{ flex: 3 }}>
<Text
style={{
fontSize: 20,
color: "white",
fontWeight: "bold",
}}
>
{d.time}
</Text>
</View>
<View style={{ flex: 4 }}>
<Text
style={{
fontSize: 20,
color: "white",
fontWeight: "bold",
}}
>
{(() => {
const delay = currentTrain.filter(
(a) => a.num == d.train
)[0].delay;
switch (true) {
case isNaN(delay):
return delay;
case delay == 0:
return "定刻通り";
default:
return delay + "分遅れ";
}
})()}
</Text>
</View>
</View>,
trainDescriptionSwitch && !!train.info && (
<View
style={{
alignContent: "center",
@@ -344,12 +319,13 @@ export default function LED_vision(props) {
}}
>
{" "}
&gt; {customTrainDataDetector(d.train).info}
&gt; {train.info}
</Text>
</View>
</View>
),
])
];
})
: null}
<View style={{ flexDirection: "row", padding: 10 }}>
<Text
@@ -363,10 +339,7 @@ export default function LED_vision(props) {
>
種別名 / 列番
</Text>
<Switch
value={trainIDSwitch}
onValueChange={(value) => setTrainIDSwitch(!trainIDSwitch)}
/>
<Switch value={trainIDSwitch} onValueChange={setTrainIDSwitch} />
<View style={{ flex: 1 }} />
<Text
style={{
@@ -381,9 +354,7 @@ export default function LED_vision(props) {
</Text>
<Switch
value={trainDescriptionSwitch}
onValueChange={(value) =>
setTrainDescriptionSwitch(!trainDescriptionSwitch)
}
onValueChange={setTrainDescriptionSwitch}
/>
<View style={{ flex: 1 }} />
<Text
@@ -397,10 +368,7 @@ export default function LED_vision(props) {
>
当駅止表示
</Text>
<Switch
value={finalSwitch}
onValueChange={(value) => setFinalSwitch(!finalSwitch)}
/>
<Switch value={finalSwitch} onValueChange={setFinalSwitch} />
</View>
</View>
);