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 { ListItem } from "native-base"; import Icon from "react-native-vector-icons/Entypo"; import * as Location from "expo-location"; import StatusbarDetect from "./StatusbarDetect"; import AutoHeightImage from "react-native-auto-height-image"; import { widthPercentageToDP as wp } from "react-native-responsive-screen"; import { FontAwesome, Foundation, Ionicons, MaterialCommunityIcons, } from "@expo/vector-icons"; import LottieView from "lottie-react-native"; import { StationDeteilView } from "./components/ActionSheetComponents/StationDeteilView"; import { parseAllTrainDiagram } from "./lib/parseAllTrainDiagram"; import trainList from "./assets/originData/trainList"; import LED_vision from "./components/発車時刻表/LED_vidion"; import Sign from "./components/駅名表/Sign"; import { UsefulBox } from "./components/atom/UsefulBox"; import { TicketBox } from "./components/atom/TicketBox"; import { TextBox } from "./components/atom/TextBox"; import { getStationList, lineList } from "./lib/getStationList"; import { JRSTraInfo } from "./components/ActionSheetComponents/JRSTraInfo"; import { EachTrainInfo } from "./components/ActionSheetComponents/EachTrainInfo"; import useInterval from "./lib/useInterval"; import { HeaderConfig } from "./lib/HeaderConfig"; import { useFavoriteStation } from "./stateBox/useFavoriteStation"; export default function Menu(props) { const { navigation: { navigate }, getCurrentTrain, } = props; const JRSTraInfoEXAcSR = useRef(null); const StationBoardAcSR = useRef(null); const { favoriteStation } = useFavoriteStation(); //位置情報 const [locationStatus, setLocationStatus] = useState(null); useEffect(() => { Location.requestForegroundPermissionsAsync().then((data) => { setLocationStatus(data.status); }); }, []); //列車情報表示関連 const EachTrainInfoAsSR = useRef(null); const [trainInfo, setTrainInfo] = useState({ trainNum: undefined, limited: undefined, trainData: undefined, }); 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 [count, setCount] = useState(0); const [delayData, setDelayData] = useState(undefined); const [getTime, setGetTime] = useState(new Date()); const [loadingDelayData, setLoadingDelayData] = useState(true); const carouselRef = useRef(); const [selectedCurrentStation, setSelectedCurrentStation] = useState(0); useEffect(() => { fetch( "https://script.google.com/macros/s/AKfycbyKxch7z7l8e07LXulRHqxjVoIiB13kcgvoToLE-rqlxLmLSKdlmqz0FI1F2EuA7Zfg/exec" ) .then((response) => response.text()) .then((data) => setDelayData(data !== "" ? data.split("^") : null)) .then(() => setGetTime(new Date())) .finally(() => setLoadingDelayData(false)); }, [loadingDelayData]); const allStationData = [currentStation, ...favoriteStation].filter( (d) => d != undefined ); 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("