From a4e80bda59a544fa683887079bb412851aa508fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9lian=20Riboulet?= Date: Fri, 25 Nov 2022 17:48:14 +0100 Subject: [PATCH] Fix BackGeoloc init after onboarding (#314) Fix #276 BGGeoloc init after onboarding --- src/screens/Onboarding.js | 1 - .../BackgroundGolocationContextProvider.js | 18 ++++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/screens/Onboarding.js b/src/screens/Onboarding.js index 21f178dd..6d34b93d 100644 --- a/src/screens/Onboarding.js +++ b/src/screens/Onboarding.js @@ -313,7 +313,6 @@ export default function Onboarding({ navigation }) { const requestBackgroundGeolocationPermissions = async (onSuccess = () => {}) => { setLoading(true); try { - await BackgroundGeolocation.requestPermission(); setBackgroundGeolocationEnabled(true); } catch (error) { console.error(error); diff --git a/src/states/BackgroundGolocationContextProvider.js b/src/states/BackgroundGolocationContextProvider.js index 62085738..b280add1 100644 --- a/src/states/BackgroundGolocationContextProvider.js +++ b/src/states/BackgroundGolocationContextProvider.js @@ -21,9 +21,13 @@ const BackgroundGolocationProvider = ({ children }) => { const [backgroundGeolocationEnabled, _setBackgroundGeolocationEnabled] = useState(false); const [initialized, setInitialized] = useState(false); + const [enableAfterInit, setEnableAfterInit] = useState(false); const [logs, setLogs] = useState(null); useEffect(() => { + if (user == null) + return; + const locationSubscriber = BackgroundGeolocation.onLocation(() => {}, () => {}); const motionChangeSubscriber = BackgroundGeolocation.onMotionChange(() => {}); @@ -53,20 +57,18 @@ const BackgroundGolocationProvider = ({ children }) => { if (!initialized) return; - log(`Background geolocation ${backgroundGeolocationEnabled ? 'enabled' : 'disabled'}`); if (backgroundGeolocationEnabled) BackgroundGeolocation.start(); else BackgroundGeolocation.stop(); - }, [backgroundGeolocationEnabled]); + }, [backgroundGeolocationEnabled, initialized]); const initializeBackgroundGeolocation = async () => { if (initialized === true) return; - const authTokens = await Storage.getItem('@auth_tokens'); if (authTokens == null) { @@ -75,9 +77,10 @@ const BackgroundGolocationProvider = ({ children }) => { } const state = await setupBackgroundGeolocation(authTokens); - _setBackgroundGeolocationEnabled(state.enabled); + const enable = enableAfterInit || state.enabled; + _setBackgroundGeolocationEnabled(enable); - log(`Initialized successfully (started : ${state.enabled})`); + log(`Initialized successfully (started : ${enable})`); setInitialized(true); }; @@ -114,7 +117,10 @@ const BackgroundGolocationProvider = ({ children }) => { try { if (enabled) { await BackgroundGeolocation.requestPermission(); - await initializeBackgroundGeolocation(); + if (user != null) + await initializeBackgroundGeolocation(); + else + setEnableAfterInit(true); } _setBackgroundGeolocationEnabled(enabled); } catch (error) {