From 2fe74528117e57b5df2021ec3b18673d251f7966 Mon Sep 17 00:00:00 2001 From: evlist Date: Wed, 28 Sep 2022 09:46:57 +0200 Subject: [PATCH] Adding the possibility to delete tracks, moving the import button into the track browser, changing the background geolocation precision to 5m and adding a couple of calls to db.compact() and db.viewCleanup(). --- src/components/map/TracksBrowser.tsx | 27 ++++++++++++++++--- src/components/map/map.tsx | 2 -- src/db/gpx.ts | 40 +++++++++++++++++----------- src/db/index.ts | 3 +++ src/lib/background-geolocation.ts | 2 +- 5 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/components/map/TracksBrowser.tsx b/src/components/map/TracksBrowser.tsx index 335caed..b845730 100644 --- a/src/components/map/TracksBrowser.tsx +++ b/src/components/map/TracksBrowser.tsx @@ -2,16 +2,20 @@ import { IonButton, IonButtons, IonContent, + IonIcon, IonItem, IonList, IonModal, IonTitle, IonToolbar, } from '@ionic/react'; +import { trash } from 'ionicons/icons'; import React, { Fragment, useRef } from 'react'; -import { useFind } from 'react-pouchdb'; +import { useFind, useDB } from 'react-pouchdb'; +import { deleteGps } from '../../db/gpx'; import { enterAnimation, leaveAnimation } from '../../lib/animation'; import phoneRoute from '../../theme/icons/font-gis/svg/routing/uEB08-phone-route-nons.svg'; +import GpxImport from './gpx-import'; const TrackBrowser: React.FC<{}> = () => { const gpxes = useFind({ @@ -20,6 +24,8 @@ const TrackBrowser: React.FC<{}> = () => { }, }); + const db = useDB(); + const modal = useRef(null); const dismiss = () => { @@ -40,15 +46,28 @@ const TrackBrowser: React.FC<{}> = () => { Tracks + dismiss()}>Close {gpxes.map((gpx: any) => { - return - {gpx.gpx.metadata.time} - ; + return ( + + {gpx.gpx.metadata.time} + + { + deleteGps(db, { _id: gpx._id, _rev: gpx._rev }); + }} + color='danger' + > + + + + + ); })} diff --git a/src/components/map/map.tsx b/src/components/map/map.tsx index d586fef..bcdf698 100644 --- a/src/components/map/map.tsx +++ b/src/components/map/map.tsx @@ -19,7 +19,6 @@ import { IonToolbar, } from '@ionic/react'; -import GpxImport from './gpx-import'; import Gpxes from './gpxes'; import GpxRecord from './gpx-record'; import { initDb } from '../../db'; @@ -68,7 +67,6 @@ const Map: react.FC<{}> = (props: {}) => { - diff --git a/src/db/gpx.ts b/src/db/gpx.ts index cf2e00a..5d6aa76 100644 --- a/src/db/gpx.ts +++ b/src/db/gpx.ts @@ -132,6 +132,29 @@ export const saveCurrent = async (db: any) => { } }; +export const deleteGps = async ( + db: any, + gps: { _id: string; _rev: string } +) => { + console.log(`Deleting document ${JSON.stringify(gps)}`); + await db.put({ _deleted: true, ...gps }); + console.log(`done, id: ${gps}`); + const currentTrkpts = await db.find({ + selector: { + type: 'trkpt', + gpx: gps._id, + }, + fields: ['_id', '_rev'], + }); + console.log(`deleteGps - db.find(trkpts) : ${JSON.stringify(currentTrkpts)}`); + const trkpts: { _id: string; _rev: string }[] = currentTrkpts.docs; + for (let j = 0; j < trkpts.length; j++) { + await db.put({ _deleted: true, ...trkpts[j] }); + } + await await db.compact(); + await db.viewCleanup(); +}; + export const deleteCurrent = async (db: any) => { const currents = await db.find({ selector: { @@ -144,21 +167,6 @@ export const deleteCurrent = async (db: any) => { const docs: { _id: string; _rev: string }[] = currents.docs; for (let i = 0; i < docs.length; i++) { console.log(`Deleting document ${JSON.stringify(docs[i])}`); - await db.put({ _deleted: true, ...docs[i] }); - console.log(`done, id: ${docs[i]._id}`); - const currentTrkpts = await db.find({ - selector: { - type: 'trkpt', - gpx: docs[i]._id, - }, - fields: ['_id', '_rev', 'type'], - }); - console.log( - `deleteCurrent - db.find(trkpts) : ${JSON.stringify(currentTrkpts)}` - ); - const trkpts: { _id: string; _rev: string }[] = currentTrkpts.docs; - for (let j = 0; j < trkpts.length; j++) { - await db.put({ _deleted: true, ...trkpts[j] }); - } + await deleteGps(db, docs[i]); } }; diff --git a/src/db/index.ts b/src/db/index.ts index f0a171d..137db6b 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -21,6 +21,9 @@ export const initDb = async (db: any, setDbReady: any) => { } } + await await db.compact(); + await db.viewCleanup(); + // WARNING: defs must use the canonical form and be identical to what will be returned by db.getIndexes const requiredIndexes: any = [ { diff --git a/src/lib/background-geolocation.ts b/src/lib/background-geolocation.ts index 1c46ea9..9d25547 100644 --- a/src/lib/background-geolocation.ts +++ b/src/lib/background-geolocation.ts @@ -32,7 +32,7 @@ const backgroundGeolocationConfig = { // The minimum number of metres between subsequent locations. Defaults // to 0. - distanceFilter: 1, + distanceFilter: 5, }; export const startBackgroundGeolocation = async (db: any) => {