diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..71bc2e1bfcea9d4e0cd4e3fb346d6b79c29449e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer +/entry/.preview \ No newline at end of file diff --git a/AppScope/app.json5 b/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8f4b95ef70c4e23f0606f17dca09212e5b761ec2 --- /dev/null +++ b/AppScope/app.json5 @@ -0,0 +1,10 @@ +{ + "app": { + "bundleName": "com.example.statusmanagementv2", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:layered_image", + "label": "$string:app_name" + } +} diff --git a/AppScope/resources/base/element/string.json b/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..ebdfc86b0a6ab4aae6f999373ef7fd3b2aa8b5ce --- /dev/null +++ b/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "statusmanagementv2" + } + ] +} diff --git a/AppScope/resources/base/media/background.png b/AppScope/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/AppScope/resources/base/media/background.png differ diff --git a/AppScope/resources/base/media/foreground.png b/AppScope/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..eb9427585b36d14b12477435b6419d1f07b3e0bb Binary files /dev/null and b/AppScope/resources/base/media/foreground.png differ diff --git a/AppScope/resources/base/media/layered_image.json b/AppScope/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/AppScope/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..18795a48d6b12fcdc1aa7bac9a9cb99f83815267 --- /dev/null +++ b/LICENSE @@ -0,0 +1,78 @@ + Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +Apache License, Version 2.0 +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. + +"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: +1.You must give any other recipients of the Work or Derivative Works a copy of this License; and +2.You must cause any modified files to carry prominent notices stating that You changed the files; and +3.You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and +4.If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. + +You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/README.md b/README.md index 300b2059cc7ef64e16d905c3fff18ed5a2c83643..266d6bb92e3922bcf73a07dcf5d1a2c99525aad2 100644 --- a/README.md +++ b/README.md @@ -1,37 +1,73 @@ -# StatusManagementV2 +# 实现状态管理V2装饰器原子级场景 -#### 介绍 -此Codelab主要介绍状态管理V2所属装饰器的含义和使用场景。 +## 项目简介 -#### 软件架构 -软件架构说明 +本示例针对状态管理V2所属装饰器的功能和属性的介绍及单一场景案例的实现,涉及如下装饰器: +- @ObservedV2装饰器和@Trace装饰器:类属性变化观测 +- @ComponentV2装饰器:自定义组件 +- @Local装饰器:组件内部状态 +- @Param:组件外部输入 +- @Event装饰器:规范组件输出 +- @Provider装饰器和@Consumer装饰器:跨组件层级双向同步 +- @Monitor装饰器:状态变量修改监听 +- @Computed装饰器:计算属性 -#### 安装教程 +## 效果预览 -1. xxxx -2. xxxx -3. xxxx + -#### 使用说明 +## 使用说明 -1. xxxx -2. xxxx -3. xxxx +1. 每个page页面仅描述实现单一的装饰器。 +2. 使用时,选择要查看效果的page页面,点击开发工具的“预览功能”。 -#### 参与贡献 +## 工程目录 -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request + ``` + ├──entry + │ ├──build-profile.json5 // 当前的模块信息、编译信息配置项 + │ ├──hvigorfile.ts // 模块级编译构建任务脚本 + │ ├──oh-package.json5 // 描述三方包的包名、版本、入口文件(类型声明文件)和依赖项等信息 + │ └──src + │ └──main + │ ├──ets + │ │ ├──entryability + │ │ │ └──EntryAbility.ets // 程序入口类 + │ │ └──pages + │ │ ├──ComponentInternalStatus.ets // 组件内状态变量定义-@Local + │ │ ├──EventCommunication.ets // 组件间的事件通信-@Event + │ │ ├──EventSynchronizationAfter.ets // 跨组件事件传递优化后-@Provider&@Consumer + │ │ ├──EventSynchronizationBefore.ets // 跨组件事件传递优化前-@Event + │ │ ├──Monitoring.ets // 监听状态变量变化-@Monitor + │ │ ├──MonitoringBatch.ets // 批量监听状态变量变化-@Monitor + │ │ ├──ParameterInput.ets // 组件间的数据传递-@Param + │ │ ├──StateCacheAfter.ets // 状态变量数据缓存优化后-@Computed + │ │ ├──StateCacheBefore.ets // 状态变量数据缓存优化前 + │ │ ├──StateSynchronizationAfter.ets // 跨组件变量传递优化后-@Provider&@Consumer + │ │ ├──StateSynchronizationBefore.ets // 跨组件变量传递优化前-@Param + │ │ ├──Status.ets // 开关状态&计数器 + │ │ ├──StatusOfManagementObjectsAfter.ets // 监测类型属性变量的变化优化后-@Local+@ObservedV2和@Trace + │ │ └──StatusOfManagementObjectsBefore.ets // 监测类型属性变量的变化优化前-@Local + │ ├──module.json5 // Stage模型模块配置文件 + │ └──resources // 应用静态资源目录 + ├──hvigor + │ └──hvigor-config.json5 + ├──hvigorfile.ts // 应用级编译构建任务脚本 + └──oh-package.json5 // 描述全局配置 + ``` +## 具体实现 -#### 特技 +根据每一个装饰器的特性使用相应简单的场景案例来实现其效果,使用常见组件如Text、Button等。 -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +## 相关权限 + +不涉及 + +## 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 +2. HarmonyOS系统:HarmonyOS 6.0.0 Release及以上。 +3. DevEco Studio版本:DevEco Studio 6.0.0 Release及以上。 +4. HarmonyOS SDK版本:HarmonyOS 6.0.0 Release SDK及以上。 \ No newline at end of file diff --git a/build-profile.json5 b/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c4e8cef12735e3a7929c1740117603c46528ebe4 --- /dev/null +++ b/build-profile.json5 @@ -0,0 +1,42 @@ +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "targetSdkVersion": "6.0.0(20)", + "compatibleSdkVersion": "6.0.0(20)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/entry/build-profile.json5 b/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7b7a53dbfb988298f8f51017167b5fc427dd169d --- /dev/null +++ b/entry/build-profile.json5 @@ -0,0 +1,32 @@ +{ + "apiType": "stageMode", + "buildOption": { + "resOptions": { + "copyCodeResource": { + "enable": false + } + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/entry/hvigorfile.ts b/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..b0e3a1ab98a91bc918d6404b2413111a5011f14a --- /dev/null +++ b/entry/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins: [] /* Custom plugin to extend the functionality of Hvigor. */ +} \ No newline at end of file diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..248c3b7541a589682a250f86a6d3ecf7414d2d6a --- /dev/null +++ b/entry/oh-package.json5 @@ -0,0 +1,10 @@ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..8119ffd1c56e343b9cd531c12e28b8fb2d9ba652 --- /dev/null +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ConfigurationConstant, UIAbility } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; +import { BusinessError } from '@kit.BasicServicesKit'; + +const DOMAIN = 0x0000; +const TAG = 'EntryAbility'; + +export default class EntryAbility extends UIAbility { + onCreate(): void { + try { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + } catch (error) { + let err = error as BusinessError; + hilog.error(DOMAIN, TAG, `getApplicationContext Failed,code:${err.code};message:${err.message}`); + } + hilog.info(DOMAIN, TAG, '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, TAG, '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, TAG, '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Status', (err) => { + if (err.code) { + hilog.error(DOMAIN, TAG, `getApplicationContext Failed,code:${err.code};message:${err.message}`); + return; + } + hilog.info(DOMAIN, TAG, 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, TAG, '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, TAG, '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, TAG, '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/ComponentInternalStatus.ets b/entry/src/main/ets/pages/ComponentInternalStatus.ets new file mode 100644 index 0000000000000000000000000000000000000000..e9d251ce8e0c98b5e93c2a5d45a3616fd1a8bbf8 --- /dev/null +++ b/entry/src/main/ets/pages/ComponentInternalStatus.ets @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Local装饰器:组件内部状态 + * 当被@Local装饰的变量变化时,会刷新使用该变量的组件 + * 组件内状态 + * 01-定义组件内状态 + */ +@Entry +@ComponentV2 +struct ComponentInternalStatus { + @Local value1: number = 0; + public value2: number = 0; + + build() { + Column({ space: 20 }) { + Text($r('app.string.counter')) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .margin({ bottom: 60 }) + Row() { + Text('value1:') + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text(this.value1.toString()) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + } + + Row() { + Text('value2:') + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text(this.value2.toString()) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + } + + Row({ space: 20 }) { + Button($r('app.string.count', '-1')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.value1--; + this.value2--; + }) + + Button($r('app.string.count', '+1')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.value1++; + this.value2++; + }) + }.margin({ top: 60 }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/EventCommunication.ets b/entry/src/main/ets/pages/EventCommunication.ets new file mode 100644 index 0000000000000000000000000000000000000000..f71ed529451698c94cf6a46998bbed5deec30417 --- /dev/null +++ b/entry/src/main/ets/pages/EventCommunication.ets @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Event装饰器:规范组件输出 + * @Event主要配合@Param实现数据的双向同步。 + * 事件通讯 + * 03-事件通信 + */ +@ComponentV2 +struct Child { + @Param index: number = 0; + @Event changeIndex: (val: number) => void; + + build() { + Column({ space: 10 }) { + Text('Child') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text(`Child index:${this.index}`) + .fontSize(36) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Button('Change to 20') + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.changeIndex(20); + }) + } + .backgroundColor(Color.White) + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#3B82F6') + } +} + +@Entry +@ComponentV2 +struct Parent { + @Local index: number = 0; + + build() { + Column({ space: 20 }) { + Text('Parent') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Child({ + index: this.index, + changeIndex: (val: number) => { + this.index = val; + } + }) + } + .width('100%') + .height('100%') + .backgroundColor($r('app.color.pages_background')) + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/EventSynchronizationAfter.ets b/entry/src/main/ets/pages/EventSynchronizationAfter.ets new file mode 100644 index 0000000000000000000000000000000000000000..641e91dd0d0a0b08d1484193da2133bfd263d372 --- /dev/null +++ b/entry/src/main/ets/pages/EventSynchronizationAfter.ets @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Provider装饰器和@Consumer装饰器:跨组件层级双向同步 + * @Provider和@Consumer用于跨组件层级数据双向同步,可以使得开发者不用拘泥于组件层级。 + * 事件同步优化后 + * 04-跨组件共享状态-事件同步 + */ +@ComponentV2 +struct Child { + @Consumer('index') index: number = 0; + @Consumer('changeIndex') changeIndex: (val: number) => void = () => { + }; + + build() { + Column({ space: 10 }) { + Text('Child') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text(`Child index:${this.index}`) + .fontSize(36) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Button('Change to 20') + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.changeIndex(20); + }) + } + .backgroundColor(Color.White) + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#87B6EE') + } +} + +@ComponentV2 +struct ChildList { + build() { + Column({ space: 10 }) { + Text('ChildList') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + + Child() + Child() + Child() + } + .backgroundColor('#EDF5FD') + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#3B82F6') + } +} + +@Entry +@ComponentV2 +struct Parent { + @Provider('index') index: number = 0; + @Provider('changeIndex') changeIndex: (val: number) => void = (val: number) => { + this.index = val; + } + + build() { + Column({ space: 20 }) { + Text('Parent') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + ChildList() + } + .width('100%') + .height('100%') + .backgroundColor($r('app.color.pages_background')) + .padding({ top: 20 }) + .justifyContent(FlexAlign.Start) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/EventSynchronizationBefore.ets b/entry/src/main/ets/pages/EventSynchronizationBefore.ets new file mode 100644 index 0000000000000000000000000000000000000000..cadf5959f1ff2ac100a28a8e619ae9721ebbfce6 --- /dev/null +++ b/entry/src/main/ets/pages/EventSynchronizationBefore.ets @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Event主要配合@Param实现数据的双向同步。 + * 事件同步优化前 + * 04-跨组件共享状态-事件同步 + */ +@ComponentV2 +struct Child { + @Consumer('index') index: number = 0; + @Event changeIndex: (val: number) => void; + + build() { + Column({ space: 10 }) { + Text('Child') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text(`Child index:${this.index}`) + .fontSize(36) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Button('Change to 20') + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.changeIndex(20); + }) + } + .backgroundColor(Color.White) + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#87B6EE') + } +} + +@ComponentV2 +struct ChildList { + @Event changeIndex: (val: number) => void; + + build() { + Column({ space: 10 }) { + Text('ChildList') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + + Child({ changeIndex: this.changeIndex }) + Child({ changeIndex: this.changeIndex }) + Child({ changeIndex: this.changeIndex }) + } + .backgroundColor('#EDF5FD') + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#3B82F6') + } +} + +@Entry +@ComponentV2 +struct Parent { + @Provider('index') index: number = 0; + + build() { + Column({ space: 20 }) { + Text('Parent') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + ChildList({ + changeIndex: (val: number) => { + this.index = val; + } + }) + } + .width('100%') + .height('100%') + .backgroundColor($r('app.color.pages_background')) + .padding({ top: 20 }) + .justifyContent(FlexAlign.Start) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/Monitoring.ets b/entry/src/main/ets/pages/Monitoring.ets new file mode 100644 index 0000000000000000000000000000000000000000..08154786e67cba1bd060b28d55f1bcf807589f59 --- /dev/null +++ b/entry/src/main/ets/pages/Monitoring.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Monitor装饰器:状态变量修改监听 + * 状态监听 + * 06-监听状态变化 + */ +@Entry +@ComponentV2 +struct Monitoring { + @Local index: number = 0; + @Local change: string = ''; + + @Monitor('index') + indexChange(monitor: IMonitor) { + this.change = `index change from ${monitor.value()?.before} to ${monitor.value()?.now}`; + } + + build() { + Column({ space: 20 }) { + Button(`Change index:${this.index}`) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .height(50) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.index = 20; + }) + + Text(this.change) + .fontSize(20) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(300) + .textAlign(TextAlign.End) + .margin({ right: 16 }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/MonitoringBatch.ets b/entry/src/main/ets/pages/MonitoringBatch.ets new file mode 100644 index 0000000000000000000000000000000000000000..0b557868ded32ed5767944561e63d711dca1526f --- /dev/null +++ b/entry/src/main/ets/pages/MonitoringBatch.ets @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Monitor装饰器:状态变量修改监听 + * 批量监听 + * 06-监听状态变化 + */ +@Entry +@ComponentV2 +struct MonitoringBatch { + @Local index1: number = 0; + @Local index2: number = 0; + @Local change: string = ''; + + @Monitor('index1','index2') + indexChange(monitor: IMonitor) { + monitor.dirty.forEach((path: string) => { + this.change = `${path} change from ${monitor.value(path)?.before} to ${monitor.value(path)?.now}`; + }) + } + + build() { + Column({ space: 20 }) { + Button(`Change index1:${this.index1}`) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .height(50) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.index1 = 20; + }) + Button(`Change index2:${this.index2}`) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .height(50) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.index2 = 200; + }) + + Text(this.change) + .fontSize(20) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(300) + .textAlign(TextAlign.End) + .margin({ right: 16 }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/ParameterInput.ets b/entry/src/main/ets/pages/ParameterInput.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1ceda8b48097393745c65448aafdd56f19d659a --- /dev/null +++ b/entry/src/main/ets/pages/ParameterInput.ets @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Param:组件外部输入 + * 参数输入 + * 02-参数输入 + */ +@ComponentV2 +struct MyCounter { + @Param title: string = ''; + @Param value: number = 0; + + build() { + Row() { + Text(`${this.title}:`) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text(this.value.toString()) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + } + } +} + +@Entry +@ComponentV2 +struct ParameterInput { + @Local value1: number = 0; + @Local value2: number = 0; + + build() { + Column({ space: 20 }) { + Text($r('app.string.counter')) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .margin({ bottom: 60 }) + + MyCounter({ title: 'value1', value: this.value1 }) + MyCounter({ title: 'value2', value: this.value2 }) + + Row({ space: 20 }) { + Button($r('app.string.count', '-1')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.value1--; + this.value2--; + }) + + Button($r('app.string.count', '+1')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.value1++; + this.value2++; + }) + }.margin({ top: 60 }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/StateCacheAfter.ets b/entry/src/main/ets/pages/StateCacheAfter.ets new file mode 100644 index 0000000000000000000000000000000000000000..b2beb139502496e03fb1fe1525af4ea838ad329e --- /dev/null +++ b/entry/src/main/ets/pages/StateCacheAfter.ets @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Computed装饰器:计算属性 + * 状态缓存优化后 + * 05-使用状态缓存 + */ +@Entry +@ComponentV2 +struct StateCacheAfter { + @Local count: number = 0; + price: number = 15; + + @Computed + get getTotal(): number { + return this.count * this.price; + } + + build() { + Column({ space: 20 }) { + Row() { + Text(this.count.toString()) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + + Text($r('app.string.counts')) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + } + + Row() { + Text($r('app.string.unit_price')) + .fontSize(40) + .fontColor('#69687A') + .textAlign(TextAlign.Start) + + Text(this.price.toString()) + .fontSize(40) + .fontColor('#69687A') + .textAlign(TextAlign.Start) + } + + Row() { + Text($r('app.string.money')) + .fontSize(40) + .fontColor('#1E40AF') + .fontWeight(FontWeight.Bold) + .textAlign(TextAlign.Start) + + Text(this.getTotal.toString()) + .fontSize(40) + .fontColor('#1E40AF') + .fontWeight(FontWeight.Bold) + .textAlign(TextAlign.Start) + } + + Row({ space: 20 }) { + Button($r('app.string.count', '-1')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + if (this.count > 0) { + this.count--; + } + }) + + Button($r('app.string.count', '+1')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.count++; + }) + } + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/StateCacheBefore.ets b/entry/src/main/ets/pages/StateCacheBefore.ets new file mode 100644 index 0000000000000000000000000000000000000000..b75350a19eb954b085805ba17ab3316b622e34bd --- /dev/null +++ b/entry/src/main/ets/pages/StateCacheBefore.ets @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * 状态缓存优化前 + * 05-使用状态缓存 + */ +@Entry +@ComponentV2 +struct StateCacheBefore { + @Local count: number = 0; + price: number = 15; + @Local total: number = 0; + + build() { + Column({ space: 20 }) { + Row() { + Text(this.count.toString()) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text($r('app.string.counts')) + .fontSize(40) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + } + + Row() { + Text($r('app.string.unit_price')) + .fontSize(40) + .fontColor('#69687A') + .textAlign(TextAlign.Start) + + Text(this.price.toString()) + .fontSize(40) + .fontColor('#69687A') + .textAlign(TextAlign.Start) + } + + Row() { + Text($r('app.string.money')) + .fontSize(40) + .fontColor('#1E40AF') + .fontWeight(FontWeight.Bold) + .textAlign(TextAlign.Start) + + Text(this.total.toString()) + .fontSize(40) + .fontColor('#1E40AF') + .fontWeight(FontWeight.Bold) + .textAlign(TextAlign.Start) + } + + Row({ space: 20 }) { + Button($r('app.string.count', '-1')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + if (this.count > 0) { + this.count--; + this.total = this.count * this.price; + } + }) + + Button($r('app.string.count', '+1')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.count++; + this.total = this.count * this.price; + }) + } + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/StateSynchronizationAfter.ets b/entry/src/main/ets/pages/StateSynchronizationAfter.ets new file mode 100644 index 0000000000000000000000000000000000000000..23c845ee90ed944b6dd0971b2ba58b5de6479c73 --- /dev/null +++ b/entry/src/main/ets/pages/StateSynchronizationAfter.ets @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Provider装饰器和@Consumer装饰器:跨组件层级双向同步 + * 状态同步优化后 + * 04-跨组件共享状态-状态同步 + */ +@ComponentV2 +struct Child { + @Consumer('index') index: number = 0; + @Event changeIndex: (val: number) => void; + + build() { + Column({ space: 10 }) { + Text('Child') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text(`Child index:${this.index}`) + .fontSize(36) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Button('Change to 20') + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.changeIndex(20); + }) + } + .backgroundColor(Color.White) + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#87B6EE') + } +} + +@ComponentV2 +struct ChildList { + @Param index: number = 0; + @Event changeIndex: (val: number) => void; + + build() { + Column({ space: 10 }) { + Text('ChildList') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + + Child({ changeIndex: this.changeIndex }) + Child({ changeIndex: this.changeIndex }) + Child({ changeIndex: this.changeIndex }) + } + .backgroundColor('#EDF5FD') + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#3B82F6') + } +} + +@Entry +@ComponentV2 +struct Parent { + @Provider('index') index: number = 0; + + build() { + Column({ space: 20 }) { + Text('Parent') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + ChildList({ + changeIndex: (val: number) => { + this.index = val; + } + }) + } + .width('100%') + .height('100%') + .backgroundColor($r('app.color.pages_background')) + .padding({ top: 20 }) + .justifyContent(FlexAlign.Start) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/StateSynchronizationBefore.ets b/entry/src/main/ets/pages/StateSynchronizationBefore.ets new file mode 100644 index 0000000000000000000000000000000000000000..1dbd4ed25a3d81fa70c1a2eec4518f1e300e61e6 --- /dev/null +++ b/entry/src/main/ets/pages/StateSynchronizationBefore.ets @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Param:组件外部输入 + * 状态同步优化前 + * 04-跨组件共享状态-状态同步 + */ +@ComponentV2 +struct Child { + @Param index: number = 0; + @Event changeIndex: (val: number) => void; + + build() { + Column({ space: 10 }) { + Text('Child') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text(`Child index:${this.index}`) + .fontSize(36) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Button('Change to 20') + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.changeIndex(20); + }) + } + .backgroundColor(Color.White) + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#87B6EE') + } +} + +@ComponentV2 +struct ChildList { + @Param index: number = 0; + @Event changeIndex: (val: number) => void; + + build() { + Column({ space: 10 }) { + Text('ChildList') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + + Child({ index: this.index, changeIndex: this.changeIndex }) + Child({ index: this.index, changeIndex: this.changeIndex }) + Child({ index: this.index, changeIndex: this.changeIndex }) + } + .backgroundColor('#EDF5FD') + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#3B82F6') + } +} + +@Entry +@ComponentV2 +struct Parent { + @Local index: number = 0; + + build() { + Column({ space: 20 }) { + Text('Parent') + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + ChildList({ + index: this.index, + changeIndex: (val: number) => { + this.index = val; + } + }) + } + .width('100%') + .height('100%') + .backgroundColor($r('app.color.pages_background')) + .padding({ top: 20 }) + .justifyContent(FlexAlign.Start) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/Status.ets b/entry/src/main/ets/pages/Status.ets new file mode 100644 index 0000000000000000000000000000000000000000..5a2d1f64f2e4ccc90ff440a941bf7d9ef26876ac --- /dev/null +++ b/entry/src/main/ets/pages/Status.ets @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * 开关状态&计数器 + * 01-什么是状态 + */ +@ComponentV2 +struct SwitchStatus { + @Local isOpen: boolean = true; + + build() { + Column({ space: 10 }) { + Text($r('app.string.switch_status')) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Column() { + Text(this.isOpen ? $r('app.string.OPEN') : $r('app.string.CLOSE')) + .fontSize(24) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Toggle({ type: ToggleType.Switch, isOn: true }) + .selectedColor($r('app.color.toggle_selected_color')) + .switchPointColor($r('app.color.toggle_switch_point_color')) + .onChange(() => { + this.isOpen = !this.isOpen; + }) + } + .width(100) + .height(100) + .justifyContent(FlexAlign.Center) + .borderRadius(12) + .backgroundColor('#EEF6FD') + } + .backgroundColor(Color.White) + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#87B6EE') + } +} + +@ComponentV2 +struct Counters { + @Local index: number = 0; + + build() { + Column({ space: 10 }) { + Text($r('app.string.counter')) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + Text(this.index.toString()) + .width(80) + .height(80) + .textAlign(TextAlign.Center) + .fontSize(36) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .borderRadius(12) + .backgroundColor('#EEF6FD') + Button('+1') + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.index++; + }) + } + .backgroundColor(Color.White) + .padding(20) + .borderRadius(12) + .borderWidth(2) + .borderColor('#87B6EE') + } +} + +@Entry +@ComponentV2 +struct Status { + build() { + Column({ space: 100 }) { + SwitchStatus() + Counters() + } + .width('100%') + .height('100%') + .padding({ top: 20 }) + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/StatusOfManagementObjectsAfter.ets b/entry/src/main/ets/pages/StatusOfManagementObjectsAfter.ets new file mode 100644 index 0000000000000000000000000000000000000000..d861df38a9a40ad5e19052b60b88b1b61ba4f800 --- /dev/null +++ b/entry/src/main/ets/pages/StatusOfManagementObjectsAfter.ets @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @ObservedV2装饰器和@Trace装饰器:类属性变化观测 + * 管理对象状态优化后 + * 07-管理对象状态 + */ +@ObservedV2 +class Student { + name: ResourceStr = ''; + age: number = 17; + @Trace major: ResourceStr = ''; + + constructor(name: ResourceStr, age: number, major: ResourceStr) { + this.name = name; + this.age = age; + this.major = major; + } +} + +@Entry +@ComponentV2 +struct StatusOfManagementObjectsAfter { + @Local student: Student = new Student($r('app.string.student_name'), 18, $r('app.string.software_engineering')); + + build() { + Column({ space: 20 }) { + Row() { + Text($r('app.string.names')) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(140) + .textAlign(TextAlign.End) + .margin({ right: 16 }) + Text(this.student.name) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(200) + .textAlign(TextAlign.Start) + } + + Row() { + Text($r('app.string.age')) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(140) + .textAlign(TextAlign.End) + .margin({ right: 16 }) + Text(this.student.age.toString()) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(200) + .textAlign(TextAlign.Start) + } + + Row() { + Text($r('app.string.major')) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(140) + .textAlign(TextAlign.End) + .margin({ right: 16 }) + Text(this.student.major) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(200) + .textAlign(TextAlign.Start) + } + + Button($r('app.string.change_major')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .height(50) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.student.major = $r('app.string.internet_of_things'); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/StatusOfManagementObjectsBefore.ets b/entry/src/main/ets/pages/StatusOfManagementObjectsBefore.ets new file mode 100644 index 0000000000000000000000000000000000000000..2731d02d53af158afc3250add017a2f30d5651b5 --- /dev/null +++ b/entry/src/main/ets/pages/StatusOfManagementObjectsBefore.ets @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @Local装饰器:组件内部状态 + * 管理对象状态优化前 + * 07-管理对象状态 + */ +class Student { + name: ResourceStr = ''; + age: number = 17; + major: ResourceStr = ''; + + constructor(name: ResourceStr, age: number, major: ResourceStr) { + this.name = name; + this.age = age; + this.major = major; + } +} + +@Entry +@ComponentV2 +struct StatusOfManagementObjectsBefore { + @Local student: Student = new Student($r('app.string.student_name'), 18, $r('app.string.software_engineering')); + + build() { + Column({ space: 20 }) { + Row() { + Text($r('app.string.names')) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(140) + .textAlign(TextAlign.End) + .margin({ right: 16 }) + Text(this.student.name) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(200) + .textAlign(TextAlign.Start) + } + + Row() { + Text($r('app.string.age')) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(140) + .textAlign(TextAlign.End) + .margin({ right: 16 }) + Text(this.student.age.toString()) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(200) + .textAlign(TextAlign.Start) + } + + Row() { + Text($r('app.string.major')) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(140) + .textAlign(TextAlign.End) + .margin({ right: 16 }) + Text(this.student.major) + .fontSize(34) + .fontWeight(FontWeight.Bold) + .fontColor('#1E40AF') + .width(200) + .textAlign(TextAlign.Start) + } + + Button($r('app.string.change_major')) + .fontSize(24) + .fontWeight(FontWeight.Medium) + .fontColor(Color.White) + .height(50) + .backgroundColor('#3B82F6') + .borderRadius(12) + .onClick(() => { + this.student.major = $r('app.string.internet_of_things'); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/module.json5 b/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1ce402d1aaf72c8642cadf077437b9d5a3e67820 --- /dev/null +++ b/entry/src/main/module.json5 @@ -0,0 +1,36 @@ +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "ohos.want.action.home" + ] + } + ] + } + ] + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/element/color.json b/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..d4967344d0dea2b871004cce4b2ed591918cdd93 --- /dev/null +++ b/entry/src/main/resources/base/element/color.json @@ -0,0 +1,20 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + }, + { + "name": "pages_background", + "value": "#F1F3F5" + }, + { + "name": "toggle_selected_color", + "value": "#007DFF" + }, + { + "name": "toggle_switch_point_color", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/base/element/float.json b/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..a0a93dd91fd48f08f3a9532c76e9b26e68d4c034 --- /dev/null +++ b/entry/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5438be4c796a64bacc23d54b53e2c04afc5415a3 --- /dev/null +++ b/entry/src/main/resources/base/element/string.json @@ -0,0 +1,76 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "StatusV2" + }, + { + "name": "EntryAbility_desc", + "value": "StatusV2" + }, + { + "name": "EntryAbility_label", + "value": "StatusV2" + }, + { + "name": "counter", + "value": "counter" + }, + { + "name": "count", + "value": "%s count" + }, + { + "name": "counts", + "value": " counts \uD83C\uDF4E" + }, + { + "name": "unit_price", + "value": "unit price:" + }, + { + "name": "money", + "value": "money:" + }, + { + "name": "switch_status", + "value": "Switch Status" + }, + { + "name": "OPEN", + "value": "OPEN" + }, + { + "name": "CLOSE", + "value": "CLOSE" + }, + { + "name": "student_name", + "value": "Li hua" + }, + { + "name": "names", + "value": "Name:" + }, + { + "name": "software_engineering", + "value": "Software Engineering" + }, + { + "name": "age", + "value": "Age:" + }, + { + "name": "major", + "value": "Major:" + }, + { + "name": "internet_of_things", + "value": "Internet of Things" + }, + { + "name": "change_major", + "value": "Change major to: IOT" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/base/media/background.png b/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/entry/src/main/resources/base/media/background.png differ diff --git a/entry/src/main/resources/base/media/foreground.png b/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/entry/src/main/resources/base/media/foreground.png differ diff --git a/entry/src/main/resources/base/media/layered_image.json b/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/media/startIcon.png b/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/entry/src/main/resources/base/media/startIcon.png differ diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..dbb0a8a3c5ce7285dc17cf58f4f362460231e44b --- /dev/null +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Status" + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/en_US/element/string.json b/entry/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5438be4c796a64bacc23d54b53e2c04afc5415a3 --- /dev/null +++ b/entry/src/main/resources/en_US/element/string.json @@ -0,0 +1,76 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "StatusV2" + }, + { + "name": "EntryAbility_desc", + "value": "StatusV2" + }, + { + "name": "EntryAbility_label", + "value": "StatusV2" + }, + { + "name": "counter", + "value": "counter" + }, + { + "name": "count", + "value": "%s count" + }, + { + "name": "counts", + "value": " counts \uD83C\uDF4E" + }, + { + "name": "unit_price", + "value": "unit price:" + }, + { + "name": "money", + "value": "money:" + }, + { + "name": "switch_status", + "value": "Switch Status" + }, + { + "name": "OPEN", + "value": "OPEN" + }, + { + "name": "CLOSE", + "value": "CLOSE" + }, + { + "name": "student_name", + "value": "Li hua" + }, + { + "name": "names", + "value": "Name:" + }, + { + "name": "software_engineering", + "value": "Software Engineering" + }, + { + "name": "age", + "value": "Age:" + }, + { + "name": "major", + "value": "Major:" + }, + { + "name": "internet_of_things", + "value": "Internet of Things" + }, + { + "name": "change_major", + "value": "Change major to: IOT" + } + ] +} \ No newline at end of file diff --git a/entry/src/main/resources/zh_CN/element/string.json b/entry/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..da571f4cb56a889a416a8c8495a4a2de5e2e8afd --- /dev/null +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,76 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "状态管理V2-原子级场景案例" + }, + { + "name": "counter", + "value": "计数器" + }, + { + "name": "count", + "value": "%s 个" + }, + { + "name": "counts", + "value": " 个 \uD83C\uDF4E" + }, + { + "name": "unit_price", + "value": "单价:" + }, + { + "name": "money", + "value": "金额:" + }, + { + "name": "switch_status", + "value": "开关状态" + }, + { + "name": "OPEN", + "value": "打开" + }, + { + "name": "CLOSE", + "value": "关闭" + }, + { + "name": "student_name", + "value": "李华" + }, + { + "name": "names", + "value": "姓名:" + }, + { + "name": "software_engineering", + "value": "软件工程" + }, + { + "name": "age", + "value": "年龄:" + }, + { + "name": "major", + "value": "专业:" + }, + { + "name": "internet_of_things", + "value": "物联网" + }, + { + "name": "change_major", + "value": "修改专业为:物联网" + } + ] +} \ No newline at end of file diff --git a/hvigor/hvigor-config.json5 b/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7a7ab8914d8db6ab89758e185df5855dffe88d04 --- /dev/null +++ b/hvigor/hvigor-config.json5 @@ -0,0 +1,23 @@ +{ + "modelVersion": "6.0.0", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | "ultrafine" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + // "optimizationStrategy": "memory" /* Define the optimization strategy. Value: [ "memory" | "performance" ]. Default: "memory" */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/hvigorfile.ts b/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..47113e2e36ecefde41c136272a0bd6ff745cffe4 --- /dev/null +++ b/hvigorfile.ts @@ -0,0 +1,6 @@ +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins: [] /* Custom plugin to extend the functionality of Hvigor. */ +} \ No newline at end of file diff --git a/oh-package.json5 b/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c4dae13f1bd0296f3e9ad4f3c21547ecba3611af --- /dev/null +++ b/oh-package.json5 @@ -0,0 +1,8 @@ +{ + "modelVersion": "6.0.0", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + } +} \ No newline at end of file diff --git a/screenshots/device/result.png b/screenshots/device/result.png new file mode 100644 index 0000000000000000000000000000000000000000..4bb9479286127ff3f03c06d9d7974c3360fb2b99 Binary files /dev/null and b/screenshots/device/result.png differ diff --git a/screenshots/device/result_en.png b/screenshots/device/result_en.png new file mode 100644 index 0000000000000000000000000000000000000000..87a91120c8fa245d3e32b751f7093422d603e39a Binary files /dev/null and b/screenshots/device/result_en.png differ