位置情報権限の取得場所を変更
This commit is contained in:
parent
97a6bbc619
commit
f922edb973
2
App.js
2
App.js
@ -18,6 +18,7 @@ import { TrainMenuProvider } from "./stateBox/useTrainMenu";
|
|||||||
import { buildProvidersTree } from "./lib/providerTreeProvider";
|
import { buildProvidersTree } from "./lib/providerTreeProvider";
|
||||||
import { StationListProvider } from "./stateBox/useStationList";
|
import { StationListProvider } from "./stateBox/useStationList";
|
||||||
import { NotificationProvider } from "./stateBox/useNotifications";
|
import { NotificationProvider } from "./stateBox/useNotifications";
|
||||||
|
import { UserPositionProvider } from "./stateBox/useUserPosition";
|
||||||
|
|
||||||
LogBox.ignoreLogs([
|
LogBox.ignoreLogs([
|
||||||
"ViewPropTypes will be removed",
|
"ViewPropTypes will be removed",
|
||||||
@ -36,6 +37,7 @@ export default function App() {
|
|||||||
const ProviderTree = buildProvidersTree([
|
const ProviderTree = buildProvidersTree([
|
||||||
AllTrainDiagramProvider,
|
AllTrainDiagramProvider,
|
||||||
NotificationProvider,
|
NotificationProvider,
|
||||||
|
UserPositionProvider,
|
||||||
StationListProvider,
|
StationListProvider,
|
||||||
FavoriteStationProvider,
|
FavoriteStationProvider,
|
||||||
TrainDelayDataProvider,
|
TrainDelayDataProvider,
|
||||||
|
26
menu.js
26
menu.js
@ -1,7 +1,6 @@
|
|||||||
import React, { useRef, useState, useEffect } from "react";
|
import React, { useRef, useState, useEffect } from "react";
|
||||||
import { Platform, View, ScrollView, useWindowDimensions } from "react-native";
|
import { Platform, View, ScrollView, useWindowDimensions } from "react-native";
|
||||||
import Constants from "expo-constants";
|
import Constants from "expo-constants";
|
||||||
import * as Location from "expo-location";
|
|
||||||
import {
|
import {
|
||||||
configureReanimatedLogger,
|
configureReanimatedLogger,
|
||||||
ReanimatedLogLevel,
|
ReanimatedLogLevel,
|
||||||
@ -13,7 +12,6 @@ import { TitleBar } from "./components/Menu/TitleBar";
|
|||||||
import { FixedContentBottom } from "./components/Menu/FixedContentBottom";
|
import { FixedContentBottom } from "./components/Menu/FixedContentBottom";
|
||||||
|
|
||||||
import { lineList } from "./lib/getStationList";
|
import { lineList } from "./lib/getStationList";
|
||||||
import useInterval from "./lib/useInterval";
|
|
||||||
import { useFavoriteStation } from "./stateBox/useFavoriteStation";
|
import { useFavoriteStation } from "./stateBox/useFavoriteStation";
|
||||||
import { useNavigation } from "@react-navigation/native";
|
import { useNavigation } from "@react-navigation/native";
|
||||||
import { useStationList } from "./stateBox/useStationList";
|
import { useStationList } from "./stateBox/useStationList";
|
||||||
@ -24,6 +22,7 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|||||||
import { useBottomTabBarHeight } from "@react-navigation/bottom-tabs";
|
import { useBottomTabBarHeight } from "@react-navigation/bottom-tabs";
|
||||||
import { CarouselBox } from "./components/Menu/Carousel/CarouselBox";
|
import { CarouselBox } from "./components/Menu/Carousel/CarouselBox";
|
||||||
import { CarouselTypeChanger } from "./components/Menu/Carousel/CarouselTypeChanger";
|
import { CarouselTypeChanger } from "./components/Menu/Carousel/CarouselTypeChanger";
|
||||||
|
import { useUserPosition } from "./stateBox/useUserPosition";
|
||||||
configureReanimatedLogger({
|
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
|
||||||
@ -54,24 +53,8 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
|
|||||||
}, 10);
|
}, 10);
|
||||||
}, []);
|
}, []);
|
||||||
//位置情報
|
//位置情報
|
||||||
const [locationStatus, setLocationStatus] = useState(null);
|
const { position, locationStatus } = useUserPosition();
|
||||||
useEffect(() => {
|
|
||||||
if (Platform.OS == "web") return;
|
|
||||||
Location.requestForegroundPermissionsAsync().then((data) => {
|
|
||||||
setLocationStatus(
|
|
||||||
Platform.OS == "ios"
|
|
||||||
? data.status == "granted"
|
|
||||||
: data.android.accuracy == "fine"
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
const [position, setPosition] = useState(undefined);
|
|
||||||
const getCurrentPosition = () => {
|
|
||||||
if (!locationStatus) return () => {};
|
|
||||||
Location.getCurrentPositionAsync({}).then((location) =>
|
|
||||||
setPosition(location)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!position) return () => {};
|
if (!position) return () => {};
|
||||||
makeCurrentStation(position);
|
makeCurrentStation(position);
|
||||||
@ -112,9 +95,6 @@ export default function Menu({ getCurrentTrain, scrollRef }) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(getCurrentPosition, [locationStatus]);
|
|
||||||
useInterval(getCurrentPosition, 5000);
|
|
||||||
|
|
||||||
const [currentStation, setCurrentStation] = useState(undefined); //第三要素
|
const [currentStation, setCurrentStation] = useState(undefined); //第三要素
|
||||||
|
|
||||||
const carouselRef = useRef();
|
const carouselRef = useRef();
|
||||||
|
68
stateBox/useTopMenu.tsx
Normal file
68
stateBox/useTopMenu.tsx
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import React, {
|
||||||
|
createContext,
|
||||||
|
useContext,
|
||||||
|
useState,
|
||||||
|
useEffect,
|
||||||
|
FC,
|
||||||
|
} from "react";
|
||||||
|
import { lineList, getStationList } from "../lib/getStationList";
|
||||||
|
|
||||||
|
type initialStateType = {
|
||||||
|
originalStationList: any[][];
|
||||||
|
setOriginalStationList: React.Dispatch<React.SetStateAction<any[]>>;
|
||||||
|
getStationData: (id: string) => void;
|
||||||
|
stationList: any[];
|
||||||
|
};
|
||||||
|
const initialState = {
|
||||||
|
originalStationList: [[]],
|
||||||
|
setOriginalStationList: () => {},
|
||||||
|
getStationData: () => {},
|
||||||
|
stationList: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
const TopMenuContext = createContext<initialStateType>(initialState);
|
||||||
|
type Props = {
|
||||||
|
children: React.ReactNode;
|
||||||
|
};
|
||||||
|
export const useTopMenu = () => {
|
||||||
|
return useContext(TopMenuContext);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const TopMenuProvider: FC<Props> = ({ children }) => {
|
||||||
|
const [originalStationList, setOriginalStationList] = useState<any[]>([]);
|
||||||
|
useEffect(() => {
|
||||||
|
getStationList().then(setOriginalStationList);
|
||||||
|
}, []);
|
||||||
|
const getStationData: (name: string) => void = (name) => {
|
||||||
|
const returnArray = [];
|
||||||
|
Object.keys(originalStationList).forEach((key) => {
|
||||||
|
originalStationList[key].forEach((station) => {
|
||||||
|
if (station.Station_JP === name) {
|
||||||
|
if (!!station.jslodApi) returnArray.push(station);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return returnArray;
|
||||||
|
};
|
||||||
|
const [stationList, setStationList] = useState<any[][]>([[]]);
|
||||||
|
useEffect(()=>{
|
||||||
|
if(originalStationList.length === 0) return;
|
||||||
|
const stationList =
|
||||||
|
originalStationList &&
|
||||||
|
lineList.map((d) =>
|
||||||
|
originalStationList[d].map((a) => ({
|
||||||
|
StationNumber: a.StationNumber,
|
||||||
|
StationName: a.Station_JP,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
setStationList(stationList)
|
||||||
|
},[originalStationList])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TopMenuContext.Provider
|
||||||
|
value={{ originalStationList, setOriginalStationList, getStationData, stationList }}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</TopMenuContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
76
stateBox/useUserPosition.tsx
Normal file
76
stateBox/useUserPosition.tsx
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import React, {
|
||||||
|
createContext,
|
||||||
|
useContext,
|
||||||
|
useState,
|
||||||
|
useEffect,
|
||||||
|
FC,
|
||||||
|
} from "react";
|
||||||
|
import {
|
||||||
|
LocationObject,
|
||||||
|
requestForegroundPermissionsAsync,
|
||||||
|
getCurrentPositionAsync,
|
||||||
|
} from "expo-location";
|
||||||
|
import { Platform } from "react-native";
|
||||||
|
import useInterval from "@/lib/useInterval";
|
||||||
|
|
||||||
|
type initialStateType = {
|
||||||
|
position: LocationObject | undefined;
|
||||||
|
getCurrentPosition: () => void;
|
||||||
|
locationStatus: boolean | null;
|
||||||
|
getLocationPermission: () => void;
|
||||||
|
};
|
||||||
|
const initialState = {
|
||||||
|
position: undefined,
|
||||||
|
getCurrentPosition: () => {},
|
||||||
|
locationStatus: null,
|
||||||
|
getLocationPermission: () => {},
|
||||||
|
};
|
||||||
|
|
||||||
|
const UserPositionContext = createContext<initialStateType>(initialState);
|
||||||
|
type Props = {
|
||||||
|
children: React.ReactNode;
|
||||||
|
};
|
||||||
|
export const useUserPosition = () => {
|
||||||
|
return useContext(UserPositionContext);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const UserPositionProvider: FC<Props> = ({ children }) => {
|
||||||
|
//位置情報
|
||||||
|
const [locationStatus, setLocationStatus] = useState<boolean | null>(null);
|
||||||
|
const [position, setPosition] = useState<LocationObject | undefined>(
|
||||||
|
undefined
|
||||||
|
);
|
||||||
|
|
||||||
|
const getLocationPermission = async () => {
|
||||||
|
return requestForegroundPermissionsAsync().then((data) => {
|
||||||
|
setLocationStatus(
|
||||||
|
Platform.OS == "ios"
|
||||||
|
? data.status == "granted"
|
||||||
|
: data.android.accuracy == "fine"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const getCurrentPosition = () => {
|
||||||
|
if (!locationStatus) return () => {};
|
||||||
|
getCurrentPositionAsync({}).then((location) => setPosition(location));
|
||||||
|
};
|
||||||
|
useEffect(() => {
|
||||||
|
if (Platform.OS == "web") return;
|
||||||
|
getLocationPermission();
|
||||||
|
}, []);
|
||||||
|
useEffect(getCurrentPosition, [locationStatus]);
|
||||||
|
useInterval(getCurrentPosition, 5000);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<UserPositionContext.Provider
|
||||||
|
value={{
|
||||||
|
position,
|
||||||
|
getCurrentPosition,
|
||||||
|
locationStatus,
|
||||||
|
getLocationPermission,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</UserPositionContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user