early draft of loading session via url scheme

This commit is contained in:
Bruno Windels 2020-10-08 18:23:37 +02:00
parent 087aff4ef4
commit 00bd4364f0
2 changed files with 53 additions and 22 deletions

View File

@ -15,6 +15,7 @@ limitations under the License.
*/ */
import {SessionViewModel} from "./session/SessionViewModel.js"; import {SessionViewModel} from "./session/SessionViewModel.js";
import {SessionLoadViewModel} from "./SessionLoadViewModel.js";
import {LoginViewModel} from "./LoginViewModel.js"; import {LoginViewModel} from "./LoginViewModel.js";
import {SessionPickerViewModel} from "./SessionPickerViewModel.js"; import {SessionPickerViewModel} from "./SessionPickerViewModel.js";
import {ViewModel} from "./ViewModel.js"; import {ViewModel} from "./ViewModel.js";
@ -35,25 +36,41 @@ export class BrawlViewModel extends ViewModel {
this._sessionContainer = null; this._sessionContainer = null;
this._sessionCallback = this._sessionCallback.bind(this); this._sessionCallback = this._sessionCallback.bind(this);
this.track(this.navigation.observe("login").subscribe(value => {
if (value) {
this._showLogin();
}
}));
this.track(this.navigation.observe("session").subscribe(value => {
if (value === true) {
this._showPicker();
} else if (value) {
alert("showing session " + value);
}
}));
} }
async load() { async load() {
if (await this._sessionInfoStorage.hasAnySession()) { this.track(this.navigation.observe("login").subscribe(shown => {
this._showPicker(); if (shown) {
} else { this._showLogin();
}
}));
this.track(this.navigation.observe("session").subscribe(sessionId => {
if (sessionId === true) {
this._showPicker();
} else if (sessionId) {
this._showSessionLoader(sessionId);
}
}));
const isLogin = this.navigation.observe("login").get();
const sessionId = this.navigation.observe("session").get();
if (isLogin) {
this._showLogin(); this._showLogin();
} else if (sessionId === true) {
this._showPicker();
} else if (sessionId) {
this._showSessionLoader(sessionId);
} else {
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);
} }
} }
@ -97,8 +114,21 @@ export class BrawlViewModel extends ViewModel {
createSessionContainer: this._createSessionContainer, createSessionContainer: this._createSessionContainer,
sessionCallback: this._sessionCallback, sessionCallback: this._sessionCallback,
}); });
}) });
}
_showSessionLoader(sessionId) {
this._setSection(() => {
this._sessionLoadViewModel = new SessionLoadViewModel({
createAndStartSessionContainer: () => {
const sessionContainer = this._createSessionContainer();
sessionContainer.startWithExistingSession(sessionId);
return sessionContainer;
},
sessionCallback: sessionContainer => this._sessionCallback(sessionContainer)
});
this._sessionLoadViewModel.start();
});
} }
get activeSection() { get activeSection() {
@ -108,8 +138,12 @@ export class BrawlViewModel extends ViewModel {
return "session"; return "session";
} else if (this._loginViewModel) { } else if (this._loginViewModel) {
return "login"; return "login";
} else { } else if (this._sessionPickerViewModel) {
return "picker"; return "picker";
} else if (this._sessionLoadViewModel) {
return "loading";
} else {
return "redirecting";
} }
} }
@ -119,6 +153,7 @@ export class BrawlViewModel extends ViewModel {
this._sessionViewModel = null; this._sessionViewModel = null;
this._loginViewModel = null; this._loginViewModel = null;
this._sessionPickerViewModel = null; this._sessionPickerViewModel = null;
this._sessionLoadViewModel = null;
if (this._sessionContainer) { if (this._sessionContainer) {
this._sessionContainer.stop(); this._sessionContainer.stop();
@ -133,4 +168,5 @@ export class BrawlViewModel extends ViewModel {
get sessionViewModel() { return this._sessionViewModel; } get sessionViewModel() { return this._sessionViewModel; }
get loginViewModel() { return this._loginViewModel; } get loginViewModel() { return this._loginViewModel; }
get sessionPickerViewModel() { return this._sessionPickerViewModel; } get sessionPickerViewModel() { return this._sessionPickerViewModel; }
get sessionLoadViewModel() { return this._sessionLoadViewModel; }
} }

View File

@ -30,11 +30,6 @@ export class SessionInfoStorage {
return Promise.resolve([]); return Promise.resolve([]);
} }
async hasAnySession() {
const all = await this.getAll();
return all && all.length > 0;
}
async updateLastUsed(id, timestamp) { async updateLastUsed(id, timestamp) {
const sessions = await this.getAll(); const sessions = await this.getAll();
if (sessions) { if (sessions) {