refactor: 应用参考工程代码模式,优化启动流程和架构
- 新增 SplashPage:启动页,2秒后跳转到 Index - 新增 MainPage:独立主页组件,含 Tab 导航 + 宽屏/窄屏自适应 - 简化 Index.ets:纯路由入口,初始化数据库,加载 MainPage - EntryAbility:加载 SplashPage 而非直接加载 Index - 新增 PageContext:NavPathStack 路由管理(参考工程模式) - 新增 BreakpointSystem:响应式断点系统 xs/sm/md/lg/xl - 新增 BaseViewModel:ViewModel 基类,含 attach/detach/dispose 生命周期 - 更新 common/Index.ets 导出新模块 - 更新 main_pages.json 注册 SplashPage 和 MainPage
This commit is contained in:
@ -3,3 +3,6 @@ export { GraphDatabase, RecallEntity, TimeRangeParams } from './src/main/ets/mod
|
||||
export { GraphMemoryService } from './src/main/ets/service/GraphMemoryService';
|
||||
export { AIAgentService, ChatMessage, AgentResponse } from './src/main/ets/service/AIAgentService';
|
||||
export { ImmersiveTabNavigation } from './src/main/ets/component/ImmersiveTabNavigation';
|
||||
export { PageContext, RouterParam, IPageContext } from './src/main/ets/routermanager/PageContext';
|
||||
export { BreakpointType, BreakpointTypes, WidthBreakpoint } from './src/main/ets/util/BreakpointSystem';
|
||||
export { BaseViewModel, VMEvent } from './src/main/ets/viewmodel/BaseViewModel';
|
||||
|
||||
54
common/src/main/ets/routermanager/PageContext.ets
Normal file
54
common/src/main/ets/routermanager/PageContext.ets
Normal file
@ -0,0 +1,54 @@
|
||||
export interface RouterParam {
|
||||
routerName: string;
|
||||
param?: object;
|
||||
}
|
||||
|
||||
export interface IPageContext {
|
||||
openPage(data: RouterParam, animated?: boolean): void;
|
||||
popPage(animated?: boolean): void;
|
||||
replacePage(data: RouterParam, animated?: boolean): void;
|
||||
}
|
||||
|
||||
export class PageContext implements IPageContext {
|
||||
private readonly pathStack: NavPathStack;
|
||||
|
||||
constructor() {
|
||||
this.pathStack = new NavPathStack();
|
||||
}
|
||||
|
||||
public get navPathStack(): NavPathStack {
|
||||
return this.pathStack;
|
||||
}
|
||||
|
||||
public replacePage(data: RouterParam, animated: boolean = true): void {
|
||||
try {
|
||||
this.pathStack.replacePath({ name: data.routerName, param: data.param }, animated);
|
||||
} catch (err) {
|
||||
console.error('Open Page ' + data.routerName + ' failed. ' + err.code + ' ' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
public openPage(data: RouterParam, animated: boolean = true): void {
|
||||
try {
|
||||
this.pathStack.pushPath({ name: data.routerName, param: data.param }, animated);
|
||||
} catch (err) {
|
||||
console.error('Open Page ' + data.routerName + ' failed. ' + err.code + ' ' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
public popPage(animated: boolean = true): void {
|
||||
try {
|
||||
this.pathStack.pop(animated);
|
||||
} catch (err) {
|
||||
console.error('Pop Page failed. ' + err.code + ' ' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
public popPageByIndex(index: number, animated: boolean = true): void {
|
||||
this.pathStack.popToIndex(index, animated);
|
||||
}
|
||||
|
||||
public clear(animated: boolean = true): void {
|
||||
this.pathStack.clear(animated);
|
||||
}
|
||||
}
|
||||
47
common/src/main/ets/util/BreakpointSystem.ets
Normal file
47
common/src/main/ets/util/BreakpointSystem.ets
Normal file
@ -0,0 +1,47 @@
|
||||
export enum WidthBreakpoint {
|
||||
WIDTH_XS = 'xs',
|
||||
WIDTH_SM = 'sm',
|
||||
WIDTH_MD = 'md',
|
||||
WIDTH_LG = 'lg',
|
||||
WIDTH_XL = 'xl'
|
||||
}
|
||||
|
||||
export interface BreakpointTypes<T> {
|
||||
xs?: T;
|
||||
sm: T;
|
||||
md: T;
|
||||
lg: T;
|
||||
xl?: T;
|
||||
}
|
||||
|
||||
export class BreakpointType<T> {
|
||||
private xs: T;
|
||||
private sm: T;
|
||||
private md: T;
|
||||
private lg: T;
|
||||
private xl: T;
|
||||
|
||||
public constructor(param: BreakpointTypes<T>) {
|
||||
this.xs = param.xs ?? param.sm;
|
||||
this.sm = param.sm;
|
||||
this.md = param.md;
|
||||
this.lg = param.lg;
|
||||
this.xl = param.xl ?? param.lg;
|
||||
}
|
||||
|
||||
public getValue(currentBreakpoint: WidthBreakpoint): T {
|
||||
if (currentBreakpoint === WidthBreakpoint.WIDTH_XS) {
|
||||
return this.xs;
|
||||
}
|
||||
if (currentBreakpoint === WidthBreakpoint.WIDTH_SM) {
|
||||
return this.sm;
|
||||
}
|
||||
if (currentBreakpoint === WidthBreakpoint.WIDTH_MD) {
|
||||
return this.md;
|
||||
}
|
||||
if (currentBreakpoint === WidthBreakpoint.WIDTH_XL) {
|
||||
return this.xl;
|
||||
}
|
||||
return this.lg;
|
||||
}
|
||||
}
|
||||
52
common/src/main/ets/viewmodel/BaseViewModel.ets
Normal file
52
common/src/main/ets/viewmodel/BaseViewModel.ets
Normal file
@ -0,0 +1,52 @@
|
||||
import { BreakpointType, WidthBreakpoint } from '../util/BreakpointSystem';
|
||||
|
||||
export interface VMEvent {
|
||||
}
|
||||
|
||||
export class BaseViewModel {
|
||||
protected isAttached: boolean = false;
|
||||
protected isDisposed: boolean = false;
|
||||
protected currentBreakpoint: WidthBreakpoint = WidthBreakpoint.WIDTH_MD;
|
||||
|
||||
attach(): void {
|
||||
if (this.isAttached) {
|
||||
return;
|
||||
}
|
||||
this.isAttached = true;
|
||||
this.onAttach();
|
||||
}
|
||||
|
||||
detach(): void {
|
||||
if (!this.isAttached) {
|
||||
return;
|
||||
}
|
||||
this.isAttached = false;
|
||||
this.onDetach();
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
if (this.isDisposed) {
|
||||
return;
|
||||
}
|
||||
this.isDisposed = true;
|
||||
this.detach();
|
||||
this.onDispose();
|
||||
}
|
||||
|
||||
protected onAttach(): void {
|
||||
}
|
||||
|
||||
protected onDetach(): void {
|
||||
}
|
||||
|
||||
protected onDispose(): void {
|
||||
}
|
||||
|
||||
public get attached(): boolean {
|
||||
return this.isAttached;
|
||||
}
|
||||
|
||||
public get disposed(): boolean {
|
||||
return this.isDisposed;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user