2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-04-20 21:26:39 +02:00
|
|
|
import {SessionViewModel} from "./session/SessionViewModel.js";
|
2020-10-08 18:23:37 +02:00
|
|
|
import {SessionLoadViewModel} from "./SessionLoadViewModel.js";
|
2020-04-20 21:26:39 +02:00
|
|
|
import {LoginViewModel} from "./LoginViewModel.js";
|
|
|
|
import {SessionPickerViewModel} from "./SessionPickerViewModel.js";
|
2020-05-04 19:23:11 +02:00
|
|
|
import {ViewModel} from "./ViewModel.js";
|
2019-07-29 22:39:56 +02:00
|
|
|
|
2020-10-09 09:59:59 +02:00
|
|
|
export class RootViewModel extends ViewModel {
|
2020-05-05 23:14:58 +02:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
const {createSessionContainer, sessionInfoStorage, storageFactory} = options;
|
2020-04-20 23:10:13 +02:00
|
|
|
this._createSessionContainer = createSessionContainer;
|
2020-04-19 19:13:38 +02:00
|
|
|
this._sessionInfoStorage = sessionInfoStorage;
|
2020-04-20 23:10:13 +02:00
|
|
|
this._storageFactory = storageFactory;
|
2019-07-29 22:39:56 +02:00
|
|
|
|
|
|
|
this._error = null;
|
|
|
|
this._sessionViewModel = null;
|
|
|
|
this._loginViewModel = null;
|
|
|
|
this._sessionPickerViewModel = null;
|
2020-04-20 23:10:13 +02:00
|
|
|
|
|
|
|
this._sessionContainer = null;
|
|
|
|
this._sessionCallback = this._sessionCallback.bind(this);
|
2020-10-07 09:40:51 +02:00
|
|
|
|
2020-10-08 18:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2020-10-09 09:56:01 +02:00
|
|
|
// TODO: deduplicate code here
|
2020-10-08 18:23:37 +02:00
|
|
|
this.track(this.navigation.observe("login").subscribe(shown => {
|
|
|
|
if (shown) {
|
2020-10-07 09:40:51 +02:00
|
|
|
this._showLogin();
|
|
|
|
}
|
|
|
|
}));
|
2020-10-08 18:23:37 +02:00
|
|
|
this.track(this.navigation.observe("session").subscribe(sessionId => {
|
|
|
|
if (sessionId === true) {
|
2020-10-07 09:40:51 +02:00
|
|
|
this._showPicker();
|
2020-10-08 18:23:37 +02:00
|
|
|
} else if (sessionId) {
|
|
|
|
this._showSessionLoader(sessionId);
|
2020-10-07 09:40:51 +02:00
|
|
|
}
|
|
|
|
}));
|
2019-07-29 22:39:56 +02:00
|
|
|
|
2020-10-08 18:23:37 +02:00
|
|
|
const isLogin = this.navigation.observe("login").get();
|
|
|
|
const sessionId = this.navigation.observe("session").get();
|
|
|
|
if (isLogin) {
|
|
|
|
this._showLogin();
|
|
|
|
} else if (sessionId === true) {
|
2019-07-29 22:39:56 +02:00
|
|
|
this._showPicker();
|
2020-10-08 18:23:37 +02:00
|
|
|
} else if (sessionId) {
|
|
|
|
this._showSessionLoader(sessionId);
|
2019-07-29 22:39:56 +02:00
|
|
|
} else {
|
2020-10-08 18:23:37 +02:00
|
|
|
const sessionInfos = await this._sessionInfoStorage.getAll();
|
|
|
|
let url;
|
|
|
|
if (sessionInfos.length === 0) {
|
|
|
|
url = this.urlRouter.urlForSegment("login");
|
|
|
|
} else if (sessionInfos.length === 1) {
|
|
|
|
url = this.urlRouter.urlForSegment("session", sessionInfos[0].id);
|
|
|
|
} else {
|
|
|
|
url = this.urlRouter.urlForSegment("session");
|
|
|
|
}
|
|
|
|
this.urlRouter.replaceUrl(url);
|
2019-07-29 22:39:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 23:10:13 +02:00
|
|
|
_sessionCallback(sessionContainer) {
|
|
|
|
if (sessionContainer) {
|
|
|
|
this._setSection(() => {
|
|
|
|
this._sessionContainer = sessionContainer;
|
2020-05-04 19:23:11 +02:00
|
|
|
this._sessionViewModel = new SessionViewModel(this.childOptions({sessionContainer}));
|
2020-05-05 23:16:51 +02:00
|
|
|
this._sessionViewModel.start();
|
2020-04-20 23:10:13 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// switch between picker and login
|
|
|
|
if (this.activeSection === "login") {
|
|
|
|
this._showPicker();
|
|
|
|
} else {
|
|
|
|
this._showLogin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 22:39:56 +02:00
|
|
|
async _showPicker() {
|
2020-04-09 23:19:49 +02:00
|
|
|
this._setSection(() => {
|
|
|
|
this._sessionPickerViewModel = new SessionPickerViewModel({
|
2020-04-19 19:13:38 +02:00
|
|
|
sessionInfoStorage: this._sessionInfoStorage,
|
2020-04-09 23:19:49 +02:00
|
|
|
storageFactory: this._storageFactory,
|
2020-04-20 23:10:13 +02:00
|
|
|
createSessionContainer: this._createSessionContainer,
|
|
|
|
sessionCallback: this._sessionCallback,
|
2020-04-09 23:19:49 +02:00
|
|
|
});
|
2019-07-29 22:39:56 +02:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
await this._sessionPickerViewModel.load();
|
|
|
|
} catch (err) {
|
2020-04-09 23:19:49 +02:00
|
|
|
this._setSection(() => this._error = err);
|
2019-07-29 22:39:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_showLogin() {
|
2020-04-09 23:19:49 +02:00
|
|
|
this._setSection(() => {
|
|
|
|
this._loginViewModel = new LoginViewModel({
|
|
|
|
defaultHomeServer: "https://matrix.org",
|
2020-04-20 23:10:13 +02:00
|
|
|
createSessionContainer: this._createSessionContainer,
|
|
|
|
sessionCallback: this._sessionCallback,
|
2020-04-09 23:19:49 +02:00
|
|
|
});
|
2020-10-08 18:23:37 +02:00
|
|
|
});
|
|
|
|
}
|
2019-07-29 22:39:56 +02:00
|
|
|
|
2020-10-08 18:23:37 +02:00
|
|
|
_showSessionLoader(sessionId) {
|
|
|
|
this._setSection(() => {
|
|
|
|
this._sessionLoadViewModel = new SessionLoadViewModel({
|
|
|
|
createAndStartSessionContainer: () => {
|
|
|
|
const sessionContainer = this._createSessionContainer();
|
|
|
|
sessionContainer.startWithExistingSession(sessionId);
|
|
|
|
return sessionContainer;
|
|
|
|
},
|
2020-10-09 09:56:01 +02:00
|
|
|
sessionCallback: this._sessionCallback
|
2020-10-08 18:23:37 +02:00
|
|
|
});
|
|
|
|
this._sessionLoadViewModel.start();
|
|
|
|
});
|
2019-07-29 22:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get activeSection() {
|
|
|
|
if (this._error) {
|
|
|
|
return "error";
|
|
|
|
} else if (this._sessionViewModel) {
|
|
|
|
return "session";
|
|
|
|
} else if (this._loginViewModel) {
|
|
|
|
return "login";
|
2020-10-08 18:23:37 +02:00
|
|
|
} else if (this._sessionPickerViewModel) {
|
2019-07-29 22:39:56 +02:00
|
|
|
return "picker";
|
2020-10-08 18:23:37 +02:00
|
|
|
} else if (this._sessionLoadViewModel) {
|
|
|
|
return "loading";
|
|
|
|
} else {
|
|
|
|
return "redirecting";
|
2019-07-29 22:39:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 23:19:49 +02:00
|
|
|
_setSection(setter) {
|
|
|
|
// clear all members the activeSection depends on
|
|
|
|
this._error = null;
|
|
|
|
this._sessionViewModel = null;
|
|
|
|
this._loginViewModel = null;
|
|
|
|
this._sessionPickerViewModel = null;
|
2020-10-08 18:23:37 +02:00
|
|
|
this._sessionLoadViewModel = null;
|
2020-04-20 23:10:13 +02:00
|
|
|
|
|
|
|
if (this._sessionContainer) {
|
|
|
|
this._sessionContainer.stop();
|
|
|
|
this._sessionContainer = null;
|
|
|
|
}
|
2020-04-09 23:19:49 +02:00
|
|
|
// now set it again
|
|
|
|
setter();
|
2020-05-04 19:23:11 +02:00
|
|
|
this.emitChange("activeSection");
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 23:10:13 +02:00
|
|
|
get error() { return this._error; }
|
2019-07-29 22:39:56 +02:00
|
|
|
get sessionViewModel() { return this._sessionViewModel; }
|
|
|
|
get loginViewModel() { return this._loginViewModel; }
|
|
|
|
get sessionPickerViewModel() { return this._sessionPickerViewModel; }
|
2020-10-08 18:23:37 +02:00
|
|
|
get sessionLoadViewModel() { return this._sessionLoadViewModel; }
|
2019-07-29 22:39:56 +02:00
|
|
|
}
|