Skip to content
Snippets Groups Projects
Commit 6038b67d authored by Midhun Suresh's avatar Midhun Suresh
Browse files

Remove applySegment, use navigation.push()

If we don't allow segments to nest in allowsChild, Navigation.push()
does exactly what we need.
parent 8aa7ba43
No related branches found
No related tags found
No related merge requests found
......@@ -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();
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 {
......
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() {
......
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");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment