@buaa_smat/hometrans 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -124
- package/agents/build-fixer.md +1 -0
- package/agents/code-review-fix.md +1 -0
- package/agents/code-reviewer.md +1 -0
- package/agents/logic-coding.md +1 -0
- package/agents/logic-context-builder.md +1 -0
- package/agents/review-fixer.md +1 -0
- package/agents/self-test-fixer.md +1 -0
- package/agents/self-tester.md +260 -233
- package/agents/spec-generator.md +1 -0
- package/agents/test-tools/autotest/README.md +223 -0
- package/agents/test-tools/autotest/config.yaml.example +58 -0
- package/agents/test-tools/autotest/pyproject.toml +16 -0
- package/agents/test-tools/autotest/report_tool.py +759 -0
- package/agents/test-tools/autotest/self_test_runner.py +773 -0
- package/agents/test-tools/autotest/testcases_schema.md +143 -0
- package/agents/test-tools/autotest/testcases_tool.py +215 -0
- package/agents/test-tools/autotest/uv.lock +3156 -0
- package/agents/test-tools/harmony_autotest-0.1.0-py3-none-any.whl +0 -0
- package/agents/test-tools/hypium-6.1.0.210-py3-none-any.whl +0 -0
- package/agents/test-tools/hypium_mcp-0.6.5-py3-none-any.whl +0 -0
- package/agents/test-tools/xdevice-6.1.0.210-py3-none-any.whl +0 -0
- package/agents/test-tools/xdevice_devicetest-6.1.0.210-py3-none-any.whl +0 -0
- package/agents/test-tools/xdevice_ohos-6.1.0.210-py3-none-any.whl +0 -0
- package/dist/cli/config-store.js +27 -2
- package/dist/cli/config.js +17 -6
- package/dist/cli/index.js +3 -2
- package/dist/cli/init.js +135 -22
- package/dist/cli/mcp.js +2 -2
- package/dist/context/index.js +165 -69
- package/package.json +59 -60
- package/skills/code-dev-review-fix/SKILL.md +279 -0
- package/skills/code-dev-review-fix-workspace/evals/evals.json +56 -0
- package/skills/code-dev-review-fix-workspace/iteration-1/routing-results.md +23 -0
- package/skills/convert_pipeline/SKILL.md +423 -439
- package/skills/hmos-resources-convert/SKILL.md +623 -0
- package/skills/hmos-resources-convert/evals/evals.json +171 -0
- package/skills/hmos-resources-convert/references/conversion-rules.md +663 -0
- package/skills/hmos-resources-convert/references/dependency-analysis-rules.md +388 -0
- package/skills/hmos-resources-convert/references/resource-mapping-rules.md +457 -0
- package/skills/hmos-resources-convert/references/xml-drawable-to-svg-rules.md +513 -0
- package/skills/hmos-resources-convert/template/AppScope/app.json5 +10 -0
- package/skills/hmos-resources-convert/template/AppScope/resources/base/element/string.json +8 -0
- package/skills/hmos-resources-convert/template/AppScope/resources/base/media/background.png +0 -0
- package/skills/hmos-resources-convert/template/AppScope/resources/base/media/foreground.png +0 -0
- package/skills/hmos-resources-convert/template/AppScope/resources/base/media/layered_image.json +7 -0
- package/skills/hmos-resources-convert/template/build-profile.json5 +42 -0
- package/skills/hmos-resources-convert/template/code-linter.json5 +32 -0
- package/skills/hmos-resources-convert/template/entry/build-profile.json5 +33 -0
- package/skills/hmos-resources-convert/template/entry/hvigorfile.ts +6 -0
- package/skills/hmos-resources-convert/template/entry/obfuscation-rules.txt +23 -0
- package/skills/hmos-resources-convert/template/entry/oh-package.json5 +10 -0
- package/skills/hmos-resources-convert/template/entry/src/main/ets/entryability/EntryAbility.ets +48 -0
- package/skills/hmos-resources-convert/template/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets +16 -0
- package/skills/hmos-resources-convert/template/entry/src/main/ets/pages/Index.ets +23 -0
- package/skills/hmos-resources-convert/template/entry/src/main/module.json5 +55 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/base/element/color.json +8 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/base/element/float.json +8 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/base/element/string.json +16 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/base/media/background.png +0 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/base/media/foreground.png +0 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/base/media/layered_image.json +7 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/base/media/startIcon.png +0 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/base/profile/backup_config.json +3 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/base/profile/main_pages.json +5 -0
- package/skills/hmos-resources-convert/template/entry/src/main/resources/dark/element/color.json +8 -0
- package/skills/hmos-resources-convert/template/entry/src/mock/mock-config.json5 +2 -0
- package/skills/hmos-resources-convert/template/entry/src/ohosTest/ets/test/Ability.test.ets +35 -0
- package/skills/hmos-resources-convert/template/entry/src/ohosTest/ets/test/List.test.ets +5 -0
- package/skills/hmos-resources-convert/template/entry/src/ohosTest/module.json5 +16 -0
- package/skills/hmos-resources-convert/template/entry/src/test/List.test.ets +5 -0
- package/skills/hmos-resources-convert/template/entry/src/test/LocalUnit.test.ets +33 -0
- package/skills/hmos-resources-convert/template/hvigor/hvigor-config.json5 +23 -0
- package/skills/hmos-resources-convert/template/hvigorfile.ts +6 -0
- package/skills/hmos-resources-convert/template/oh-package-lock.json5 +28 -0
- package/skills/hmos-resources-convert/template/oh-package.json5 +10 -0
- package/skills/hmos-resources-convert/tools/apktool.bat +85 -0
- package/skills/hmos-resources-convert/tools/apktool_3.0.1.jar +0 -0
- package/skills/hmos-ui-align/SKILL.md +182 -0
- package/skills/hmos-ui-align/config-example.json +11 -0
- package/skills/hmos-ui-align/config.json +11 -0
- package/skills/hmos-ui-align/diff_analysis.md +53 -0
- package/skills/hmos-ui-align/page_align.md +62 -0
- package/skills/hmos-ui-align/readme.md +231 -0
- package/skills/hmos-ui-align/references/Comparison_Template.md +2 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Link/350/243/205/351/245/260/345/231/250/357/274/232/347/210/266/345/255/220/345/217/214/345/220/221/345/220/214/346/255/245.md +648 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Observed/350/243/205/351/245/260/345/231/250/345/222/214@ObjectLink/350/243/205/351/245/260/345/231/250/357/274/232/345/265/214/345/245/227/347/261/273/345/257/271/350/261/241/345/261/236/346/200/247/345/217/230/345/214/226.md +2089 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Prop/350/243/205/351/245/260/345/231/250/357/274/232/347/210/266/345/255/220/345/215/225/345/220/221/345/220/214/346/255/245.md +1033 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Provide/350/243/205/351/245/260/345/231/250/345/222/214@Consume/350/243/205/351/245/260/345/231/250/357/274/232/344/270/216/345/220/216/344/273/243/347/273/204/344/273/266/345/217/214/345/220/221/345/220/214/346/255/245.md +1183 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@State/350/243/205/351/245/260/345/231/250/357/274/232/347/273/204/344/273/266/345/206/205/347/212/266/346/200/201.md +576 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Track/350/243/205/351/245/260/345/231/250/357/274/232class/345/257/271/350/261/241/345/261/236/346/200/247/347/272/247/346/233/264/346/226/260.md +297 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Watch/350/243/205/351/245/260/345/231/250/357/274/232/347/212/266/346/200/201/345/217/230/351/207/217/346/233/264/346/224/271/351/200/232/347/237/245.md +395 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/AppStorage/357/274/232/345/272/224/347/224/250/345/205/250/345/261/200/347/232/204UI/347/212/266/346/200/201/345/255/230/345/202/250.md +903 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/Environment/357/274/232/350/256/276/345/244/207/347/216/257/345/242/203/346/237/245/350/257/242.md +106 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/LocalStorage/357/274/232/351/241/265/351/235/242/347/272/247UI/347/212/266/346/200/201/345/255/230/345/202/250.md +1178 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/MVVM/346/250/241/345/274/217V1.md +911 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/MVVM/346/250/241/345/274/217/357/274/210V1/357/274/211.md +911 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/PersistentStorage/357/274/232/346/214/201/344/271/205/345/214/226/345/255/230/345/202/250UI/347/212/266/346/200/201.md +355 -0
- package/skills/hmos-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243//347/256/241/347/220/206/345/272/224/347/224/250/346/213/245/346/234/211/347/232/204/347/212/266/346/200/201/346/246/202/350/277/260.md +11 -0
- package/skills/hmos-ui-align/references/UI_Analysis_Template.md +4 -0
- package/skills/hmos-ui-align/references/android-to-harmonyOS-ui-atomic-component-mapping-reference.md +2535 -0
- package/skills/hmos-ui-align/references/android-to-harmonyOS-ui-interaction-mapping-reference.md +555 -0
- package/skills/hmos-ui-align/references/android-to-harmonyOS-ui-layout-mapping-reference.md +117 -0
- package/skills/hmos-ui-align/scripts/app_feature_verify.py +443 -0
- package/skills/hmos-ui-align/scripts/navigation-capure.md +37 -0
- package/skills/hmos-ui-align/scripts/page_capture.py +592 -0
- package/skills/hmos-ui-align-batch/SKILL.md +99 -0
- package/skills/hmos-ui-align-batch/references/conversion-procedure.md +180 -0
- package/skills/hmos-ui-align-batch/references/mappings/android-to-harmonyOS-ui-atomic-component-mapping-reference.md +2535 -0
- package/skills/hmos-ui-align-batch/references/mappings/android-to-harmonyOS-ui-interaction-mapping-reference.md +555 -0
- package/skills/hmos-ui-align-batch/references/mappings/android-to-harmonyOS-ui-layout-mapping-reference.md +117 -0
- package/skills/hmos-ui-align-batch/references/mvvm/@Link/350/243/205/351/245/260/345/231/250/357/274/232/347/210/266/345/255/220/345/217/214/345/220/221/345/220/214/346/255/245.md +648 -0
- package/skills/hmos-ui-align-batch/references/mvvm/@Observed/350/243/205/351/245/260/345/231/250/345/222/214@ObjectLink/350/243/205/351/245/260/345/231/250/357/274/232/345/265/214/345/245/227/347/261/273/345/257/271/350/261/241/345/261/236/346/200/247/345/217/230/345/214/226.md +2089 -0
- package/skills/hmos-ui-align-batch/references/mvvm/@Prop/350/243/205/351/245/260/345/231/250/357/274/232/347/210/266/345/255/220/345/215/225/345/220/221/345/220/214/346/255/245.md +1033 -0
- package/skills/hmos-ui-align-batch/references/mvvm/@Provide/350/243/205/351/245/260/345/231/250/345/222/214@Consume/350/243/205/351/245/260/345/231/250/357/274/232/344/270/216/345/220/216/344/273/243/347/273/204/344/273/266/345/217/214/345/220/221/345/220/214/346/255/245.md +1183 -0
- package/skills/hmos-ui-align-batch/references/mvvm/@State/350/243/205/351/245/260/345/231/250/357/274/232/347/273/204/344/273/266/345/206/205/347/212/266/346/200/201.md +576 -0
- package/skills/hmos-ui-align-batch/references/mvvm/@Track/350/243/205/351/245/260/345/231/250/357/274/232class/345/257/271/350/261/241/345/261/236/346/200/247/347/272/247/346/233/264/346/226/260.md +297 -0
- package/skills/hmos-ui-align-batch/references/mvvm/@Watch/350/243/205/351/245/260/345/231/250/357/274/232/347/212/266/346/200/201/345/217/230/351/207/217/346/233/264/346/224/271/351/200/232/347/237/245.md +395 -0
- package/skills/hmos-ui-align-batch/references/mvvm/AppStorage/357/274/232/345/272/224/347/224/250/345/205/250/345/261/200/347/232/204UI/347/212/266/346/200/201/345/255/230/345/202/250.md +903 -0
- package/skills/hmos-ui-align-batch/references/mvvm/Environment/357/274/232/350/256/276/345/244/207/347/216/257/345/242/203/346/237/245/350/257/242.md +106 -0
- package/skills/hmos-ui-align-batch/references/mvvm/LocalStorage/357/274/232/351/241/265/351/235/242/347/272/247UI/347/212/266/346/200/201/345/255/230/345/202/250.md +1178 -0
- package/skills/hmos-ui-align-batch/references/mvvm/MVVM/346/250/241/345/274/217/357/274/210V1/357/274/211.md +911 -0
- package/skills/hmos-ui-align-batch/references/mvvm/PersistentStorage/357/274/232/346/214/201/344/271/205/345/214/226/345/255/230/345/202/250UI/347/212/266/346/200/201.md +355 -0
- package/skills/hmos-ui-align-batch/references/mvvm//347/256/241/347/220/206/345/272/224/347/224/250/346/213/245/346/234/211/347/232/204/347/212/266/346/200/201/346/246/202/350/277/260.md +11 -0
- package/skills/hmos-ui-align-batch/scripts/android_parse_fast.py +1606 -0
- package/skills/self-test/SKILL.md +369 -0
- package/skills/self-test/readme.md +309 -0
- package/skills/spec-generator-skill/SKILL.md +332 -0
- package/skills/spec-generator-skill/references/android-platform-tokens.md +105 -0
- package/skills/spec-generator-skill/references/spec-sample-1.md +78 -0
- package/skills/spec-generator-skill/references/spec-sample-2.md +58 -0
- package/skills/spec-generator-skill/references/spec-sample-3.md +116 -0
- package/skills/spec-generator-skill/references/step4-report-template.md +33 -0
- package/agents/self-test-setup.md +0 -165
- package/dist/context/resources/sdkConfig.json +0 -24
- package/src/context/resources/sdkConfig.json +0 -24
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
# PersistentStorage:持久化存储UI状态
|
|
2
|
+
PersistentStorage是应用程序中的可选单例对象。此对象的作用是持久化存储选定的AppStorage属性,以确保这些属性在应用程序重新启动时的值与应用程序关闭时的值相同。
|
|
3
|
+
|
|
4
|
+
PersistentStorage提供状态变量持久化的能力,但是需要注意,其持久化和读回UI的能力都需要依赖AppStorage。在阅读本文档前,建议提前阅读:[AppStorage](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-appstorage),[PersistentStorage API文档](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-state-management#persistentstorage)。
|
|
5
|
+
## 概述
|
|
6
|
+
PersistentStorage将选定的AppStorage属性保留在设备磁盘上。应用程序通过API,以决定哪些属性应借助PersistentStorage持久化。PersistentStorage和AppStorage中的属性建立了双向同步,UI和业务逻辑不直接访问PersistentStorage中的属性,所有属性访问都是对AppStorage的访问,AppStorage中的更改会自动同步到PersistentStorage。
|
|
7
|
+
|
|
8
|
+
PersistentStorage的存储路径为module级别,即哪个module调用了PersistentStorage,数据副本存入对应module的持久化文件中。如果多个module使用相同的key,则数据归属到最先使用PersistentStorage的module里。
|
|
9
|
+
|
|
10
|
+
PersistentStorage的存储路径在应用第一个ability启动时就已确定,为该ability所属的module。如果一个ability调用了PersistentStorage,并且该ability能被不同的module拉起,那么ability存在多少种启动方式,就会有多少份数据副本。
|
|
11
|
+
|
|
12
|
+
PersistentStorage功能上耦合了AppStorage,并且数据在不同module中使用也会有问题,因此推荐开发者使用[PersistenceV2](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-new-persistencev2)的globalConnect接口替换掉PersistentStorage的persistProp接口。PersistentStorage向PersistenceV2迁移的方案见[PersistentStorage->PersistenceV2](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-v1-v2-migration-application#persistentstorage-persistencev2)。
|
|
13
|
+
## 限制条件
|
|
14
|
+
PersistentStorage允许的类型和值有:
|
|
15
|
+
|
|
16
|
+
- number,string,boolean,enum 等简单类型。
|
|
17
|
+
- 可以被JSON.stringify()和JSON.parse()重构的对象(但是对象中的成员方法不支持持久化)。
|
|
18
|
+
- API version 12及以上支持Map类型,可以观察到Map整体的赋值,同时可通过调用Map的接口set、clear、delete 更新Map的值,且更新的值被持久化存储。详见[持久化Map类型变量](#持久化map类型变量)。
|
|
19
|
+
- API version 12及以上支持Set类型,可以观察到Set整体的赋值,同时可通过调用Set的接口add、clear、delete 更新Set的值,且更新的值被持久化存储。详见[持久化Set类型变量](#持久化set类型变量)。
|
|
20
|
+
- API version 12及以上支持Date类型,可以观察到Date整体的赋值,同时可通过调用Date的接口setFullYear、setMonth、setDate、setHours、setMinutes、setSeconds、setMilliseconds、setTime、setUTCFullYear、setUTCMonth、setUTCDate、setUTCHours、setUTCMinutes、setUTCSeconds、setUTCMilliseconds 更新Date的属性,且更新的值被持久化存储。详见[持久化Date类型变量](#持久化date类型变量)。
|
|
21
|
+
- API version 12及以上支持undefined 和 null。
|
|
22
|
+
- API version 12及以上[支持联合类型](#持久化联合类型变量)。
|
|
23
|
+
PersistentStorage不允许的类型和值有:
|
|
24
|
+
|
|
25
|
+
- 嵌套对象(对象数组,对象的属性是对象等)。因为目前框架无法检测AppStorage中嵌套对象(包括数组)值的变化,所以无法写回到PersistentStorage中。
|
|
26
|
+
持久化数据是一个相对缓慢的操作,应用程序应避免以下情况:
|
|
27
|
+
|
|
28
|
+
- 持久化大型数据集。
|
|
29
|
+
|
|
30
|
+
- 持久化经常变化的变量。
|
|
31
|
+
|
|
32
|
+
PersistentStorage的持久化变量最好是小于2kb的数据,不要大量的数据持久化,因为PersistentStorage写入磁盘是在UI线程同步执行的,大量数据本地读写会影响UI渲染性能。如果开发者需要存储大量的数据,建议使用[@ohos.data.relationalStore (关系型数据库)](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-data-relationalstore)相关接口。
|
|
33
|
+
|
|
34
|
+
PersistentStorage和UI实例相关联,持久化操作需要在UI实例初始化成功后(即[loadContent](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-windowstage#loadcontent9)传入的回调被调用时)才可以被调用,早于该时机调用会导致持久化失败。
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
// EntryAbility.ets
|
|
38
|
+
onWindowStageCreate(windowStage: window.WindowStage): void {
|
|
39
|
+
windowStage.loadContent('pages/PageOneMessageStorage', (err) => {
|
|
40
|
+
if (err.code) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
PersistentStorage.persistProp('aProp', 47);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
## 使用场景 ### 从AppStorage中访问PersistentStorage初始化的属性
|
|
48
|
+
- 初始化PersistentStorage:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
PersistentStorage.persistProp('aProp', 47);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
- 在AppStorage获取对应属性:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
AppStorage.get<number>('aProp'); // returns 47
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
或在组件内部定义:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
@StorageLink('aProp') aProp: number = 48;
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
完整代码如下:
|
|
67
|
+
|
|
68
|
+
```TypeScript
|
|
69
|
+
PersistentStorage.persistProp('aProp', 47);
|
|
70
|
+
|
|
71
|
+
@Entry
|
|
72
|
+
@Component
|
|
73
|
+
struct TestPageOne {
|
|
74
|
+
@State message: string = 'Hello World';
|
|
75
|
+
@StorageLink('aProp') aProp: number = 48;
|
|
76
|
+
|
|
77
|
+
build() {
|
|
78
|
+
Row() {
|
|
79
|
+
Column() {
|
|
80
|
+
Text(this.message)
|
|
81
|
+
// 应用退出时会保存当前结果。重新启动后,会显示上一次的保存结果
|
|
82
|
+
// 未修改时默认值为47
|
|
83
|
+
Text(`${this.aProp}`)
|
|
84
|
+
.onClick(() => {
|
|
85
|
+
this.aProp += 1;
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
- 新应用安装后首次启动运行:
|
|
94
|
+
|
|
95
|
+
- 调用persistProp初始化PersistentStorage,首先查询在PersistentStorage本地文件中是否存在“aProp”,查询结果为不存在,因为应用是第一次安装。
|
|
96
|
+
- 接着查询属性“aProp”在AppStorage中是否存在,依旧不存在。
|
|
97
|
+
- 在AppStorage中创建名为“aProp”的number类型属性,属性初始值是定义的默认值47。
|
|
98
|
+
- PersistentStorage将属性“aProp”和值47写入磁盘,AppStorage中“aProp”对应的值和其后续的更改将被持久化。
|
|
99
|
+
- 在TestPageOne组件中创建状态变量@StorageLink('aProp') aProp,和AppStorage中“aProp”双向绑定,在创建的过程中会在AppStorage中查找,成功找到“aProp”,所以使用其在AppStorage找到的值47。
|
|
100
|
+
**图1** PersistProp初始化流程
|
|
101
|
+
|
|
102
|
+

|
|
103
|
+
|
|
104
|
+
- 触发点击事件后:
|
|
105
|
+
|
|
106
|
+
- 状态变量@StorageLink('aProp') aProp改变,触发Text组件重新刷新。
|
|
107
|
+
- @StorageLink装饰的变量是和AppStorage中建立双向同步的,所以@StorageLink('aProp') aProp的变化会被同步回AppStorage中。
|
|
108
|
+
- AppStorage中“aProp”属性的改变会同步到所有绑定该“aProp”的单向或者双向变量,在本示例中没有其他的绑定“aProp”的变量。
|
|
109
|
+
- 因为“aProp”对应的属性已经被持久化,所以在AppStorage中“aProp”的改变会触发PersistentStorage,将新的改变写入本地磁盘。
|
|
110
|
+
- 后续启动应用:
|
|
111
|
+
|
|
112
|
+
- 执行PersistentStorage.persistProp('aProp', 47),首先在PersistentStorage本地文件查询“aProp”属性,成功查询到。
|
|
113
|
+
- 将在PersistentStorage查询到的值写入AppStorage中。
|
|
114
|
+
- 在TestPageOne组件里,@StorageLink绑定的“aProp”为PersistentStorage写入AppStorage中的值,即为上一次退出应用存入的值。 ### 在PersistentStorage之前访问AppStorage中的属性
|
|
115
|
+
该示例为反例。在调用PersistentStorage.persistProp或者persistProps之前使用接口访问AppStorage中的属性是错误的,因为这样的调用顺序会丢失上一次应用程序运行中的属性值:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
let aProp = AppStorage.setOrCreate('aProp', 47);
|
|
119
|
+
PersistentStorage.persistProp('aProp', 48);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
应用在非首次运行时,先执行AppStorage.setOrCreate('aProp', 47):属性“aProp”在AppStorage中创建,其类型为number,其值设置为指定的默认值47。“aProp”是持久化的属性,所以会被写回PersistentStorage磁盘中,PersistentStorage存储的上次退出应用的值被覆盖。
|
|
123
|
+
|
|
124
|
+
PersistentStorage.persistProp('aProp', 48):在PersistentStorage中查找到“aProp”,值为刚刚使用AppStorage接口写入的47。
|
|
125
|
+
### 在PersistentStorage之后访问AppStorage中的属性
|
|
126
|
+
开发者可以先判断是否需要覆盖上一次保存在PersistentStorage中的值,如果需要覆盖,再调用AppStorage的接口进行修改,如果不需要覆盖,则不调用AppStorage的接口。
|
|
127
|
+
|
|
128
|
+
```TypeScript
|
|
129
|
+
const MAX_NUM: number = 50; // 大字体尺寸
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
```TypeScript
|
|
133
|
+
PersistentStorage.persistProp('aProp', 48);
|
|
134
|
+
if ((AppStorage.get<number>('aProp') ?? 0) > MAX_NUM) {
|
|
135
|
+
// 如果PersistentStorage存储的值超过50,设置为47
|
|
136
|
+
AppStorage.setOrCreate('aProp',47);
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
示例代码在读取PersistentStorage存储的数据后,判断“aProp”的值是否大于50,如果大于50,则使用AppStorage的接口将其设置为47。
|
|
141
|
+
### 持久化联合类型变量
|
|
142
|
+
PersistentStorage支持联合类型和undefined和null,在下面的示例中,使用persistProp方法初始化“P”为undefined。通过@StorageLink('P')绑定变量p,类型为number | undefined | null,点击Button改变P的值,视图会随之刷新。且P的值被持久化存储。
|
|
143
|
+
|
|
144
|
+
```TypeScript
|
|
145
|
+
// 定义常量替代魔法值,明确数值含义
|
|
146
|
+
const DEFAULT_NUMBER: number = 10; // 默认数字值
|
|
147
|
+
const FONT_SIZE_LARGE: number = 50; // 大字体尺寸
|
|
148
|
+
|
|
149
|
+
// 初始化持久化属性,键名使用常量定义(若有多处使用可提取)
|
|
150
|
+
const STORAGE_KEY_P: string = 'P';
|
|
151
|
+
PersistentStorage.persistProp(STORAGE_KEY_P, undefined);
|
|
152
|
+
|
|
153
|
+
@Entry
|
|
154
|
+
@Component
|
|
155
|
+
struct TestCase6 {
|
|
156
|
+
// 使用常量作为默认值,类型明确
|
|
157
|
+
@StorageLink(STORAGE_KEY_P) p: number | undefined | null = DEFAULT_NUMBER;
|
|
158
|
+
|
|
159
|
+
build() {
|
|
160
|
+
Row() {
|
|
161
|
+
Column() {
|
|
162
|
+
Text(this.p + '')
|
|
163
|
+
.fontSize(FONT_SIZE_LARGE)
|
|
164
|
+
.fontWeight(FontWeight.Bold)
|
|
165
|
+
Button('changeToNumber').onClick(() => {
|
|
166
|
+
this.p = DEFAULT_NUMBER; // 引用常量,避免直接写10
|
|
167
|
+
})
|
|
168
|
+
Button('changeTo undefined').onClick(() => {
|
|
169
|
+
this.p = undefined;
|
|
170
|
+
})
|
|
171
|
+
Button('changeTo null').onClick(() => {
|
|
172
|
+
this.p = null;
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
.width('100%')
|
|
176
|
+
}
|
|
177
|
+
.height('100%')
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
### 持久化Date类型变量
|
|
182
|
+
在下面的示例中,@StorageLink装饰的persistedDate类型为Date,点击Button改变persistedDate的值,视图会随之刷新。且persistedDate的值被持久化存储。
|
|
183
|
+
|
|
184
|
+
```TypeScript
|
|
185
|
+
PersistentStorage.persistProp('persistedDate', new Date());
|
|
186
|
+
|
|
187
|
+
@Entry
|
|
188
|
+
@Component
|
|
189
|
+
struct PersistedDate {
|
|
190
|
+
@StorageLink('persistedDate') persistedDate: Date = new Date();
|
|
191
|
+
|
|
192
|
+
updateDate() {
|
|
193
|
+
this.persistedDate = new Date();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
build() {
|
|
197
|
+
List() {
|
|
198
|
+
ListItem() {
|
|
199
|
+
Column() {
|
|
200
|
+
Text(`Persisted Date is ${this.persistedDate.toString()}`)
|
|
201
|
+
.margin(20)
|
|
202
|
+
|
|
203
|
+
Text(`Persisted Date year is ${this.persistedDate.getFullYear()}`)
|
|
204
|
+
.margin(20)
|
|
205
|
+
|
|
206
|
+
Text(`Persisted Date hours is ${this.persistedDate.getHours()}`)
|
|
207
|
+
.margin(20)
|
|
208
|
+
|
|
209
|
+
Text(`Persisted Date minutes is ${this.persistedDate.getMinutes()}`)
|
|
210
|
+
.margin(20)
|
|
211
|
+
|
|
212
|
+
Text(`Persisted Date time is ${this.persistedDate.toLocaleTimeString()}`)
|
|
213
|
+
.margin(20)
|
|
214
|
+
|
|
215
|
+
Button() {
|
|
216
|
+
Text('Update Date')
|
|
217
|
+
.fontSize(25)
|
|
218
|
+
.fontWeight(FontWeight.Bold)
|
|
219
|
+
.fontColor(Color.White)
|
|
220
|
+
}
|
|
221
|
+
.type(ButtonType.Capsule)
|
|
222
|
+
.margin({
|
|
223
|
+
top: 20
|
|
224
|
+
})
|
|
225
|
+
.backgroundColor('#0D9FFB')
|
|
226
|
+
.width('60%')
|
|
227
|
+
.height('5%')
|
|
228
|
+
.onClick(() => {
|
|
229
|
+
this.updateDate();
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
}.width('100%')
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
### 持久化Map类型变量
|
|
239
|
+
在下面的示例中,@StorageLink装饰的persistedMapString类型为Map<number, string>,点击Button改变persistedMapString的值,视图会随之刷新。且persistedMapString的值被持久化存储。
|
|
240
|
+
|
|
241
|
+
```TypeScript
|
|
242
|
+
PersistentStorage.persistProp('persistedMapString', new Map<number, string>([]));
|
|
243
|
+
|
|
244
|
+
@Entry
|
|
245
|
+
@Component
|
|
246
|
+
struct PersistedMap {
|
|
247
|
+
@StorageLink('persistedMapString') persistedMapString: Map<number, string> = new Map<number, string>([]);
|
|
248
|
+
|
|
249
|
+
persistMapString() {
|
|
250
|
+
this.persistedMapString = new Map<number, string>([[3, 'one'], [6, 'two'], [9, 'three']]);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
build() {
|
|
254
|
+
List() {
|
|
255
|
+
ListItem() {
|
|
256
|
+
Column() {
|
|
257
|
+
Text(`Persisted Map String is `)
|
|
258
|
+
.margin(20)
|
|
259
|
+
ForEach(Array.from(this.persistedMapString.entries()), (item: [number, string]) => {
|
|
260
|
+
Text(`${item[0]} ${item[1]}`)
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
Button() {
|
|
264
|
+
Text('Persist Map String')
|
|
265
|
+
.fontSize(20)
|
|
266
|
+
.fontWeight(FontWeight.Bold)
|
|
267
|
+
.fontColor(Color.White)
|
|
268
|
+
}
|
|
269
|
+
.type(ButtonType.Capsule)
|
|
270
|
+
.margin({
|
|
271
|
+
top: 20
|
|
272
|
+
})
|
|
273
|
+
.backgroundColor('#0D9FFB')
|
|
274
|
+
.width('60%')
|
|
275
|
+
.height('5%')
|
|
276
|
+
.onClick(() => {
|
|
277
|
+
this.persistMapString();
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
}.width('100%')
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
### 持久化Set类型变量
|
|
287
|
+
在下面的示例中,@StorageLink装饰的persistedSet类型为Set<number>,点击Button改变persistedSet的值,视图会随之刷新。且persistedSet的值被持久化存储。
|
|
288
|
+
|
|
289
|
+
```TypeScript
|
|
290
|
+
PersistentStorage.persistProp('persistedSet', new Set<number>([]));
|
|
291
|
+
|
|
292
|
+
@Entry
|
|
293
|
+
@Component
|
|
294
|
+
struct PersistedSet {
|
|
295
|
+
@StorageLink('persistedSet') persistedSet: Set<number> = new Set<number>([]);
|
|
296
|
+
|
|
297
|
+
persistSet() {
|
|
298
|
+
this.persistedSet = new Set<number>([33, 1, 3]);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
clearSet() {
|
|
302
|
+
this.persistedSet.clear();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
build() {
|
|
306
|
+
List() {
|
|
307
|
+
ListItem() {
|
|
308
|
+
Column() {
|
|
309
|
+
Text(`Persisted Set is `)
|
|
310
|
+
.margin(20)
|
|
311
|
+
ForEach(Array.from(this.persistedSet.entries()), (item: [number, number]) => {
|
|
312
|
+
Text(`${item[1]}`)
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
Button() {
|
|
316
|
+
Text('Persist Set')
|
|
317
|
+
.fontSize(25)
|
|
318
|
+
.fontWeight(FontWeight.Bold)
|
|
319
|
+
.fontColor(Color.White)
|
|
320
|
+
}
|
|
321
|
+
.type(ButtonType.Capsule)
|
|
322
|
+
.margin({
|
|
323
|
+
top: 20
|
|
324
|
+
})
|
|
325
|
+
.backgroundColor('#0D9FFB')
|
|
326
|
+
.width('60%')
|
|
327
|
+
.height('5%')
|
|
328
|
+
.onClick(() => {
|
|
329
|
+
this.persistSet();
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
Button() {
|
|
333
|
+
Text('Persist Clear')
|
|
334
|
+
.fontSize(25)
|
|
335
|
+
.fontWeight(FontWeight.Bold)
|
|
336
|
+
.fontColor(Color.White)
|
|
337
|
+
}
|
|
338
|
+
.type(ButtonType.Capsule)
|
|
339
|
+
.margin({
|
|
340
|
+
top: 20
|
|
341
|
+
})
|
|
342
|
+
.backgroundColor('#0D9FFB')
|
|
343
|
+
.width('60%')
|
|
344
|
+
.height('5%')
|
|
345
|
+
.onClick(() => {
|
|
346
|
+
this.clearSet();
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
}
|
|
350
|
+
.width('100%')
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# 管理应用拥有的状态概述
|
|
2
|
+
|
|
3
|
+
在管理组件拥有的状态章节中介绍的装饰器仅能在页面内,即一个组件树上共享状态变量。如果开发者要实现应用级的,或者多个页面的状态数据共享,就需要用到应用级别的状态管理的概念。ArkTS根据不同特性,提供了多种应用状态管理的能力:
|
|
4
|
+
|
|
5
|
+
- [LocalStorage](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-localstorage):页面级UI状态存储,通常用于[UIAbility](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability)内、页面间的状态共享。
|
|
6
|
+
|
|
7
|
+
- [AppStorage](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-appstorage):特殊的单例LocalStorage对象,由UI框架在应用程序启动时创建,为应用程序UI状态属性提供中央存储。
|
|
8
|
+
|
|
9
|
+
- [PersistentStorage](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-persiststorage):持久化存储UI状态,通常和AppStorage配合使用,选择AppStorage存储的数据写入磁盘,以确保这些属性在应用程序重新启动时的值与应用程序关闭时的值相同。
|
|
10
|
+
|
|
11
|
+
- [Environment](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-environment):应用程序运行的设备的环境参数,环境参数会同步到AppStorage中,可以和AppStorage搭配使用。
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
| 类别 | 文本内容 (Content) | 尺寸与布局 (Size & Layout) | 颜色与背景 (Color & Background) | 边框与轮廓 (Border & Outline) | 文本样式 (Typography) | 变换与效果 (Transform & Effect) | 状态与交互反馈 (State & Feedback) |
|
|
2
|
+
| :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- |
|
|
3
|
+
| **具体属性名** | `text` / `content` / `title` | `width`<br>`height`<br>`minWidth` / `maxWidth`<br>`minHeight` / `maxHeight`<br>`padding`<br>`margin`<br>`constraints`<br>`aspectRatio`<br>`alignment` (子控件位置) | `backgroundColor`<br>`background` (drawable/image)<br>`foreground`<br>`color` / `textColor`<br>`tintColor` / `colorFilter`<br>`shadow` (offset, radius, color)<br>`opacity` / `alpha` | `borderWidth`<br>`borderColor`<br>`borderRadius`<br>`borderStyle` (solid, dashed, dotted)<br>`outline` | `fontSize`<br>`fontFamily`<br>`fontWeight`<br>`fontStyle` (normal, italic)<br>`letterSpacing`<br>`lineHeight`<br>`textDecoration` (underline, line-through)<br>`textAlign` / `gravity` (文字位置)<br>`textOverflow` / `maxLines` | `scale` (x, y)<br>`rotation` (z, x, y)<br>`translate` (x, y)<br>`blur`<br>`brightness`<br>`contrast`<br>`saturation`<br>`clip` (circle, path) | `state` (normal, pressed, focused, disabled, selected, error)<br>(所有上述属性均可按状态单独设置) |
|
|
4
|
+
| **具体例子<br>(登录按钮)** | `text = "登录"` | `width = 200vp`<br>`height = 48vp`<br>`padding = 12vp`<br>`margin = 16vp`<br>`alignment = center` (在父容器中居中) | `backgroundColor = #007AFF`<br>`textColor = #FFFFFF`<br>`shadow(offset=0, radius=4, color=#40000000)` | `borderRadius = 24vp`<br>`borderWidth = 0`<br>`borderStyle = solid` | `fontSize = 16fp`<br>`fontWeight = bold`<br>`fontFamily = sans-serif-medium`<br>`textAlign = center` (文字在按钮内居中) | `scale = 0.95` (按下时)<br>`opacity = 0.8` (禁用时) | `normal`: 蓝色背景<br>`pressed`: 深蓝色背景<br>`disabled`: 灰色背景<br>`loading`: 显示进度圈 |
|