import React, { Fragment, useRef, useState } from 'react'; import { useDB, useFind } from 'react-pouchdb'; import '../../theme/get-location.css'; import { IonButton, IonButtons, IonContent, IonIcon, IonModal, IonTitle, IonToolbar, } from '@ionic/react'; import { recordingOutline, recording, closeCircle, pauseCircle, stop, } from 'ionicons/icons'; import { startBackgroundGeolocation, stopBackgroundGeolocation, } from '../../lib/background-geolocation'; import { appendTrkpt, deleteCurrent, saveCurrent } from '../../db/gpx'; import { enterAnimation, leaveAnimation } from '../../lib/animation'; import { useDispatch, useSelector } from 'react-redux'; import { mapActions } from '../../store/map'; import { SettingsState } from '../../store/settings'; import i18n, { setI18nLanguage } from '../../i18n/index'; declare global { var $lastValidLocationTime: number; } const GpxRecord: React.FC<{}> = () => { const language = useSelector( (state: { settings: SettingsState }) => state.settings.language ); setI18nLanguage(language); const db = useDB(); const [isRecording, setIsRecording] = useState(false); const [watcher_id, setWatcher_id] = useState(); const geolocationsSettingsState = useSelector( (state: { settings: SettingsState }) => state.settings.geolocation ); const gpxes = useFind({ selector: { type: 'gpx', subtype: 'current', }, }); const hasCurrentTrack = gpxes.length > 0; console.log( `GpxRecord, hasCurrentTrack:${hasCurrentTrack}, gpxes: ${JSON.stringify( gpxes )}` ); const modal = useRef(null); const dismiss = () => { modal.current?.dismiss(); }; const dispatch = useDispatch(); const newLocationHandler = (location: any) => { console.log( `Location filtering, elapsed time: ${ location.time - globalThis.$lastValidLocationTime }` ); if ( location.time - globalThis.$lastValidLocationTime > geolocationsSettingsState.minTimeInterval ) { globalThis.$lastValidLocationTime = location.time; appendTrkpt(db, { $: { lat: location.latitude, lon: location.longitude, }, ele: location.altitude, time: new Date(location.time).toISOString(), extensions: { speed: location.speed, accuracy: location.accuracy, }, }); dispatch( mapActions.setCurrent({ lat: location.latitude, lon: location.longitude, }) ); } }; const startRecording = () => { globalThis.$lastValidLocationTime = 0; startBackgroundGeolocation( newLocationHandler, geolocationsSettingsState.minDistance ).then((result) => { setWatcher_id(result); }); setIsRecording(true); dismiss(); }; const pauseRecording = () => { if (isRecording) { stopBackgroundGeolocation(watcher_id); } setIsRecording(false); dismiss(); }; const stopRecording = () => { saveCurrent(db); pauseRecording(); }; const deleteRecording = () => { deleteCurrent(db); pauseRecording(); }; return ( {isRecording && ( )} {!isRecording && } {i18n.trackRecording} dismiss()}>{i18n.close} {!isRecording && ( {hasCurrentTrack ? ( {i18n.resumeRecording} ) : ( {i18n.startRecording} )} )} {isRecording && ( {i18n.pauseRecording} )} {hasCurrentTrack && ( <> {i18n.stopRecording} {i18n.cancelRecording} )} ); }; export default GpxRecord;