From a154a5aa44b4e47f0526c5214c1633d13693b012 Mon Sep 17 00:00:00 2001 From: evlist Date: Sun, 4 Sep 2022 22:45:20 +0200 Subject: [PATCH] Refactoring (moving geolocation in a library) --- src/components/live-map.tsx | 99 +--------------------------------- src/lib/geolocation.ts | 104 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 97 deletions(-) create mode 100644 src/lib/geolocation.ts diff --git a/src/components/live-map.tsx b/src/components/live-map.tsx index 3de6525..857750d 100644 --- a/src/components/live-map.tsx +++ b/src/components/live-map.tsx @@ -1,15 +1,10 @@ import react, { useState } from 'react'; import { Map, Marker, ZoomControl } from 'pigeon-maps'; -import { registerPlugin } from '@capacitor/core'; -import { BackgroundGeolocationPlugin } from '@capacitor-community/background-geolocation'; import { useDispatch } from 'react-redux'; import { gpxActions } from '../store/gpx'; import GPXOverlay from './gpx-overlay'; - -const BackgroundGeolocation = registerPlugin( - 'BackgroundGeolocation' -); +import { registerBackgroundGeolocation } from '../lib/geolocation'; const LiveMap: react.FC = () => { const initialCenter: [number, number] = [43.57154, 3.94351]; @@ -49,97 +44,7 @@ const LiveMap: react.FC = () => { }, }) ); - - console.log('com.dyomedea.dyomedea LOG', ' - Adding the watcher'); - BackgroundGeolocation.addWatcher( - { - // If the "backgroundMessage" option is defined, the watcher will - // provide location updates whether the app is in the background or the - // foreground. If it is not defined, location updates are only - // guaranteed in the foreground. This is true on both platforms. - - // On Android, a notification must be shown to continue receiving - // location updates in the background. This option specifies the text of - // that notification. - backgroundMessage: 'Cancel to prevent battery drain.', - - // The title of the notification mentioned above. Defaults to "Using - // your location". - backgroundTitle: 'Tracking You.', - - // Whether permissions should be requested from the user automatically, - // if they are not already granted. Defaults to "true". - requestPermissions: true, - - // If "true", stale locations may be delivered while the device - // obtains a GPS fix. You are responsible for checking the "time" - // property. If "false", locations are guaranteed to be up to date. - // Defaults to "false". - stale: false, - - // The minimum number of metres between subsequent locations. Defaults - // to 0. - distanceFilter: 1, - }, - function callback(location, error) { - console.log('com.dyomedea.dyomedea LOG', ' - Callback'); - if (error) { - if (error.code === 'NOT_AUTHORIZED') { - if ( - window.confirm( - 'This app needs your location, ' + - 'but does not have permission.\n\n' + - 'Open settings now?' - ) - ) { - // It can be useful to direct the user to their device's - // settings when location permissions have been denied. The - // plugin provides the 'openSettings' method to do exactly - // this. - BackgroundGeolocation.openSettings(); - } - } - return console.error( - 'com.dyomedea.dyomedea LOG', - ' - error: ', - error - ); - } - console.log(location); - if (location !== undefined) { - setCenter([location.latitude, location.longitude]); - setPosition([location.latitude, location.longitude]); - dispatch( - gpxActions.appendTrkpt({ - trkKey: 'current', - trkpt: { - $: { - lat: location.latitude, - lon: location.longitude, - }, - }, - }) - ); - } - - return console.log( - 'com.dyomedea.dyomedea LOG', - ' - location: ', - location - ); - } - ) - .then(function after_the_watcher_has_been_added(watcher_id) { - // When a watcher is no longer needed, it should be removed by calling - // 'removeWatcher' with an object containing its ID. - console.log('com.dyomedea.dyomedea LOG', ' - Watcher added'); - /*BackgroundGeolocation.removeWatcher({ - id: watcher_id, - }); */ - }) - .catch((reason) => { - console.error('com.dyomedea.dyomedea LOG', ' - reason: ', reason); - }); + registerBackgroundGeolocation(dispatch); setInitialized(true); } diff --git a/src/lib/geolocation.ts b/src/lib/geolocation.ts new file mode 100644 index 0000000..2b679e0 --- /dev/null +++ b/src/lib/geolocation.ts @@ -0,0 +1,104 @@ + + +import { BackgroundGeolocationPlugin } from '@capacitor-community/background-geolocation'; +import { registerPlugin } from '@capacitor/core'; +import { gpxActions } from '../store/gpx'; + + +export const registerBackgroundGeolocation = (dispatch: (arg0: any) => void) => { + const BackgroundGeolocation = registerPlugin( + 'BackgroundGeolocation' + ); + console.log('com.dyomedea.dyomedea LOG', ' - Adding the watcher'); + BackgroundGeolocation.addWatcher( + { + // If the "backgroundMessage" option is defined, the watcher will + // provide location updates whether the app is in the background or the + // foreground. If it is not defined, location updates are only + // guaranteed in the foreground. This is true on both platforms. + + // On Android, a notification must be shown to continue receiving + // location updates in the background. This option specifies the text of + // that notification. + backgroundMessage: 'Cancel to prevent battery drain.', + + // The title of the notification mentioned above. Defaults to "Using + // your location". + backgroundTitle: 'Tracking You.', + + // Whether permissions should be requested from the user automatically, + // if they are not already granted. Defaults to "true". + requestPermissions: true, + + // If "true", stale locations may be delivered while the device + // obtains a GPS fix. You are responsible for checking the "time" + // property. If "false", locations are guaranteed to be up to date. + // Defaults to "false". + stale: false, + + // The minimum number of metres between subsequent locations. Defaults + // to 0. + distanceFilter: 1, + }, + function callback(location, error) { + console.log('com.dyomedea.dyomedea LOG', ' - Callback'); + if (error) { + if (error.code === 'NOT_AUTHORIZED') { + if ( + window.confirm( + 'This app needs your location, ' + + 'but does not have permission.\n\n' + + 'Open settings now?' + ) + ) { + // It can be useful to direct the user to their device's + // settings when location permissions have been denied. The + // plugin provides the 'openSettings' method to do exactly + // this. + BackgroundGeolocation.openSettings(); + } + } + return console.error( + 'com.dyomedea.dyomedea LOG', + ' - error: ', + error + ); + } + console.log(location); + if (location !== undefined) { + //setCenter([location.latitude, location.longitude]); + //setPosition([location.latitude, location.longitude]); + dispatch( + gpxActions.appendTrkpt({ + trkKey: 'current', + trkpt: { + $: { + lat: location.latitude, + lon: location.longitude, + }, + }, + }) + ); + } + + return console.log( + 'com.dyomedea.dyomedea LOG', + ' - location: ', + location + ); + } + ) + .then(function after_the_watcher_has_been_added(watcher_id) { + // When a watcher is no longer needed, it should be removed by calling + // 'removeWatcher' with an object containing its ID. + console.log('com.dyomedea.dyomedea LOG', ' - Watcher added'); + /*BackgroundGeolocation.removeWatcher({ + id: watcher_id, + }); */ + }) + .catch((reason) => { + console.error('com.dyomedea.dyomedea LOG', ' - reason: ', reason); + }); + + }; +