57 lines
1.5 KiB
Plaintext
57 lines
1.5 KiB
Plaintext
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();
|
|
}
|
|
}
|