44 lines
2.2 KiB
TypeScript
44 lines
2.2 KiB
TypeScript
import { _decorator, Button, Component, Label } from 'cc';
|
|
import type { GuestSession } from './GuestContracts';
|
|
import { GuestLoginPresenter, LoginState, LoginViewPort } from './GuestLoginPresenter';
|
|
import { createGuestLogin } from './GuestRuntime';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
/** 场景适配组件,只负责控件绑定与状态展示。 / Scene adapter responsible only for control bindings and rendering state. */
|
|
@ccclass('GuestLoginView')
|
|
export class GuestLoginView extends Component implements LoginViewPort {
|
|
static readonly AUTHENTICATED = 'guest-authenticated';
|
|
static readonly STATE_CHANGED = 'guest-login-state-changed';
|
|
@property({ type: Button }) guestButton: Button | null = null;
|
|
@property({ type: Label }) statusLabel: Label | null = null;
|
|
private presenter: GuestLoginPresenter | null = null;
|
|
private session: GuestSession | null = null;
|
|
get signedIn(): boolean { return this.session !== null; }
|
|
get guestId(): string | null { return this.session?.guestId ?? null; }
|
|
onLoad(): void {
|
|
if (!this.guestButton || !this.statusLabel) return;
|
|
this.presenter = new GuestLoginPresenter(this, createGuestLogin, session => {
|
|
this.session = session;
|
|
// 事件不包含 token,后续流程按需读取受控会话存储。 / Do not expose the token in UI events; future flow can read the session store.
|
|
this.node.emit(GuestLoginView.AUTHENTICATED);
|
|
});
|
|
this.guestButton.node.on(Button.EventType.CLICK, this.submit, this);
|
|
this.render({ phase: 'idle', message: '', enabled: true });
|
|
}
|
|
private submit(): void { void this.presenter?.submit(); }
|
|
render(state: LoginState): void {
|
|
if (this.guestButton) this.guestButton.interactable = state.enabled;
|
|
if (this.statusLabel) {
|
|
this.statusLabel.string = state.message;
|
|
this.statusLabel.node.active = state.message.length > 0;
|
|
}
|
|
this.node.emit(GuestLoginView.STATE_CHANGED, state);
|
|
}
|
|
onDisable(): void { this.presenter?.cancel(); }
|
|
onDestroy(): void {
|
|
this.presenter?.dispose();
|
|
if (this.guestButton?.isValid) this.guestButton.node.off(Button.EventType.CLICK, this.submit, this);
|
|
this.session = null;
|
|
}
|
|
}
|