2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
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-21 00:56:39 +05:30
|
|
|
import {SessionViewModel} from "./session/SessionViewModel.js";
|
|
|
|
import {LoginViewModel} from "./LoginViewModel.js";
|
|
|
|
import {SessionPickerViewModel} from "./SessionPickerViewModel.js";
|
2020-05-04 22:53:11 +05:30
|
|
|
import {ViewModel} from "./ViewModel.js";
|
2019-07-30 02:09:56 +05:30
|
|
|
|
2020-05-04 22:53:11 +05:30
|
|
|
export class BrawlViewModel extends ViewModel {
|
2020-05-06 02:44:58 +05:30
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
const {createSessionContainer, sessionInfoStorage, storageFactory} = options;
|
2020-04-21 02:40:13 +05:30
|
|
|
this._createSessionContainer = createSessionContainer;
|
2020-04-19 22:43:38 +05:30
|
|
|
this._sessionInfoStorage = sessionInfoStorage;
|
2020-04-21 02:40:13 +05:30
|
|
|
this._storageFactory = storageFactory;
|
2019-07-30 02:09:56 +05:30
|
|
|
|
|
|
|
this._error = null;
|
|
|
|
this._sessionViewModel = null;
|
|
|
|
this._loginViewModel = null;
|
|
|
|
this._sessionPickerViewModel = null;
|
2020-04-21 02:40:13 +05:30
|
|
|
|
|
|
|
this._sessionContainer = null;
|
|
|
|
this._sessionCallback = this._sessionCallback.bind(this);
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2020-04-19 22:43:38 +05:30
|
|
|
if (await this._sessionInfoStorage.hasAnySession()) {
|
2019-07-30 02:09:56 +05:30
|
|
|
this._showPicker();
|
|
|
|
} else {
|
|
|
|
this._showLogin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 02:40:13 +05:30
|
|
|
_sessionCallback(sessionContainer) {
|
|
|
|
if (sessionContainer) {
|
|
|
|
this._setSection(() => {
|
|
|
|
this._sessionContainer = sessionContainer;
|
2020-05-04 22:53:11 +05:30
|
|
|
this._sessionViewModel = new SessionViewModel(this.childOptions({sessionContainer}));
|
2020-05-06 02:46:51 +05:30
|
|
|
this._sessionViewModel.start();
|
2020-04-21 02:40:13 +05:30
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// switch between picker and login
|
|
|
|
if (this.activeSection === "login") {
|
|
|
|
this._showPicker();
|
|
|
|
} else {
|
|
|
|
this._showLogin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 02:09:56 +05:30
|
|
|
async _showPicker() {
|
2020-04-10 02:49:49 +05:30
|
|
|
this._setSection(() => {
|
|
|
|
this._sessionPickerViewModel = new SessionPickerViewModel({
|
2020-04-19 22:43:38 +05:30
|
|
|
sessionInfoStorage: this._sessionInfoStorage,
|
2020-04-10 02:49:49 +05:30
|
|
|
storageFactory: this._storageFactory,
|
2020-04-21 02:40:13 +05:30
|
|
|
createSessionContainer: this._createSessionContainer,
|
|
|
|
sessionCallback: this._sessionCallback,
|
2020-04-10 02:49:49 +05:30
|
|
|
});
|
2019-07-30 02:09:56 +05:30
|
|
|
});
|
|
|
|
try {
|
|
|
|
await this._sessionPickerViewModel.load();
|
|
|
|
} catch (err) {
|
2020-04-10 02:49:49 +05:30
|
|
|
this._setSection(() => this._error = err);
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_showLogin() {
|
2020-04-10 02:49:49 +05:30
|
|
|
this._setSection(() => {
|
|
|
|
this._loginViewModel = new LoginViewModel({
|
|
|
|
defaultHomeServer: "https://matrix.org",
|
2020-04-21 02:40:13 +05:30
|
|
|
createSessionContainer: this._createSessionContainer,
|
|
|
|
sessionCallback: this._sessionCallback,
|
2020-04-10 02:49:49 +05:30
|
|
|
});
|
|
|
|
})
|
2019-07-30 02:09:56 +05:30
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
get activeSection() {
|
|
|
|
if (this._error) {
|
|
|
|
return "error";
|
|
|
|
} else if (this._sessionViewModel) {
|
|
|
|
return "session";
|
|
|
|
} else if (this._loginViewModel) {
|
|
|
|
return "login";
|
|
|
|
} else {
|
|
|
|
return "picker";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 02:49:49 +05:30
|
|
|
_setSection(setter) {
|
|
|
|
// clear all members the activeSection depends on
|
|
|
|
this._error = null;
|
|
|
|
this._sessionViewModel = null;
|
|
|
|
this._loginViewModel = null;
|
|
|
|
this._sessionPickerViewModel = null;
|
2020-04-21 02:40:13 +05:30
|
|
|
|
|
|
|
if (this._sessionContainer) {
|
|
|
|
this._sessionContainer.stop();
|
|
|
|
this._sessionContainer = null;
|
|
|
|
}
|
2020-04-10 02:49:49 +05:30
|
|
|
// now set it again
|
|
|
|
setter();
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("activeSection");
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 02:40:13 +05:30
|
|
|
get error() { return this._error; }
|
2019-07-30 02:09:56 +05:30
|
|
|
get sessionViewModel() { return this._sessionViewModel; }
|
|
|
|
get loginViewModel() { return this._loginViewModel; }
|
|
|
|
get sessionPickerViewModel() { return this._sessionPickerViewModel; }
|
|
|
|
}
|