From 6038b67d0fb3a84be7345ed7af0edf02fb9c21bc Mon Sep 17 00:00:00 2001 From: Midhun Suresh <midhunr@element.io> Date: Wed, 19 Jan 2022 16:55:34 +0530 Subject: [PATCH] Remove applySegment, use navigation.push() If we don't allow segments to nest in allowsChild, Navigation.push() does exactly what we need. --- src/main.ts | 21 ++++++++------------- src/viewmodels/AccountSetupViewModel.ts | 8 +++----- src/viewmodels/ChatterboxViewModel.ts | 6 ++---- src/viewmodels/RootViewModel.ts | 12 ++++-------- 4 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/main.ts b/src/main.ts index 03a9afa..3ce3f16 100644 --- a/src/main.ts +++ b/src/main.ts @@ -26,24 +26,19 @@ async function main() { const navigation = new Navigation(allowsChild); platform.setNavigation(navigation); const urlRouter = createRouter({ navigation, history: platform.history }); - const applySegment = getNavigation(navigation); - const rootViewModel = new RootViewModel(config, {platform, navigation, urlCreator: urlRouter, applySegment}); + const rootViewModel = new RootViewModel(config, {platform, navigation, urlCreator: urlRouter}); const rootView = new RootView(rootViewModel); root.appendChild(rootView.mount()); } function allowsChild(parent, child) { - return true; -} - -function getNavigation(navigation) { - - function applySegment(segment: string, value?: string) { - const s = navigation.segment(segment, value); - const path = navigation.pathFrom([s]); - navigation.applyPath(path); + const { type } = child; + switch (parent?.type) { + case undefined: + return type === "start" || type === "account-setup" || type === "timeline"; + default: + return false; } - - return applySegment; } + main(); diff --git a/src/viewmodels/AccountSetupViewModel.ts b/src/viewmodels/AccountSetupViewModel.ts index 448b79a..8ad243d 100644 --- a/src/viewmodels/AccountSetupViewModel.ts +++ b/src/viewmodels/AccountSetupViewModel.ts @@ -1,4 +1,4 @@ -import { ViewModel, Client, ObservableValue, LoadStatus } from "hydrogen-view-sdk"; +import { ViewModel, Client, LoadStatus } from "hydrogen-view-sdk"; import { IChatterboxConfig } from "../types/IChatterboxConfig"; import { generateRandomString } from "../random"; import "hydrogen-view-sdk/style.css"; @@ -9,13 +9,11 @@ export class AccountSetupViewModel extends ViewModel { private _client: Client; private _termsStage?: any; private _password: string; - private _applySegment: any; constructor(options) { super(options); this._client = options.client; this._config = options.config; - this._applySegment = options.applySegment; this._startRegistration(); } @@ -35,7 +33,7 @@ export class AccountSetupViewModel extends ViewModel { } // stage is username when registration is completed const loginPromise = this.login(stage, this._password); - this._applySegment("timeline", loginPromise); + this.navigation.push("timeline", loginPromise); } async login(username: string, password: string): Promise<void> { @@ -55,7 +53,7 @@ export class AccountSetupViewModel extends ViewModel { } dismiss() { - this._applySegment("start"); + this.navigation.push("start"); } private get _homeserver(): string { diff --git a/src/viewmodels/ChatterboxViewModel.ts b/src/viewmodels/ChatterboxViewModel.ts index 342b4b7..6e4e5ba 100644 --- a/src/viewmodels/ChatterboxViewModel.ts +++ b/src/viewmodels/ChatterboxViewModel.ts @@ -1,16 +1,14 @@ -import { RoomViewModel, ViewModel, TimelineViewModel, ComposerViewModel} from "hydrogen-view-sdk"; +import { RoomViewModel, ViewModel, ComposerViewModel} from "hydrogen-view-sdk"; export class ChatterboxViewModel extends ViewModel { private _messageComposerViewModel?: ComposerViewModel; private _roomViewModel?: RoomViewModel; private _loginPromise: Promise<void>; - private _applySegment: (segment) => void; constructor(options) { super(options); this._client = options.client; this._loginPromise = options.loginPromise; - this._applySegment = options.applySegment; } async loadRoom() { @@ -54,7 +52,7 @@ export class ChatterboxViewModel extends ViewModel { } minimize() { - this._applySegment("start"); + this.navigation.push("start"); } get timelineViewModel() { diff --git a/src/viewmodels/RootViewModel.ts b/src/viewmodels/RootViewModel.ts index fa7a999..d696f56 100644 --- a/src/viewmodels/RootViewModel.ts +++ b/src/viewmodels/RootViewModel.ts @@ -1,10 +1,10 @@ -import { ViewModel, Client, Platform} from "hydrogen-view-sdk"; +import { ViewModel, Client, Navigation, createRouter, Platform } from "hydrogen-view-sdk"; import { IChatterboxConfig } from "../types/IChatterboxConfig"; import { ChatterboxViewModel } from "./ChatterboxViewModel"; import "hydrogen-view-sdk/style.css"; import { AccountSetupViewModel } from "./AccountSetupViewModel"; -type Options = { platform: Platform, navigation: ReturnType<createNavigation>, applySegment: (segment: string, value?: string) => void }; +type Options = { platform: Platform, navigation: ReturnType<Navigation>, urlCreator: ReturnType<createRouter> }; export class RootViewModel extends ViewModel { private _config: IChatterboxConfig; @@ -12,13 +12,11 @@ export class RootViewModel extends ViewModel { private _chatterBoxViewModel?: ChatterboxViewModel; private _accountSetupViewModel?: AccountSetupViewModel; private _activeSection: string = "start"; - private _applySegment: Options["applySegment"]; constructor(config: IChatterboxConfig, options: Options) { super(options); this._config = config; this._client = new Client(this.platform); - this._applySegment = options.applySegment; this._setupNavigation(); } @@ -31,10 +29,10 @@ export class RootViewModel extends ViewModel { async start() { const sessionAlreadyExists = await this.attemptStartWithExistingSession(); if (sessionAlreadyExists) { - this._applySegment("timeline"); + this.navigation.push("timeline"); return; } - this._applySegment("account-setup"); + this.navigation.push("account-setup"); } private async _showTimeline(loginPromise: Promise<void>) { @@ -45,7 +43,6 @@ export class RootViewModel extends ViewModel { client: this._client, config: this._config, state: this._state, - applySegment: this._applySegment, loginPromise, }) )); @@ -61,7 +58,6 @@ export class RootViewModel extends ViewModel { client: this._client, config: this._config, state: this._state, - applySegment: this._applySegment, }) )); this.emitChange("activeSection"); -- GitLab