feat(entry): 新增四入口 ViewModel

This commit is contained in:
AndyChen1024
2026-09-11 20:48:04 +08:00
parent 28fc0a1026
commit d9f1893f67
4 changed files with 193 additions and 13 deletions

View File

@@ -0,0 +1,56 @@
import { PlanRecord, PlanType } from '../model/PlanModels';
import { PlanRepository } from '../data/repository/PlanRepository';
import { SessionRepository } from '../data/repository/SessionRepository';
import { ServiceLocator } from '../di/ServiceLocator';
/**
* 计划视图模型:周/月/年类型切换 + 计划数据加载。
*/
@ObservedV2
export class PlanViewModel {
@Trace record: PlanRecord = new PlanRecord();
@Trace selectedType: string = PlanType.WEEKLY;
@Trace loggedIn: boolean = false;
@Trace loading: boolean = false;
@Trace error: string = '';
private readonly repository: PlanRepository;
private readonly session: SessionRepository;
constructor(repository: PlanRepository, session: SessionRepository) {
this.repository = repository;
this.session = session;
}
static create(): PlanViewModel {
return new PlanViewModel(
ServiceLocator.get('planRepository') as PlanRepository,
ServiceLocator.get('sessionRepository') as SessionRepository
);
}
async load(): Promise<void> {
this.loading = true;
this.error = '';
this.loggedIn = this.session.isLoggedIn();
if (!this.loggedIn) {
this.loading = false;
return;
}
try {
this.record = await this.repository.observePlan(this.selectedType);
} catch (err) {
this.error = JSON.stringify(err);
} finally {
this.loading = false;
}
}
async selectType(type: string): Promise<void> {
if (this.selectedType === type) {
return;
}
this.selectedType = type;
await this.load();
}
}