对应服务端 FamilyBook(家书)的鸿蒙客户端首版。本轮只做**界面框架**, 数据全部是演示数据 —— 服务端尚无客户端接口,所有增删改只做入口与样式, 不落任何数据。 ## 骨架 左侧抽屉 + 右侧内容。抽屉两套形态由窗口短边决定(≥600vp 为宽屏): 宽屏常驻侧栏、窄屏滑出层。判据取「短边」而非「宽度」,因为短边在旋转时 不变,比例接近 1:1 的设备横竖屏布局天然一致。 ## 模块(与 Web 端 Module::LIST 一一对应,key 即路由名) 首页、家谱、名册、观影、旅程、日程、资源、任务、纪念日、财务、读书、 导航、笔记;另有通知消息、个人设置两个附加页。 ## 与 Web 端保持一致的几处 · 模块清单唯一来源在 model/Modules.ets,加模块只改那一处; · 模块主题色三档由主色按 HSL 压明度算出,预设色板与 Web 端逐值一致; · 术语沿用 Web 端固定下来的口径(旅程 ⊃ 行程;观影的「影院」不单独占 抽屉位,从模块内设置进); · 页面结构照 Web 端的信息层次(首页信息面板 / 财务概览在账目之前 / 纪念日「即将到来」在顶 / 任务按状态而非按看板)。 ## 编译验证 hvigorw assembleHap --mode module -p product=default → BUILD SUCCESSFUL,0 error / 0 warning(含一次删 build 后的干净重建) 过程中修掉的编译期问题: · Circle().fill() 是 API 26 才有的重载(compatibleSdk 是 24) → 换「方块 + 圆角」画圆,零版本风险; · setWindowBackgroundColor 对 phone/tablet 不可用 → 去掉(页面已铺满同色); · window.getLastWindow() 只有异步版 → 由 Ability 侧同步取窗存 AppStorage; · 颜色类 @Param 声明成 string 导致传 Resource 报错 → 统一改 ResourceColor; · 页面在 pages/<模块>/ 下,公共件 import 相对路径要多退一级。 (首轮编译 95 error / 18 warn,逐条定位到 5 类根因后归零。)
146 lines
5.7 KiB
Plaintext
146 lines
5.7 KiB
Plaintext
import { AbilityConstant, Configuration, ConfigurationConstant, EnvironmentCallback,
|
|
UIAbility, Want } from '@kit.AbilityKit';
|
|
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
import { window } from '@kit.ArkUI';
|
|
|
|
const DOMAIN: number = 0x0000;
|
|
|
|
/**
|
|
* 家书 · 应用入口。
|
|
*
|
|
* 只做三件事:加载主页面、把状态栏/导航条配色对齐主题、跟随系统深浅色重刷。
|
|
*
|
|
* ⚠️ 这里**不申请任何权限**(除 module.json5 里已声明的 INTERNET)。
|
|
* 要权限必须在用户主动点对应功能时再申请 —— 启动即弹窗会被审核驳回。
|
|
*/
|
|
export default class EntryAbility extends UIAbility {
|
|
private mainWindow?: window.Window;
|
|
|
|
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onCreate');
|
|
}
|
|
|
|
onDestroy(): void {
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onDestroy');
|
|
}
|
|
|
|
onWindowStageCreate(windowStage: window.WindowStage): void {
|
|
// ① 先跟随系统深浅色。必须在取窗口之前调用,否则会读到尚未更新的色彩配置。
|
|
try {
|
|
this.context.getApplicationContext()
|
|
.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
|
|
} catch (err) {
|
|
hilog.error(DOMAIN, 'FamilyBook', 'setColorMode failed: %{public}s', JSON.stringify(err));
|
|
}
|
|
|
|
// ② 状态栏 / 导航条配色对齐主题,消除顶部和底部的异色条。
|
|
try {
|
|
const win: window.Window = windowStage.getMainWindowSync();
|
|
this.mainWindow = win;
|
|
// 顺手把窗口引用存进 AppStorage:页面侧要在 aboutToAppear 里**同步**读
|
|
// 避让区(状态栏高度)来铺安全区,而 window.getLastWindow() 只有异步版。
|
|
AppStorage.setOrCreate<window.Window>('fbMainWindow', win);
|
|
this.applySystemBars(win);
|
|
} catch (err) {
|
|
hilog.error(DOMAIN, 'FamilyBook', 'applySystemBars failed: %{public}s', JSON.stringify(err));
|
|
}
|
|
|
|
windowStage.loadContent('pages/Index', (err) => {
|
|
if (err.code) {
|
|
hilog.error(DOMAIN, 'FamilyBook', 'loadContent failed: %{public}s', JSON.stringify(err));
|
|
return;
|
|
}
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'loadContent succeeded');
|
|
});
|
|
|
|
// ③ 系统深浅色切换时重刷系统栏配色。
|
|
// 用 applicationContext 的 environment 回调比 onConfigurationUpdate 更可靠
|
|
// (后者在部分场景不回调,会出现「页面已变深色但状态栏还是浅色」)。
|
|
try {
|
|
const cb: EnvironmentCallback = {
|
|
onConfigurationUpdated: (config: Configuration): void => {
|
|
if (this.mainWindow !== undefined) {
|
|
this.applySystemBars(this.mainWindow);
|
|
}
|
|
},
|
|
onMemoryLevel: (level: AbilityConstant.MemoryLevel): void => {
|
|
}
|
|
};
|
|
this.context.getApplicationContext().on('environment', cb);
|
|
} catch (err) {
|
|
hilog.error(DOMAIN, 'FamilyBook', 'watch environment failed: %{public}s', JSON.stringify(err));
|
|
}
|
|
}
|
|
|
|
/** 兜底:部分设备/场景只回调 UIAbility.onConfigurationUpdate */
|
|
onConfigurationUpdate(): void {
|
|
if (this.mainWindow !== undefined) {
|
|
this.applySystemBars(this.mainWindow);
|
|
}
|
|
}
|
|
|
|
onWindowStageDestroy(): void {
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onWindowStageDestroy');
|
|
}
|
|
|
|
onForeground(): void {
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onForeground');
|
|
}
|
|
|
|
onBackground(): void {
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onBackground');
|
|
}
|
|
|
|
/**
|
|
* 取当前主题下 page_bg 的**真实取值**(经 dark/ 限定符解析)。
|
|
*
|
|
* ⚠️ 必须以「资源系统实际解析出的颜色」为准来刷系统栏 ——
|
|
* 如果直接读 config.colorMode 判断,会在系统切换的那一帧读到旧值,
|
|
* 结果状态栏与页面背景对不上。
|
|
*/
|
|
private themePageBg(): string {
|
|
try {
|
|
const c: number = this.context.resourceManager.getColorSync($r('app.color.page_bg').id) >>> 0;
|
|
return '#' + c.toString(16).padStart(8, '0');
|
|
} catch (err) {
|
|
return '#F7F3EA';
|
|
}
|
|
}
|
|
|
|
/** 按背景亮度决定系统栏前景色(浅底用深字,深底用浅字) */
|
|
private isDarkBg(bg: string): boolean {
|
|
const s: string = bg.startsWith('#') ? bg.substring(1) : bg;
|
|
if (s.length < 6) {
|
|
return false;
|
|
}
|
|
const off: number = s.length >= 8 ? 2 : 0;
|
|
const r: number = parseInt(s.substring(off, off + 2), 16);
|
|
const g: number = parseInt(s.substring(off + 2, off + 4), 16);
|
|
const b: number = parseInt(s.substring(off + 4, off + 6), 16);
|
|
if (isNaN(r) || isNaN(g) || isNaN(b)) {
|
|
return false;
|
|
}
|
|
return (0.2126 * r + 0.7152 * g + 0.0722 * b) < 128;
|
|
}
|
|
|
|
/** 状态栏/导航条底色 = 页面背景色,前景按亮度取反 —— 视觉上与页面连成一片 */
|
|
private applySystemBars(win: window.Window): void {
|
|
const bg: string = this.themePageBg();
|
|
const fg: string = this.isDarkBg(bg) ? '#FFFFFF' : '#2C231D';
|
|
try {
|
|
win.setWindowSystemBarProperties({
|
|
statusBarColor: bg,
|
|
statusBarContentColor: fg,
|
|
navigationBarColor: bg,
|
|
navigationBarContentColor: fg
|
|
});
|
|
} catch (err) {
|
|
hilog.error(DOMAIN, 'FamilyBook', 'setSystemBarProperties failed: %{public}s',
|
|
JSON.stringify(err));
|
|
}
|
|
// ⚠️ 这里**不再调** `win.setWindowBackgroundColor()` —— 该 API 对
|
|
// phone / tablet 不可用(编译器告警 "This API is unavailable to phone, tablet."),
|
|
// 而页面自身(Index.ets 的安全区 Column)已经用同色铺满整屏,
|
|
// 窗口底色这层是多余的。留着只会带来一条无意义的告警。
|
|
}
|
|
} |