Skip to content

Commit

Permalink
fix: reload improvement
Browse files Browse the repository at this point in the history
compare split position not saved yet
  • Loading branch information
farfromrefug committed Jun 10, 2022
1 parent 4cab82e commit 3ebbb77
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn setup_mbtiles(key: String, path: Option<String>, window: Window) {
let window_ = window.clone();
let mb_tiles_id = format!("{:x}", md5::compute(path.clone().unwrap().as_bytes()));

// println!("mbTilesId {} {}", path.clone().unwrap(), mb_tiles_id);
// println!("setup_mbtiles {} {}", path.clone().unwrap(), mb_tiles_id);
set_mbtiles(
&mb_tiles_id,
path_buf,
Expand Down
103 changes: 67 additions & 36 deletions src/components/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
let mainMapDiv;
let secondaryMapDiv;
let savedZoom;
let savedPosition;
let savedSplitPosition;
// $: console.log('mainFeatures', mainFeatures);
onMount(async () => {
// const styleSrc = await resolve(await resourceDir(), '../resources/styles/streets.json');
Expand Down Expand Up @@ -107,27 +111,33 @@
mapToRefresh.triggerRepaint();
}
async function reloadMBtiles() {
// [mainMap, secondaryMap].forEach(reloadMap);
// clearMaps();
mainSources.forEach((source) => {
removeDataSource('main', source);
invoke('setup_mbtiles', {
key: 'main',
path: source.path,
});
let mainSourcesOld = mainSources.slice();
secondarySourcesToLoadOnMainMapLoad = secondarySources.map((s) => s.path);
savedZoom = mainMap.getZoom();
savedPosition = mainMap.getCenter();
savedSplitPosition = secondarySplit.getPercent();
mainSourcesOld.forEach((source) => {
removeDataSource('main', source, true, false);
});
secondarySources.forEach((source) => {
removeDataSource('secondary', source);
invoke('setup_mbtiles', {
key: 'secondary',
path: source.path,
});
removeDataSource('secondary', source, true, false);
});
mainSourcesOld.forEach((source) => {
setupMBtiles(source.path);
});
}
let secondarySourcesToLoadOnMainMapLoad = localStorage.getItem('currentSecondaryMBtiles')
? [localStorage.getItem('currentSecondaryMBtiles')]
: [];
let hasSources = false;
async function setupMBtiles(filePath, key = 'main') {
try {
const sources = key === 'secondary' ? secondarySources : mainSources;
if (sources.find((s) => s.path === filePath)) {
console.log('setupMBtiles source already loaded', filePath, key);
return;
}
await invoke('setup_mbtiles', {
key,
path: filePath,
Expand Down Expand Up @@ -270,7 +280,8 @@
});
}
async function removeDataSource(key, source) {
async function removeDataSource(key, source, clearIfEmpty = true, canRemoveFromStorage = true) {
console.log('removeDataSource', key, source, clearIfEmpty);
const resultMap = key === 'main' ? mainMap : secondaryMap;
const layers = source.layers;
const layerIds =
Expand All @@ -292,24 +303,28 @@
mainSources.splice(index, 1);
mainSources = mainSources;
}
updateMainSourcesCount();
updateMainSourcesCount(clearIfEmpty);
} else {
const index = secondarySources.findIndex((s) => s.id === source.id);
if (index !== -1) {
secondarySources.splice(index, 1);
secondarySources = secondarySources;
if (secondarySources.length === 0 && clearIfEmpty) {
clearSecondaryMap(canRemoveFromStorage);
}
}
}
}
function updateMainSourcesCount() {
function updateMainSourcesCount(clearIfEmpty = true) {
console.log('updateMainSourcesCount', clearIfEmpty);
hasSources = mainSources.length > 0;
if (hasSources) {
localStorage.setItem('currentMBtiles', mainSources[0].path);
}
if (secondarySources.length) {
localStorage.setItem('currentSecondaryMBtiles', secondarySources[0].path);
}
if (!hasSources) {
if (!hasSources && clearIfEmpty) {
clearMaps();
}
}
Expand Down Expand Up @@ -362,12 +377,21 @@
}
}
async function addVectorMBtiles(resultMap: Map, { key, path, json_url, source_id }, vectorData) {
console.log(
'addVectorMBtiles',
key,
path,
json_url,
!!resultMap,
resultMap && resultMap.loaded()
);
if (!vectorData) {
vectorData = await (await fetch(json_url)).json();
}
vectorData.path = path;
function onMapLoaded() {
console.log('addVectorMBtiles onMapLoaded', key, path, json_url);
// resultMap.addSource('osm', {
// type: 'raster',
// tiles: ['https://a.tile.openstreetmap.org/{z}/{x}/{y}.png'],
Expand Down Expand Up @@ -403,11 +427,7 @@
if (key === 'main') {
mainSources.push(vectorData);
mainSources = mainSources;
const secondaryFile = localStorage.getItem('currentSecondaryMBtiles');
if (secondaryFile && secondaryFile !== 'undefined') {
setupMBtiles(secondaryFile, 'secondary');
}
secondarySourcesToLoadOnMainMapLoad.forEach((s) => setupMBtiles(s, 'secondary'));
} else {
secondarySources.push(vectorData);
secondarySources = secondarySources;
Expand All @@ -428,14 +448,17 @@
let center;
let zoom;
if (key === 'main') {
zoom = sourceData.minzoom + (sourceData.maxzoom - sourceData.minzoom) / 2;
center =
sourceData.center ?? sourceData.bounds
? [
sourceData.bounds[0] + (sourceData.bounds[2] - sourceData.bounds[0]) / 2,
sourceData.bounds[3] + (sourceData.bounds[1] - sourceData.bounds[3]) / 2,
]
: undefined;
zoom = savedZoom
? savedZoom
: sourceData.minzoom + (sourceData.maxzoom - sourceData.minzoom) / 2;
center = savedPosition
? savedPosition
: sourceData.center ?? sourceData.bounds
? [
sourceData.bounds[0] + (sourceData.bounds[2] - sourceData.bounds[0]) / 2,
sourceData.bounds[3] + (sourceData.bounds[1] - sourceData.bounds[3]) / 2,
]
: undefined;
} else {
zoom = mainMap.getZoom();
center = mainMap.getCenter();
Expand Down Expand Up @@ -502,6 +525,7 @@
}
function clearMaps() {
console.log('clearMaps');
try {
if (mainMap) {
mainMap.remove();
Expand All @@ -518,7 +542,8 @@
console.error(err);
}
}
function clearSecondaryMap() {
function clearSecondaryMap(canRemoveFromStorage = true) {
console.log('clearSecondaryMap', canRemoveFromStorage);
if (secondaryMap) {
try {
secondaryMap.remove();
Expand All @@ -528,7 +553,12 @@
secondaryMap = null;
secondaryFeatures = [];
secondarySources = [];
if (compareMap) {
compareMap.remove();
compareMap = null;
}
secondarySplit.setPercent(100);
canRemoveFromStorage && localStorage.removeItem('currentSecondaryMBtiles');
} else {
addMBTiles('secondary');
}
Expand Down Expand Up @@ -565,8 +595,13 @@
// compareMap = null;
// }
if (path) {
const sources = key === 'secondary' ? secondarySources : mainSources;
if (sources.find((s) => s.path === path)) {
console.log('setupMBtiles source already loaded', path, key);
return;
}
if (key === 'main') {
if (!hasSources) {
if (!mainMap) {
mainMap = await createMap({ key, path, json_url, source_id });
} else {
let sourceData = await (await fetch(json_url)).json();
Expand Down Expand Up @@ -648,10 +683,6 @@
function switchCompareView() {
if (compareMap) {
clearSecondaryMap();
if (compareMap) {
compareMap.remove();
compareMap = null;
}
} else {
addMBTiles('secondary');
}
Expand Down

0 comments on commit 3ebbb77

Please sign in to comment.