import React, { useRef, useState, useEffect } from "react"; import Carousel from "react-native-snap-carousel"; import { Platform, View, ScrollView, Linking, Text, TouchableOpacity, } from "react-native"; import Constants from "expo-constants"; import * as Location from "expo-location"; import StatusbarDetect from "./StatusbarDetect"; import { widthPercentageToDP as wp } from "react-native-responsive-screen"; import { Ionicons } from "@expo/vector-icons"; import LottieView from "lottie-react-native"; import { parseAllTrainDiagram } from "./lib/parseAllTrainDiagram"; import trainList from "./assets/originData/trainList"; import LED_vision from "./components/発車時刻表/LED_vidion"; import Sign from "./components/駅名表/Sign"; import { TitleBar } from "./components/Menu/TitleBar"; import { FixedContentBottom } from "./components/Menu/FixedContentBottom"; import { UsefulBox } from "./components/atom/UsefulBox"; import { getStationList, lineList } from "./lib/getStationList"; import useInterval from "./lib/useInterval"; import { HeaderConfig } from "./lib/HeaderConfig"; import { useFavoriteStation } from "./stateBox/useFavoriteStation"; import { SheetManager } from "react-native-actions-sheet"; import { useTrainDelayData } from "./stateBox/useTrainDelayData"; import { useNavigation } from "@react-navigation/native"; export default function Menu({ getCurrentTrain }) { const { navigate } = useNavigation(); const { favoriteStation } = useFavoriteStation(); //位置情報 const [locationStatus, setLocationStatus] = useState(null); useEffect(() => { Location.requestForegroundPermissionsAsync().then((data) => { setLocationStatus(data.status); }); }, []); const getCurrentPosition = () => { if (locationStatus !== "granted") return () => {}; Location.getCurrentPositionAsync({}).then((location) => makeCurrentStation(location) ); }; const makeCurrentStation = (location) => { if (!originalStationList) return () => {}; const findStationEachLine = (selectLine) => { const searchArea = 0.0015; const _calcDistance = (from, to) => { let lat = Math.abs(from.lat - to.lat); let lng = Math.abs(from.lng - to.lng); return Math.sqrt(lat * lat + lng * lng); }; let NearStation = selectLine.filter( (d) => _calcDistance(d, { lat: location.coords.latitude, lng: location.coords.longitude, }) < searchArea ); return NearStation; }; let returnDataBase = lineList .map((d) => findStationEachLine(originalStationList[d])) .filter((d) => d.length > 0) .reduce((pre, current) => { pre.push(...current); return pre; }, []); if (returnDataBase.length) { let currentStation = currentStation == undefined ? [] : currentStation; if (currentStation.toString() != returnDataBase.toString()) { setCurrentStation(returnDataBase); } } else { setCurrentStation(undefined); } }; useEffect(getCurrentPosition, [locationStatus]); useInterval(getCurrentPosition, 5000); const [currentStation, setCurrentStation] = useState(undefined); //第三要素 const [originalStationList, setOriginalStationList] = useState([]); // 第一要素 useEffect(() => getStationList().then(setOriginalStationList), []); const carouselRef = useRef(); const [selectedCurrentStation, setSelectedCurrentStation] = useState(0); const [allStationData, setAllStationData] = useState([]); useEffect(() => { setAllStationData( [currentStation, ...favoriteStation].filter((d) => d != undefined) ); }, [currentStation, favoriteStation]); useEffect(() => { if (allStationData.length == 0) { setSelectedCurrentStation(0); return; } if (allStationData[selectedCurrentStation] == undefined) { const count = selectedCurrentStation - 1; setSelectedCurrentStation(count); } }, [selectedCurrentStation, currentStation, favoriteStation]); useEffect(() => { if (!carouselRef.current) return; if (carouselRef.current?._itemToSnapTo != selectedCurrentStation) { carouselRef.current.snapToItem(0); carouselRef.current.snapToItem(selectedCurrentStation); } }, [selectedCurrentStation]); //全列車ダイヤリストを作成するuseEffect const [trainDiagram, setTrainDiagram] = useState(null); // 全列車のダイヤを列番ベースで整理 useEffect(() => { //全列車リストを生成する副作用[無条件初回実行] fetch( "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=traintimeinfo&arg3=dia", HeaderConfig ) .then((response) => response.text()) .then((d) => { if (d.indexOf("