| 
									
										
										
										
											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 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"; | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  | import StaticFeatureSource from "../FeatureSource/Sources/StaticFeatureSource"; | 
					
						
							|  |  |  | import FeatureSource from "../FeatureSource/FeatureSource"; | 
					
						
							| 
									
										
										
										
											2021-11-09 01:49:07 +01:00
										 |  |  | import {Feature} from "@turf/turf"; | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * 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 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public installedThemes: UIEventSource<{ layout: LayoutConfig; definition: string }[]>; | 
					
						
							| 
									
										
										
										
											2021-11-08 02:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constructor(layoutToUse: LayoutConfig) { | 
					
						
							|  |  |  |         super(layoutToUse); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.osmConnection = new OsmConnection({ | 
					
						
							|  |  |  |             changes: this.changes, | 
					
						
							|  |  |  |             dryRun: this.featureSwitchIsTesting.data, | 
					
						
							|  |  |  |             fakeUser: this.featureSwitchFakeUser.data, | 
					
						
							|  |  |  |             allElements: this.allElements, | 
					
						
							|  |  |  |             oauth_token: QueryParameters.GetQueryParameter( | 
					
						
							|  |  |  |                 "oauth_token", | 
					
						
							|  |  |  |                 undefined, | 
					
						
							|  |  |  |                 "Used to complete the login" | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             layoutName: layoutToUse?.id, | 
					
						
							|  |  |  |             osmConfiguration: <'osm' | 'osm-test'>this.featureSwitchApiURL.data | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.installedThemes = new InstalledThemes( | 
					
						
							|  |  |  |             this.osmConnection | 
					
						
							|  |  |  |         ).installedThemes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 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(";") | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |