Files
fruit-pop-googleplay/assets/login/scripts/GuestLoginView.ts
T
2026-11-20 15:13:11 +08:00

41 lines
2.0 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';
@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; }
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;
}
}
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;
}
}