コードの整理とuseCurrentTrainの取得ルートの変更

This commit is contained in:
harukin-expo-dev-env 2025-04-13 18:06:22 +00:00
parent 015e4e54ed
commit f26da93193
4 changed files with 42 additions and 59 deletions

View File

@ -15,7 +15,6 @@ import Setting from "./components/Settings/settings";
import { useFavoriteStation } from "./stateBox/useFavoriteStation"; import { useFavoriteStation } from "./stateBox/useFavoriteStation";
import { optionData } from "./lib/stackOption"; import { optionData } from "./lib/stackOption";
import AllTrainDiagramView from "./components/AllTrainDiagramView"; import AllTrainDiagramView from "./components/AllTrainDiagramView";
import { useCurrentTrain } from "./stateBox/useCurrentTrain";
import { useNavigation } from "@react-navigation/native"; import { useNavigation } from "@react-navigation/native";
import { news } from "./config/newsUpdate"; import { news } from "./config/newsUpdate";
import { useBottomTabBarHeight } from "@react-navigation/bottom-tabs"; import { useBottomTabBarHeight } from "@react-navigation/bottom-tabs";
@ -25,7 +24,6 @@ export function MenuPage() {
const { favoriteStation, setFavoriteStation } = useFavoriteStation(); const { favoriteStation, setFavoriteStation } = useFavoriteStation();
const { height, width } = useWindowDimensions(); const { height, width } = useWindowDimensions();
const tabBarHeight = useBottomTabBarHeight(); const tabBarHeight = useBottomTabBarHeight();
const { getCurrentTrain } = useCurrentTrain();
const navigation = useNavigation(); const navigation = useNavigation();
const { addListener } = navigation; const { addListener } = navigation;
useEffect(() => { useEffect(() => {
@ -86,7 +84,7 @@ export function MenuPage() {
headerTransparent: true, headerTransparent: true,
}} }}
children={() => ( children={() => (
<Menu getCurrentTrain={getCurrentTrain} scrollRef={scrollRef} /> <Menu scrollRef={scrollRef} />
)} )}
/> />
<Stack.Screen name="news" options={optionData} component={News} /> <Stack.Screen name="news" options={optionData} component={News} />

View File

@ -1,25 +1,25 @@
import Sign from "@/components/駅名表/Sign"; import Sign from "@/components/駅名表/Sign";
import React, { useEffect, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import { AS } from "@/storageControl"; import { AS } from "@/storageControl";
import { useWindowDimensions, View, LayoutAnimation } from "react-native"; import { useWindowDimensions, View, LayoutAnimation } from "react-native";
import Carousel from "react-native-reanimated-carousel"; import Carousel, { ICarouselInstance } from "react-native-reanimated-carousel";
import { SheetManager } from "react-native-actions-sheet"; import { SheetManager } from "react-native-actions-sheet";
import { StationNumber } from "../StationPagination"; import { StationNumber } from "../StationPagination";
import { SimpleDot } from "../SimpleDot"; import { SimpleDot } from "../SimpleDot";
export const CarouselBox = ({ export const CarouselBox = ({
originalStationList, originalStationList,
allStationData, listUpStation,
currentStation, nearPositionStation,
setSelectedCurrentStation, setListIndex,
carouselRef, listIndex,
selectedCurrentStation,
navigate, navigate,
}) => { }) => {
const carouselRef = useRef<ICarouselInstance>(null);
const { height, width } = useWindowDimensions(); const { height, width } = useWindowDimensions();
const [dotButton, setDotButton] = useState(false); const [dotButton, setDotButton] = useState(false);
const oPSign = () => { const oPSign = () => {
const payload = { const payload = {
currentStation: allStationData[selectedCurrentStation], currentStation: listUpStation[listIndex],
navigate, navigate,
goTo: "menu", goTo: "menu",
//@ts-ignore //@ts-ignore
@ -46,6 +46,13 @@ export const CarouselBox = ({
setDotButton(data === "true"); setDotButton(data === "true");
}); });
}, []); }, []);
useEffect(() => {
if (!carouselRef.current) return;
carouselRef?.current.scrollTo({
count: listIndex - carouselRef.current.getCurrentIndex(),
animated: true,
});
}, [listIndex]);
const RenderItem = ({ item, index }) => { const RenderItem = ({ item, index }) => {
return ( return (
<View <View
@ -61,7 +68,7 @@ export const CarouselBox = ({
<View style={{ flex: 1 }} /> <View style={{ flex: 1 }} />
<Sign <Sign
stationID={item[0].StationNumber} stationID={item[0].StationNumber}
isCurrentStation={item == currentStation} isCurrentStation={item == nearPositionStation}
oP={oPSign} oP={oPSign}
oLP={oLPSign} oLP={oLPSign}
/> />
@ -73,7 +80,7 @@ export const CarouselBox = ({
<View style={{ flex: 1, paddingTop: 10 }}> <View style={{ flex: 1, paddingTop: 10 }}>
<Carousel <Carousel
ref={carouselRef} ref={carouselRef}
data={allStationData} data={listUpStation}
height={(((width / 100) * 80) / 20) * 9 + 10} height={(((width / 100) * 80) / 20) * 9 + 10}
pagingEnabled={true} pagingEnabled={true}
snapEnabled={true} snapEnabled={true}
@ -86,7 +93,7 @@ export const CarouselBox = ({
parallaxScrollingOffset: 100, parallaxScrollingOffset: 100,
parallaxAdjacentItemScale: 0.8, parallaxAdjacentItemScale: 0.8,
}} }}
onSnapToItem={setSelectedCurrentStation} onSnapToItem={setListIndex}
renderItem={RenderItem} renderItem={RenderItem}
/> />
<View <View
@ -98,19 +105,19 @@ export const CarouselBox = ({
}} }}
> >
{originalStationList && {originalStationList &&
allStationData.map((d, index) => { listUpStation.map((d, index) => {
const active = index == selectedCurrentStation; const active = index == listIndex;
const numberKey = d[0].StationNumber + index; const numberKey = d[0].StationNumber + index;
return dotButton ? ( return dotButton ? (
<StationNumber <StationNumber
onPress={() => setSelectedCurrentStation(index)} onPress={() => setListIndex(index)}
currentStation={d} currentStation={d}
active={active} active={active}
key={numberKey} key={numberKey}
/> />
) : ( ) : (
<SimpleDot <SimpleDot
onPress={() => setSelectedCurrentStation(index)} onPress={() => setListIndex(index)}
active={active} active={active}
key={numberKey} key={numberKey}
/> />

View File

@ -42,44 +42,43 @@ import { useAllTrainDiagram } from "@/stateBox/useAllTrainDiagram";
* 9062D 四国まんなか千年ものがたり(臨時) * 9062D 四国まんなか千年ものがたり(臨時)
*/ */
export default function LED_vision(props) { export default function LED_vision(props) {
const { station, getCurrentTrain, navigate, openStationACFromEachTrainInfo } = const { station, navigate, openStationACFromEachTrainInfo } = props;
props; const { getCurrentTrain, currentTrain } = useCurrentTrain();
const { currentTrain } = useCurrentTrain();
const [stationDiagram, setStationDiagram] = useState({}); //当該駅の全時刻表 const [stationDiagram, setStationDiagram] = useState({}); //当該駅の全時刻表
const [finalSwitch, setFinalSwitch] = useState(false); const [finalSwitch, setFinalSwitch] = useState(false);
const [trainIDSwitch, setTrainIDSwitch] = useState(false); const [trainIDSwitch, setTrainIDSwitch] = useState(false);
const [trainDescriptionSwitch, setTrainDescriptionSwitch] = useState(false); const [trainDescriptionSwitch, setTrainDescriptionSwitch] = useState(false);
const [isInfoArea, setIsInfoArea] = useState(false); const [isInfoArea, setIsInfoArea] = useState(false);
const { areaInfo, areaStationID } = useAreaInfo(); const { areaInfo, areaStationID } = useAreaInfo();
const { allTrainDiagram: trainDiagram } = useAllTrainDiagram(); const { allTrainDiagram } = useAllTrainDiagram();
useEffect(() => { useEffect(() => {
AS.getItem("LEDSettings/trainIDSwitch").then((data) => { AS.getItem("LEDSettings/trainIDSwitch").then((data) => {
setTrainIDSwitch(data === "true"); setTrainIDSwitch(data === "true");
}); });
AS.getItem("LEDSettings/trainDescriptionSwitch").then((data) => { AS.getItem("LEDSettings/trainDescriptionSwitch").then((data) => {
setTrainDescriptionSwitch(data == "true"); setTrainDescriptionSwitch(data === "true");
}); });
AS.getItem("LEDSettings/finalSwitch").then((data) => { AS.getItem("LEDSettings/finalSwitch").then((data) => {
setFinalSwitch(data == "true"); setFinalSwitch(data === "true");
}); });
}, []); }, []);
useEffect(() => { useEffect(() => {
// 現在の駅に停車するダイヤを作成する副作用[列車ダイヤと現在駅情報] // 現在の駅に停車するダイヤを作成する副作用[列車ダイヤと現在駅情報]
if (!trainDiagram) { if (!allTrainDiagram) {
setStationDiagram({}); setStationDiagram({});
return; return;
} }
let returnData = {}; let returnData = {};
Object.keys(trainDiagram).forEach((key) => { Object.keys(allTrainDiagram).forEach((key) => {
if (trainDiagram[key].match(station[0].Station_JP + ",")) { if (allTrainDiagram[key].match(station[0].Station_JP + ",")) {
returnData[key] = trainDiagram[key]; returnData[key] = allTrainDiagram[key];
} }
}); });
setStationDiagram(returnData); setStationDiagram(returnData);
setIsInfoArea(station.some((s) => areaStationID.includes(s.StationNumber))); setIsInfoArea(station.some((s) => areaStationID.includes(s.StationNumber)));
}, [trainDiagram, station]); }, [allTrainDiagram, station]);
const [trainTimeAndNumber, setTrainTimeAndNumber] = useState(null); const [trainTimeAndNumber, setTrainTimeAndNumber] = useState(null);

39
menu.js
View File

@ -27,7 +27,7 @@ configureReanimatedLogger({
level: ReanimatedLogLevel.error, // Set the log level to error level: ReanimatedLogLevel.error, // Set the log level to error
strict: true, // Reanimated runs in strict mode by default strict: true, // Reanimated runs in strict mode by default
}); });
export default function Menu({ getCurrentTrain, scrollRef }) { export default function Menu({scrollRef }) {
const { navigate, addListener, isFocused } = useNavigation(); const { navigate, addListener, isFocused } = useNavigation();
const { favoriteStation } = useFavoriteStation(); const { favoriteStation } = useFavoriteStation();
const { originalStationList } = useStationList(); const { originalStationList } = useStationList();
@ -95,28 +95,17 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
}; };
let returnDataBase = lineList let returnDataBase = lineList
.map((d) => { .map((d) => findStationEachLine(originalStationList[d]))
return findStationEachLine(originalStationList[d]); .filter((d) => d.length > 0)
})
.filter((d) => {
return d.length > 0;
})
.reduce((pre, current) => { .reduce((pre, current) => {
pre.push(...current); pre.push(...current);
return pre; return pre;
}, []); }, []).map((d) => [d]);
setNearPositionStation(returnDataBase.length ? returnDataBase : []);
if (returnDataBase.length) {
const returnData = returnDataBase.map((d) => [d]);
setNearPositionStation(returnData);
} else {
setNearPositionStation([]);
}
}; };
const [nearPositionStation, setNearPositionStation] = useState([]); //第三要素 const [nearPositionStation, setNearPositionStation] = useState([]); //第三要素
const carouselRef = useRef();
const [listIndex, setListIndex] = useState(0); const [listIndex, setListIndex] = useState(0);
const [listUpStation, setListUpStation] = useState([]); const [listUpStation, setListUpStation] = useState([]);
@ -137,14 +126,6 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
setListIndex(count); setListIndex(count);
} }
}, [listIndex, nearPositionStation, listUpStation]); }, [listIndex, nearPositionStation, listUpStation]);
useEffect(() => {
if (!carouselRef.current) return;
carouselRef?.current.scrollTo({
count: listIndex - carouselRef.current.getCurrentIndex(),
animated: true,
});
}, [listIndex]);
useEffect(() => { useEffect(() => {
if (originalStationList == undefined) return; if (originalStationList == undefined) return;
if (listUpStation.length == 0) return; if (listUpStation.length == 0) return;
@ -211,18 +192,16 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
<CarouselBox <CarouselBox
{...{ {...{
originalStationList, originalStationList,
allStationData: listUpStation, listUpStation,
currentStation: nearPositionStation, nearPositionStation,
setSelectedCurrentStation: setListIndex, setListIndex ,
carouselRef, listIndex,
selectedCurrentStation: listIndex,
navigate, navigate,
}} }}
/> />
{listUpStation[listIndex] && ( {listUpStation[listIndex] && (
<LED_vision <LED_vision
station={listUpStation[listIndex]} station={listUpStation[listIndex]}
getCurrentTrain={getCurrentTrain}
navigate={navigate} navigate={navigate}
openStationACFromEachTrainInfo={() => {}} openStationACFromEachTrainInfo={() => {}}
/> />