forked from MapComplete/MapComplete
Remove unmaintained preferences page, re-add earlier visited installed themes (only remove themes)
This commit is contained in:
parent
4b6769d601
commit
7fe79600fb
6 changed files with 78 additions and 282 deletions
|
@ -1,65 +0,0 @@
|
|||
import {UIEventSource} from "../UIEventSource";
|
||||
import {OsmConnection} from "../Osm/OsmConnection";
|
||||
import {Utils} from "../../Utils";
|
||||
import LZString from "lz-string";
|
||||
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig";
|
||||
|
||||
/**
|
||||
* Gives an overview of themes that are installed in the user preferences
|
||||
*/
|
||||
export default class InstalledThemes {
|
||||
public installedThemes: UIEventSource<{ layout: LayoutConfig; definition: string }[]>;
|
||||
|
||||
|
||||
constructor(osmConnection: OsmConnection) {
|
||||
this.installedThemes = osmConnection.preferencesHandler.preferences.map<{ layout: LayoutConfig, definition: string }[]>(allPreferences => {
|
||||
const installedThemes: { layout: LayoutConfig, definition: string }[] = [];
|
||||
if (allPreferences === undefined) {
|
||||
console.log("All prefs is undefined");
|
||||
return installedThemes;
|
||||
}
|
||||
const invalidThemes = []
|
||||
for (const allPreferencesKey in allPreferences) {
|
||||
const themename = allPreferencesKey.match(/^mapcomplete-installed-theme-(.*)-combined-length$/);
|
||||
if (themename && themename[1] !== "") {
|
||||
const customLayout = osmConnection.GetLongPreference("installed-theme-" + themename[1]);
|
||||
if (customLayout.data === undefined) {
|
||||
console.log("No data defined for ", themename[1]);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
let layoutJson;
|
||||
try {
|
||||
layoutJson = JSON.parse(atob(customLayout.data))
|
||||
} catch (e) {
|
||||
layoutJson = JSON.parse(Utils.UnMinify(LZString.decompressFromBase64(customLayout.data)))
|
||||
}
|
||||
const layout = new LayoutConfig(layoutJson, false);
|
||||
installedThemes.push({
|
||||
layout: layout,
|
||||
definition: customLayout.data
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn("Could not parse custom layout from preferences - deleting: ", allPreferencesKey, e, customLayout.data);
|
||||
invalidThemes.push(themename[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
InstalledThemes.DeleteInvalid(osmConnection, invalidThemes);
|
||||
|
||||
return installedThemes;
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
private static DeleteInvalid(osmConnection: OsmConnection, invalidThemes: any[]) {
|
||||
for (const invalid of invalidThemes) {
|
||||
console.error("Attempting to remove ", invalid)
|
||||
osmConnection.GetLongPreference(
|
||||
"installed-theme-" + invalid
|
||||
).setData(null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -14,6 +14,7 @@ import {FixLegacyTheme, PrepareTheme} from "../Models/ThemeConfig/LegacyJsonConv
|
|||
import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson";
|
||||
import SharedTagRenderings from "../Customizations/SharedTagRenderings";
|
||||
import * as known_layers from "../assets/generated/known_layers.json"
|
||||
import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson";
|
||||
|
||||
export default class DetermineLayout {
|
||||
|
||||
|
@ -62,6 +63,22 @@ export default class DetermineLayout {
|
|||
return layoutToUse
|
||||
}
|
||||
|
||||
private static prepCustomTheme(json: any): LayoutConfigJson{
|
||||
const knownLayersDict = new Map<string, LayerConfigJson>()
|
||||
for (const key in known_layers["default"]) {
|
||||
const layer = known_layers["default"][key]
|
||||
knownLayersDict.set(layer.id, layer)
|
||||
}
|
||||
const converState = {
|
||||
tagRenderings: SharedTagRenderings.SharedTagRenderingJson,
|
||||
sharedLayers: knownLayersDict
|
||||
}
|
||||
json = new FixLegacyTheme().convertStrict(converState, json, "While loading a dynamic theme")
|
||||
json = new PrepareTheme().convertStrict(converState, json, "While preparing a dynamic theme")
|
||||
console.log("The layoutconfig is ", json)
|
||||
return json
|
||||
}
|
||||
|
||||
public static LoadLayoutFromHash(
|
||||
userLayoutParam: UIEventSource<string>
|
||||
): LayoutConfig | null {
|
||||
|
@ -102,25 +119,9 @@ export default class DetermineLayout {
|
|||
}
|
||||
}
|
||||
|
||||
const knownLayersDict = new Map<string, LayerConfigJson>()
|
||||
for (const key in known_layers["default"]) {
|
||||
const layer = known_layers["default"][key]
|
||||
console.log("Found shared layer "+layer.id)
|
||||
knownLayersDict.set(layer.id, layer)
|
||||
}
|
||||
|
||||
const converState = {
|
||||
tagRenderings: SharedTagRenderings.SharedTagRenderingJson,
|
||||
sharedLayers: knownLayersDict
|
||||
}
|
||||
|
||||
json = new FixLegacyTheme().convertStrict(converState, json, "While loading a dynamic theme")
|
||||
|
||||
json = new PrepareTheme().convertStrict(converState, json, "While preparing a dynamic theme")
|
||||
|
||||
const layoutToUse = new LayoutConfig(json, false);
|
||||
const layoutToUse = DetermineLayout.prepCustomTheme(json)
|
||||
userLayoutParam.setData(layoutToUse.id);
|
||||
return layoutToUse;
|
||||
return new LayoutConfig(layoutToUse, false);
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
if (hash === undefined || hash.length < 10) {
|
||||
|
@ -160,9 +161,14 @@ export default class DetermineLayout {
|
|||
tagRenderings: SharedTagRenderings.SharedTagRenderingJson,
|
||||
sharedLayers: new Map<string, LayerConfigJson>() // FIXME: actually add the layers
|
||||
}, parsed, "While loading a dynamic theme")
|
||||
try {
|
||||
|
||||
|
||||
parsed.id = link;
|
||||
return new LayoutConfig(parsed, false).patchImages(link, JSON.stringify(parsed));
|
||||
|
||||
|
||||
try {
|
||||
const layoutToUse = DetermineLayout.prepCustomTheme(parsed)
|
||||
return new LayoutConfig(layoutToUse,false).patchImages(link, JSON.stringify(layoutToUse));
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
DetermineLayout.ShowErrorOnCustomTheme(
|
||||
|
|
|
@ -3,12 +3,12 @@ import {OsmConnection} from "../Osm/OsmConnection";
|
|||
import {MangroveIdentity} from "../Web/MangroveReviews";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {QueryParameters} from "../Web/QueryParameters";
|
||||
import InstalledThemes from "../Actors/InstalledThemes";
|
||||
import {LocalStorageSource} from "../Web/LocalStorageSource";
|
||||
import {Utils} from "../../Utils";
|
||||
import Locale from "../../UI/i18n/Locale";
|
||||
import ElementsState from "./ElementsState";
|
||||
import SelectedElementTagsUpdater from "../Actors/SelectedElementTagsUpdater";
|
||||
import {log} from "util";
|
||||
|
||||
/**
|
||||
* The part of the state which keeps track of user-related stuff, e.g. the OSM-connection,
|
||||
|
@ -33,7 +33,10 @@ export default class UserRelatedState extends ElementsState {
|
|||
/**
|
||||
* WHich other themes the user previously visited
|
||||
*/
|
||||
public installedThemes: UIEventSource<{ layout: LayoutConfig; definition: string }[]>;
|
||||
public installedThemes: UIEventSource<{ id: string, // The id doubles as the URL
|
||||
icon: string,
|
||||
title: any,
|
||||
shortDescription: any}[]>;
|
||||
|
||||
|
||||
constructor(layoutToUse: LayoutConfig, options?:{attemptLogin : true | boolean}) {
|
||||
|
@ -69,9 +72,47 @@ export default class UserRelatedState extends ElementsState {
|
|||
})
|
||||
}
|
||||
|
||||
this.installedThemes = new InstalledThemes(
|
||||
this.osmConnection
|
||||
).installedThemes;
|
||||
this.installedThemes = this.osmConnection.GetLongPreference("installed-themes").map(
|
||||
str => {
|
||||
if(str === undefined || str === ""){
|
||||
return []
|
||||
}
|
||||
try{
|
||||
return JSON.parse(str)
|
||||
}catch(e){
|
||||
console.warn("Could not parse preference with installed themes due to ", e,"\nThe offending string is",str)
|
||||
return []
|
||||
}
|
||||
}, [],(installed => JSON.stringify(installed))
|
||||
)
|
||||
|
||||
|
||||
const self = this;
|
||||
this.osmConnection.isLoggedIn.addCallbackAndRunD(loggedIn => {
|
||||
if(!loggedIn){
|
||||
return
|
||||
}
|
||||
|
||||
if(this.layoutToUse.id.startsWith("http")){
|
||||
if(!this.installedThemes.data.some(installed => installed.id === this.layoutToUse.id)){
|
||||
|
||||
this.installedThemes.data.push({
|
||||
id: this.layoutToUse.id,
|
||||
icon: this.layoutToUse.icon,
|
||||
title: this.layoutToUse.title.translations,
|
||||
shortDescription: this.layoutToUse.shortDescription.translations
|
||||
})
|
||||
}
|
||||
this.installedThemes.ping()
|
||||
console.log("Registered "+this.layoutToUse.id+" as installed themes")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
})
|
||||
|
||||
|
||||
// Important: the favourite layers are initialized _after_ the installed themes, as these might contain an installedTheme
|
||||
this.favouriteLayers = LocalStorageSource.Get("favouriteLayers")
|
||||
|
@ -81,8 +122,7 @@ export default class UserRelatedState extends ElementsState {
|
|||
[],
|
||||
(layers) => Utils.Dedup(layers)?.join(";")
|
||||
);
|
||||
|
||||
|
||||
|
||||
this.InitializeLanguage();
|
||||
new SelectedElementTagsUpdater(this)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue