Refactoring to prepare jpg imports

This commit is contained in:
Eric van der Vlist 2023-04-03 14:39:55 +02:00
parent 20bb1387fc
commit 9863efc26a
6 changed files with 23 additions and 23 deletions

View File

@ -1 +0,0 @@
export { default, importUrls } from './GpxImport';

View File

@ -4,11 +4,11 @@ import CloudUploadIcon from '@suid/icons-material/CloudUpload';
// @ts-ignore // @ts-ignore
import css from './GpxImport.module.css'; import css from './Import.module.css';
import Dialog from '../dialog'; import Dialog from '../dialog';
import { useI18n } from '@solid-primitives/i18n'; import { useI18n } from '@solid-primitives/i18n';
import { Box } from '@suid/material'; import { Box } from '@suid/material';
import GpxImportSingleFile from './GpxImportSingleFile'; import ImportSingleFile from './ImportSingleFile';
const [filesToImport, setFilesToImport] = createSignal<FileList | string[]>(); const [filesToImport, setFilesToImport] = createSignal<FileList | string[]>();
@ -48,7 +48,7 @@ const GpxImport: Component = () => {
type='file' type='file'
id='gpx-import' id='gpx-import'
class={css.inputFile} class={css.inputFile}
accept='.gpx' accept='.gpx, .jpg'
multiple={true} multiple={true}
onChange={onChangeHandler} onChange={onChangeHandler}
/> />
@ -65,7 +65,7 @@ const GpxImport: Component = () => {
}} }}
> >
<For each={filesToImport()}> <For each={filesToImport()}>
{(gpxFile) => <GpxImportSingleFile gpxFile={gpxFile} />} {(file) => <ImportSingleFile file={file} />}
</For> </For>
</Box> </Box>
</Dialog> </Dialog>

View File

@ -21,7 +21,7 @@ import {
} from '@capacitor/filesystem'; } from '@capacitor/filesystem';
interface Props { interface Props {
gpxFile: File; file: File;
} }
interface StatsAndGpx { interface StatsAndGpx {
@ -50,7 +50,7 @@ const analyzeGpx = (gpx: Gpx | undefined) => {
return stats; return stats;
}; };
const GpxImportSingleFile: Component<Props> = ({ gpxFile }) => { const ImportSingleFile: Component<Props> = ({ file: file }) => {
const [t] = useI18n(); const [t] = useI18n();
const [statsAndGpx, setStatsAndGpx] = createSignal<StatsAndGpx>(); const [statsAndGpx, setStatsAndGpx] = createSignal<StatsAndGpx>();
const [state, setState] = createSignal('init'); const [state, setState] = createSignal('init');
@ -61,11 +61,11 @@ const GpxImportSingleFile: Component<Props> = ({ gpxFile }) => {
const parseGpx = (content: string) => { const parseGpx = (content: string) => {
const gpx = GPX.parse(content); const gpx = GPX.parse(content);
console.log({ caller: 'GpxImportSingleFile / JSON', gpxFile, gpx }); console.log({ caller: 'GpxImportSingleFile / JSON', gpxFile: file, gpx });
setStatsAndGpx({ gpx, stats: analyzeGpx(gpx) }); setStatsAndGpx({ gpx, stats: analyzeGpx(gpx) });
}; };
if (typeof gpxFile === 'string') { if (typeof file === 'string') {
CapacitorFileSystem.requestPermissions().then((permissionStatus) => { CapacitorFileSystem.requestPermissions().then((permissionStatus) => {
console.log({ console.log({
caller: 'GpxImportSingleFile / content', caller: 'GpxImportSingleFile / content',
@ -73,26 +73,26 @@ const GpxImportSingleFile: Component<Props> = ({ gpxFile }) => {
}); });
}); });
CapacitorFileSystem.readFile({ CapacitorFileSystem.readFile({
path: gpxFile, path: file,
encoding: Encoding.UTF8, encoding: Encoding.UTF8,
}).then((content) => { }).then((content) => {
console.log({ console.log({
caller: 'GpxImportSingleFile / content', caller: 'GpxImportSingleFile / content',
gpxFile, gpxFile: file,
content, content,
}); });
parseGpx(content.data); parseGpx(content.data);
}); });
} else { } else {
const gpxReader = new FileReader(); const gpxReader = new FileReader();
gpxReader.readAsText(gpxFile); gpxReader.readAsText(file);
gpxReader.addEventListener( gpxReader.addEventListener(
'load', 'load',
async () => { async () => {
// this will then display a text gpxfile // this will then display a text gpxfile
console.log({ console.log({
caller: 'GpxImportSingleFile / XML', caller: 'GpxImportSingleFile / XML',
gpxFile, gpxFile: file,
result: gpxReader.result, result: gpxReader.result,
}); });
parseGpx(gpxReader.result); parseGpx(gpxReader.result);
@ -111,23 +111,23 @@ const GpxImportSingleFile: Component<Props> = ({ gpxFile }) => {
id: gpxId(), id: gpxId(),
gpx: statsAndGpx()?.gpx, gpx: statsAndGpx()?.gpx,
tech: tech:
typeof gpxFile === 'string' typeof file === 'string'
? { ? {
importDate: new Date().toISOString(), importDate: new Date().toISOString(),
uri: gpxFile, uri: file,
} }
: { : {
lastModified: new Date(gpxFile.lastModified).toISOString(), lastModified: new Date(file.lastModified).toISOString(),
importDate: new Date().toISOString(), importDate: new Date().toISOString(),
name: gpxFile.name, name: file.name,
size: gpxFile.size, size: file.size,
type: gpxFile.type, type: file.type,
}, },
}, },
}); });
console.log({ console.log({
caller: 'GpxImport / JSON / done', caller: 'GpxImport / JSON / done',
gpxFile, gpxFile: file,
gpx: statsAndGpx()?.gpx, gpx: statsAndGpx()?.gpx,
}); });
setState('done'); setState('done');
@ -168,7 +168,7 @@ const GpxImportSingleFile: Component<Props> = ({ gpxFile }) => {
<CardContent> <CardContent>
<Typography variant='h6'> <Typography variant='h6'>
{t('file')} {t('file')}
{gpxFile.name} {file.name}
</Typography> </Typography>
</CardContent> </CardContent>
<CardContent> <CardContent>
@ -205,4 +205,4 @@ const GpxImportSingleFile: Component<Props> = ({ gpxFile }) => {
); );
}; };
export default GpxImportSingleFile; export default ImportSingleFile;

View File

@ -0,0 +1 @@
export { default, importUrls } from './Import';

View File

@ -22,7 +22,7 @@ import { Style, Icon } from 'ol/style';
import GetLocation, { getCurrentLocation } from '../get-location'; import GetLocation, { getCurrentLocation } from '../get-location';
import ShowLocationIcon from '../get-location/ShowLocationIcon.svg'; import ShowLocationIcon from '../get-location/ShowLocationIcon.svg';
import { Back, Forward } from '../back-forward'; import { Back, Forward } from '../back-forward';
import GpxImport, { importUrls } from '../gpx-import'; import GpxImport, { importUrls } from '../import';
import AllGpxes from '../all-gpxes'; import AllGpxes from '../all-gpxes';
import MapTileProvider, { mapTileProviders } from '../map-tile-provider'; import MapTileProvider, { mapTileProviders } from '../map-tile-provider';
import Interaction from 'ol/interaction/Interaction'; import Interaction from 'ol/interaction/Interaction';