| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"; | 
					
						
							|  |  |  | import {OsmConnection} from "../Osm/OsmConnection"; | 
					
						
							|  |  |  | import {MangroveIdentity} from "../Web/MangroveReviews"; | 
					
						
							|  |  |  | import {UIEventSource} from "../UIEventSource"; | 
					
						
							|  |  |  | import {QueryParameters} from "../Web/QueryParameters"; | 
					
						
							|  |  |  | import {LocalStorageSource} from "../Web/LocalStorageSource"; | 
					
						
							|  |  |  | import {Utils} from "../../Utils"; | 
					
						
							|  |  |  | import Locale from "../../UI/i18n/Locale"; | 
					
						
							|  |  |  | import ElementsState from "./ElementsState"; | 
					
						
							|  |  |  | import SelectedElementTagsUpdater from "../Actors/SelectedElementTagsUpdater"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * The part of the state which keeps track of user-related stuff, e.g. the OSM-connection, | 
					
						
							|  |  |  |  * which layers they enabled, ... | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export default class UserRelatedState extends ElementsState { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      The user credentials | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public osmConnection: OsmConnection; | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The key for mangrove | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public mangroveIdentity: MangroveIdentity; | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Which layers are enabled in the personal theme | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public favouriteLayers: UIEventSource<string[]>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * WHich other themes the user previously visited | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |     public installedThemes: UIEventSource<{ | 
					
						
							|  |  |  |         id: string, // The id doubles as the URL
 | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  |         icon: string, | 
					
						
							|  |  |  |         title: any, | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |         shortDescription: any | 
					
						
							|  |  |  |     }[]>; | 
					
						
							| 
									
										
										
										
											2021-11-08 02:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |     constructor(layoutToUse: LayoutConfig, options?: { attemptLogin: true | boolean }) { | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         super(layoutToUse); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.osmConnection = new OsmConnection({ | 
					
						
							|  |  |  |             changes: this.changes, | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             dryRun: this.featureSwitchIsTesting, | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |             fakeUser: this.featureSwitchFakeUser.data, | 
					
						
							|  |  |  |             allElements: this.allElements, | 
					
						
							|  |  |  |             oauth_token: QueryParameters.GetQueryParameter( | 
					
						
							|  |  |  |                 "oauth_token", | 
					
						
							|  |  |  |                 undefined, | 
					
						
							|  |  |  |                 "Used to complete the login" | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             layoutName: layoutToUse?.id, | 
					
						
							| 
									
										
										
										
											2021-12-13 02:05:34 +01:00
										 |  |  |             osmConfiguration: <'osm' | 'osm-test'>this.featureSwitchApiURL.data, | 
					
						
							|  |  |  |             attemptLogin: options?.attemptLogin | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.mangroveIdentity = new MangroveIdentity( | 
					
						
							|  |  |  |             this.osmConnection.GetLongPreference("identity", "mangrove") | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (layoutToUse?.hideFromOverview) { | 
					
						
							| 
									
										
										
										
											2021-10-15 19:58:02 +02:00
										 |  |  |             this.osmConnection.isLoggedIn.addCallbackAndRunD(loggedIn => { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 if (loggedIn) { | 
					
						
							| 
									
										
										
										
											2021-10-15 19:58:02 +02:00
										 |  |  |                     this.osmConnection | 
					
						
							|  |  |  |                         .GetPreference("hidden-theme-" + layoutToUse?.id + "-enabled") | 
					
						
							|  |  |  |                         .setData("true"); | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  |         this.installedThemes = this.osmConnection.GetLongPreference("installed-themes").map( | 
					
						
							|  |  |  |             str => { | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |                 if (str === undefined || str === "") { | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  |                     return [] | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |                 try { | 
					
						
							|  |  |  |                     return JSON.parse(str); | 
					
						
							|  |  |  |                 } catch (e) { | 
					
						
							|  |  |  |                     console.warn("Could not parse preference with installed themes due to ", e, "\nThe offending string is", str) | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  |                     return [] | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |             }, [], (installed) => JSON.stringify(installed) | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  |         const self = this; | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (this.layoutToUse?.id?.startsWith("http")) { | 
					
						
							|  |  |  |             this.installedThemes.addCallbackAndRun(currentThemes => { | 
					
						
							|  |  |  |                 if (currentThemes === undefined) { | 
					
						
							|  |  |  |                     // We wait till we are logged in
 | 
					
						
							|  |  |  |                     return | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (self.osmConnection.isLoggedIn.data == false) { | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |                     return; | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |                 if (currentThemes.some(installed => installed.id === this.layoutToUse.id)) { | 
					
						
							|  |  |  |                     // Already added to the 'installed theme' list
 | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |                     return; | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |                 console.log("Current installed themes are", this.installedThemes.data) | 
					
						
							|  |  |  |                 currentThemes.push({ | 
					
						
							|  |  |  |                     id: self.layoutToUse.id, | 
					
						
							|  |  |  |                     icon: self.layoutToUse.icon, | 
					
						
							|  |  |  |                     title: self.layoutToUse.title.translations, | 
					
						
							|  |  |  |                     shortDescription: self.layoutToUse.shortDescription.translations | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |                 self.installedThemes.ping() | 
					
						
							|  |  |  |                 console.log("Registered " + self.layoutToUse.id + " as installed themes") | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         // Important: the favourite layers are initialized _after_ the installed themes, as these might contain an installedTheme
 | 
					
						
							|  |  |  |         this.favouriteLayers = LocalStorageSource.Get("favouriteLayers") | 
					
						
							|  |  |  |             .syncWith(this.osmConnection.GetLongPreference("favouriteLayers")) | 
					
						
							|  |  |  |             .map( | 
					
						
							|  |  |  |                 (str) => Utils.Dedup(str?.split(";")) ?? [], | 
					
						
							|  |  |  |                 [], | 
					
						
							|  |  |  |                 (layers) => Utils.Dedup(layers)?.join(";") | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         this.InitializeLanguage(); | 
					
						
							|  |  |  |         new SelectedElementTagsUpdater(this) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private InitializeLanguage() { | 
					
						
							|  |  |  |         const layoutToUse = this.layoutToUse; | 
					
						
							|  |  |  |         Locale.language.syncWith(this.osmConnection.GetPreference("language")); | 
					
						
							|  |  |  |         Locale.language | 
					
						
							|  |  |  |             .addCallback((currentLanguage) => { | 
					
						
							|  |  |  |                 if (layoutToUse === undefined) { | 
					
						
							|  |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (this.layoutToUse.language.indexOf(currentLanguage) < 0) { | 
					
						
							|  |  |  |                     console.log( | 
					
						
							|  |  |  |                         "Resetting language to", | 
					
						
							|  |  |  |                         layoutToUse.language[0], | 
					
						
							|  |  |  |                         "as", | 
					
						
							|  |  |  |                         currentLanguage, | 
					
						
							|  |  |  |                         " is unsupported" | 
					
						
							|  |  |  |                     ); | 
					
						
							|  |  |  |                     // The current language is not supported -> switch to a supported one
 | 
					
						
							|  |  |  |                     Locale.language.setData(layoutToUse.language[0]); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             .ping(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |