Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Fix BackGeoloc init after onboarding (#314)
Browse files Browse the repository at this point in the history
Fix #276 BGGeoloc init after onboarding
  • Loading branch information
celian-rib authored Nov 25, 2022
1 parent 7b8f6c3 commit a4e80bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/screens/Onboarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 12 additions & 6 deletions src/states/BackgroundGolocationContextProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {});
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
};

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a4e80bd

Please sign in to comment.