Move unofficial theme logic into UserRelatedState; fix updating of longPreferences

This commit is contained in:
Pieter Vander Vennet 2022-06-21 18:22:09 +02:00
parent c5a6b803ec
commit d0b5163ff0
3 changed files with 67 additions and 48 deletions

View file

@ -12,6 +12,7 @@ import {Changes} from "../Osm/Changes";
import ChangeToElementsActor from "../Actors/ChangeToElementsActor";
import PendingChangesUploader from "../Actors/PendingChangesUploader";
import * as translators from "../../assets/translators.json"
import {post} from "jquery";
/**
* The part of the state which keeps track of user-related stuff, e.g. the OSM-connection,
@ -39,6 +40,8 @@ export default class UserRelatedState extends ElementsState {
public readonly isTranslator : UIEventSource<boolean>;
public readonly installedUserThemes: UIEventSource<string[]>
constructor(layoutToUse: LayoutConfig, options?: { attemptLogin: true | boolean }) {
super(layoutToUse);
@ -116,6 +119,7 @@ export default class UserRelatedState extends ElementsState {
this.InitializeLanguage();
new SelectedElementTagsUpdater(this)
this.installedUserThemes = this.InitInstalledUserThemes();
}
@ -144,5 +148,51 @@ export default class UserRelatedState extends ElementsState {
})
.ping();
}
private InitInstalledUserThemes(): UIEventSource<string[]>{
const prefix = "mapcomplete-unofficial-theme-";
const postfix = "-combined-length"
return this.osmConnection.preferencesHandler.preferences.map(prefs =>
Object.keys(prefs)
.filter(k => k.startsWith(prefix) && k.endsWith(postfix))
.map(k => k.substring(prefix.length, k.length - postfix.length))
)
}
public GetUnofficialTheme(id: string): {
id: string
icon: string,
title: any,
shortDescription: any,
definition?: any,
isOfficial: boolean
} | undefined {
console.log("GETTING UNOFFICIAL THEME")
const pref = this.osmConnection.GetLongPreference("unofficial-theme-"+id)
const str = pref.data
if (str === undefined || str === "undefined" || str === "") {
pref.setData(null)
return undefined
}
try {
const value: {
id: string
icon: string,
title: any,
shortDescription: any,
definition?: any,
isOfficial: boolean
} = JSON.parse(str)
value.isOfficial = false
return value;
} catch (e) {
console.warn("Removing theme " + id + " as it could not be parsed from the preferences; the content is:", str)
pref.setData(null)
return undefined
}
}
}