From ec474c43d90825057ff0b56e89749cbe7bb0fbd2 Mon Sep 17 00:00:00 2001 From: DevWiki Date: Thu, 18 Apr 2024 18:44:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=9E=B6=E6=9E=84=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0base=20=E5=9F=BA=E7=A1=80=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/oh-package.json5 | 3 +- app/src/main/ets/appability/AppAbility.ets | 1 - app/src/main/ets/pages/Index.ets | 30 ++++++++--------- app/src/main/ets/pages/PhotoPage.ets | 1 - .../pages/animation/CompTransitionPage.ets | 2 +- app/src/main/ets/pages/web/WebPage.ets | 3 +- app/src/main/ets/pages/web/WebPageDialog.ets | 2 +- base/.gitignore | 6 ++++ base/Index.ets | 8 +++++ base/build-profile.json5 | 25 ++++++++++++++ base/hvigorfile.ts | 6 ++++ base/obfuscation-rules.txt | 18 ++++++++++ base/oh-package.json5 | 11 +++++++ .../src/main/ets/event/Emitter.ets | 6 ++-- .../src/main/ets/event/EventKey.ets | 0 base/src/main/ets/pages/Index.ets | 17 ++++++++++ .../src/main/ets/utils/BaseLocalStorage.ets | 22 ++++++------- {app => base}/src/main/ets/utils/Log.ets | 0 .../src/main/ets/utils/ScreenUtil.ets | 9 +++++ base/src/main/module.json5 | 14 ++++++++ .../main/resources/base/element/color.json | 8 +++++ .../main/resources/base/element/string.json | 8 +++++ .../resources/base/profile/main_pages.json | 5 +++ base/src/test/List.test.ets | 5 +++ base/src/test/LocalUnit.test.ets | 33 +++++++++++++++++++ build-profile.json5 | 12 +++++++ common_ui/Index.ets | 5 +-- common_ui/oh-package.json5 | 1 + readme.md | 5 +-- 29 files changed, 225 insertions(+), 41 deletions(-) create mode 100644 base/.gitignore create mode 100644 base/Index.ets create mode 100644 base/build-profile.json5 create mode 100644 base/hvigorfile.ts create mode 100644 base/obfuscation-rules.txt create mode 100644 base/oh-package.json5 rename {common_ui => base}/src/main/ets/event/Emitter.ets (90%) rename {common_ui => base}/src/main/ets/event/EventKey.ets (100%) create mode 100644 base/src/main/ets/pages/Index.ets rename app/src/main/ets/utils/LocalStorage.ets => base/src/main/ets/utils/BaseLocalStorage.ets (75%) rename {app => base}/src/main/ets/utils/Log.ets (100%) rename {common_ui => base}/src/main/ets/utils/ScreenUtil.ets (93%) create mode 100644 base/src/main/module.json5 create mode 100644 base/src/main/resources/base/element/color.json create mode 100644 base/src/main/resources/base/element/string.json create mode 100644 base/src/main/resources/base/profile/main_pages.json create mode 100644 base/src/test/List.test.ets create mode 100644 base/src/test/LocalUnit.test.ets diff --git a/app/oh-package.json5 b/app/oh-package.json5 index eb7a723..058388d 100644 --- a/app/oh-package.json5 +++ b/app/oh-package.json5 @@ -6,7 +6,8 @@ "author": "", "license": "", "dependencies": { - "@devwiki/common_ui": "file:../common_ui" + "@devwiki/common_ui": "file:../common_ui", + "@devwiki/base": "file:../base" } } diff --git a/app/src/main/ets/appability/AppAbility.ets b/app/src/main/ets/appability/AppAbility.ets index 17c91fc..ebf3029 100644 --- a/app/src/main/ets/appability/AppAbility.ets +++ b/app/src/main/ets/appability/AppAbility.ets @@ -1,7 +1,6 @@ import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; -import { ScreenUtil } from '@devwiki/common_ui/Index'; export default class AppAbility extends UIAbility { constructor() { diff --git a/app/src/main/ets/pages/Index.ets b/app/src/main/ets/pages/Index.ets index 0a90e34..a3aa619 100644 --- a/app/src/main/ets/pages/Index.ets +++ b/app/src/main/ets/pages/Index.ets @@ -1,20 +1,18 @@ -import { ComponentConst, ScreenUtil, TitleBar } from '@devwiki/common_ui'; +import { TitleBar } from '@devwiki/common_ui'; import web_webview from '@ohos.web.webview'; import { TitleBarMenuType } from '@devwiki/common_ui'; -import { WebPage } from './web/WebPage'; import { router, window } from '@kit.ArkUI'; -import { WebPageDialog } from './web/WebPageDialog' import promptAction from '@ohos.promptAction'; import { BusinessError } from '@ohos.base'; -import { Log } from '../utils/Log'; -import { DemoLocalStorage } from '../utils/LocalStorage'; +import { Log } from '@devwiki/base'; +import { BaseLocalStorage, ScreenUtil } from '@devwiki/base'; @CustomDialog struct WebViewDialog { webUrl: string = "https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/password" private webviewController: web_webview.WebviewController = new web_webview.WebviewController(); dialogController: CustomDialogController; - @StorageLink(ScreenUtil.isPortraitKey)isPortrait: boolean = true; + @StorageLink(ScreenUtil.isPortraitKey) isPortrait: boolean = true; build() { Row() { @@ -29,9 +27,7 @@ struct WebViewDialog { @Entry @Component struct Index { - @State title: string = "Home"; - dialogController: CustomDialogController = new CustomDialogController({ builder: WebViewDialog(), alignment: DialogAlignment.BottomEnd, @@ -40,7 +36,7 @@ struct Index { aboutToAppear(): void { ScreenUtil.getInstance().initScreenSize(); - DemoLocalStorage.getInstance().init(getContext(this).getApplicationContext()); + BaseLocalStorage.getInstance().init(getContext(this)); } onPageShow(): void { @@ -97,21 +93,25 @@ struct Index { Column() { TitleBar({ title: this.title, - onLeftClicked: ()=> { this.onBackPress() }, + onLeftClicked: () => { + this.onBackPress() + }, rightMenuType: TitleBarMenuType.None }).width('100%') Column() { Row() { Button("WebDialog").width(100).height('100%').type(ButtonType.Normal) - .onClick(() => { this.dialogController.open() }); + .onClick(async () => { + this.dialogController.open() + }); - Button("Layout").width(100).height('100%').type(ButtonType.Normal).onClick(() =>{ - router.pushUrl({url: "pages/layout/LinearLayoutPage"}); + Button("Layout").width(100).height('100%').type(ButtonType.Normal).onClick(() => { + router.pushUrl({ url: "pages/layout/LinearLayoutPage" }); }); - Button("Animation").width(100).height('100%').type(ButtonType.Normal).onClick(() =>{ - router.pushUrl({url: "pages/animation/CompTransitionPage"}); + Button("Animation").width(100).height('100%').type(ButtonType.Normal).onClick(() => { + router.pushUrl({ url: "pages/animation/CompTransitionPage" }); }); }.width('100%').height(48).justifyContent(FlexAlign.SpaceEvenly) diff --git a/app/src/main/ets/pages/PhotoPage.ets b/app/src/main/ets/pages/PhotoPage.ets index 2b646fa..ed6f83c 100644 --- a/app/src/main/ets/pages/PhotoPage.ets +++ b/app/src/main/ets/pages/PhotoPage.ets @@ -2,7 +2,6 @@ import { TitleBar } from '@devwiki/common_ui/Index'; import picker from '@ohos.file.picker'; import { BusinessError } from '@kit.BasicServicesKit'; import fs, { ReadOptions } from '@ohos.file.fs'; -import common from '@ohos.app.ability.common'; import buffer from '@ohos.buffer'; @Entry diff --git a/app/src/main/ets/pages/animation/CompTransitionPage.ets b/app/src/main/ets/pages/animation/CompTransitionPage.ets index 493b336..4556e80 100644 --- a/app/src/main/ets/pages/animation/CompTransitionPage.ets +++ b/app/src/main/ets/pages/animation/CompTransitionPage.ets @@ -1,4 +1,4 @@ -import { CommonMediaName, CommonRes, TitleBar, TitleBarMenuType } from '@devwiki/common_ui/Index' +import { CommonRes, TitleBar, TitleBarMenuType } from '@devwiki/common_ui' import { curves, router } from '@kit.ArkUI'; diff --git a/app/src/main/ets/pages/web/WebPage.ets b/app/src/main/ets/pages/web/WebPage.ets index 55f428a..d1a2f38 100644 --- a/app/src/main/ets/pages/web/WebPage.ets +++ b/app/src/main/ets/pages/web/WebPage.ets @@ -1,4 +1,5 @@ -import { WebView, WebViewParam, ComponentConst, CommonRes, TitleBar, WebViewController, ScreenUtil } from '@devwiki/common_ui/Index'; +import { WebView, WebViewParam, ComponentConst, CommonRes, TitleBar, WebViewController } from '@devwiki/common_ui/Index'; +import { ScreenUtil } from '@devwiki/base' @Entry @Component diff --git a/app/src/main/ets/pages/web/WebPageDialog.ets b/app/src/main/ets/pages/web/WebPageDialog.ets index 9aac0bd..ec2344c 100644 --- a/app/src/main/ets/pages/web/WebPageDialog.ets +++ b/app/src/main/ets/pages/web/WebPageDialog.ets @@ -1,4 +1,4 @@ -import { ScreenUtil } from '@devwiki/common_ui/Index'; +import { ScreenUtil } from '@devwiki/base'; import { WebPage } from './WebPage'; @CustomDialog diff --git a/base/.gitignore b/base/.gitignore new file mode 100644 index 0000000..e2713a2 --- /dev/null +++ b/base/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/base/Index.ets b/base/Index.ets new file mode 100644 index 0000000..8bb5b35 --- /dev/null +++ b/base/Index.ets @@ -0,0 +1,8 @@ +export { Log } from "./src/main/ets/utils/Log" + +export { ScreenUtil } from './src/main/ets/utils/ScreenUtil' + +export { BaseLocalStorage, BaseLocalStorageKey } from './src/main/ets/utils/BaseLocalStorage' + +export { Emitter, EmitterItem } from './src/main/ets/event/Emitter' +export { WebViewEventKey } from './src/main/ets/event/EventKey' \ No newline at end of file diff --git a/base/build-profile.json5 b/base/build-profile.json5 new file mode 100644 index 0000000..9a68355 --- /dev/null +++ b/base/build-profile.json5 @@ -0,0 +1,25 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": true, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + }, + ], + "targets": [ + { + "name": "default" + } + ] +} \ No newline at end of file diff --git a/base/hvigorfile.ts b/base/hvigorfile.ts new file mode 100644 index 0000000..d993120 --- /dev/null +++ b/base/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hspTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hspTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/base/obfuscation-rules.txt b/base/obfuscation-rules.txt new file mode 100644 index 0000000..985b2ae --- /dev/null +++ b/base/obfuscation-rules.txt @@ -0,0 +1,18 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope \ No newline at end of file diff --git a/base/oh-package.json5 b/base/oh-package.json5 new file mode 100644 index 0000000..77a5e19 --- /dev/null +++ b/base/oh-package.json5 @@ -0,0 +1,11 @@ +{ + "name": "base", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "Index.ets", + "author": "", + "license": "Apache-2.0", + "packageType": "InterfaceHar", + "dependencies": { + } +} \ No newline at end of file diff --git a/common_ui/src/main/ets/event/Emitter.ets b/base/src/main/ets/event/Emitter.ets similarity index 90% rename from common_ui/src/main/ets/event/Emitter.ets rename to base/src/main/ets/event/Emitter.ets index aaf7084..8c4f519 100644 --- a/common_ui/src/main/ets/event/Emitter.ets +++ b/base/src/main/ets/event/Emitter.ets @@ -4,12 +4,12 @@ export interface EmitterItem { listener: Function } -type XYEmitterAllType = string | object | boolean | number | Object | Number | BigInt; +type EmitterAllType = string | object | boolean | number | Object | Number | BigInt; export class Emitter { private static instance: Emitter; private static events: Map = new Map - private static viscosityData: Map = new Map + private static viscosityData: Map = new Map public static getInstance(): Emitter { if (!Emitter.instance) { @@ -87,7 +87,7 @@ export class Emitter { */ public emmitViscosity(key: string, ...args: T[]) { this.emmit(key, ...args); - Emitter.viscosityData.set(key, args as XYEmitterAllType[]); + Emitter.viscosityData.set(key, args as EmitterAllType[]); } /** diff --git a/common_ui/src/main/ets/event/EventKey.ets b/base/src/main/ets/event/EventKey.ets similarity index 100% rename from common_ui/src/main/ets/event/EventKey.ets rename to base/src/main/ets/event/EventKey.ets diff --git a/base/src/main/ets/pages/Index.ets b/base/src/main/ets/pages/Index.ets new file mode 100644 index 0000000..423b427 --- /dev/null +++ b/base/src/main/ets/pages/Index.ets @@ -0,0 +1,17 @@ +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/app/src/main/ets/utils/LocalStorage.ets b/base/src/main/ets/utils/BaseLocalStorage.ets similarity index 75% rename from app/src/main/ets/utils/LocalStorage.ets rename to base/src/main/ets/utils/BaseLocalStorage.ets index 0843ed6..a897c99 100644 --- a/app/src/main/ets/utils/LocalStorage.ets +++ b/base/src/main/ets/utils/BaseLocalStorage.ets @@ -3,24 +3,24 @@ import { Context } from '@kit.AbilityKit'; import dataPreferences from '@ohos.data.preferences'; import { Log } from './Log'; -export class DemoLocalStorageKey { +export class BaseLocalStorageKey { static readonly KEY_SERVER_CONFIG = "key_config"; } -export class DemoLocalStorage { +export class BaseLocalStorage { - private static instance: DemoLocalStorage; + private static instance: BaseLocalStorage; private static lock: boolean = false; private static readonly XY_DP_Name = "LocalData"; - static getInstance(): DemoLocalStorage { - if (!DemoLocalStorage.instance) { - if (!DemoLocalStorage.lock) { - DemoLocalStorage.lock = true; - DemoLocalStorage.instance = new DemoLocalStorage(); - DemoLocalStorage.lock = false; + static getInstance(): BaseLocalStorage { + if (!BaseLocalStorage.instance) { + if (!BaseLocalStorage.lock) { + BaseLocalStorage.lock = true; + BaseLocalStorage.instance = new BaseLocalStorage(); + BaseLocalStorage.lock = false; } } - return DemoLocalStorage.instance; + return BaseLocalStorage.instance; } private context: Context | null = null; @@ -31,7 +31,7 @@ export class DemoLocalStorage { public init(context: Context): void { if (!this.context) { this.context = context.getApplicationContext(); - let options: dataPreferences.Options = { name: DemoLocalStorage.XY_DP_Name }; + let options: dataPreferences.Options = { name: BaseLocalStorage.XY_DP_Name }; this.preferences = dataPreferences.getPreferencesSync(this.context, options); } else { Log.i("LocalStorage is already init.") diff --git a/app/src/main/ets/utils/Log.ets b/base/src/main/ets/utils/Log.ets similarity index 100% rename from app/src/main/ets/utils/Log.ets rename to base/src/main/ets/utils/Log.ets diff --git a/common_ui/src/main/ets/utils/ScreenUtil.ets b/base/src/main/ets/utils/ScreenUtil.ets similarity index 93% rename from common_ui/src/main/ets/utils/ScreenUtil.ets rename to base/src/main/ets/utils/ScreenUtil.ets index 39f9cbb..716ae93 100644 --- a/common_ui/src/main/ets/utils/ScreenUtil.ets +++ b/base/src/main/ets/utils/ScreenUtil.ets @@ -1,5 +1,6 @@ import window from '@ohos.window'; import mediaquery from '@ohos.mediaquery'; +import { Log } from './Log'; export class ScreenUtil { // TODO: 工具待抽离, key统一定义 @@ -103,4 +104,12 @@ export class ScreenUtil { .catch((error: Error) => { }) } + + setWindowKeepScreenOn(isKeep: boolean) { + Log.i(`setWindowKeepScreenOn:${isKeep}`) + window.getLastWindow(getContext(this)) + .then((windowClass: window.Window) => { + windowClass.setWindowKeepScreenOn(isKeep); + }); + } } \ No newline at end of file diff --git a/base/src/main/module.json5 b/base/src/main/module.json5 new file mode 100644 index 0000000..16427c0 --- /dev/null +++ b/base/src/main/module.json5 @@ -0,0 +1,14 @@ +{ + "module": { + "name": "base", + "type": "shared", + "description": "$string:shared_desc", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "pages": "$profile:main_pages" + } +} \ No newline at end of file diff --git a/base/src/main/resources/base/element/color.json b/base/src/main/resources/base/element/color.json new file mode 100644 index 0000000..1bbc9aa --- /dev/null +++ b/base/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "white", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/base/src/main/resources/base/element/string.json b/base/src/main/resources/base/element/string.json new file mode 100644 index 0000000..98e1d8a --- /dev/null +++ b/base/src/main/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "shared_desc", + "value": "description" + } + ] +} \ No newline at end of file diff --git a/base/src/main/resources/base/profile/main_pages.json b/base/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000..1898d94 --- /dev/null +++ b/base/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/base/src/test/List.test.ets b/base/src/test/List.test.ets new file mode 100644 index 0000000..bb5b5c3 --- /dev/null +++ b/base/src/test/List.test.ets @@ -0,0 +1,5 @@ +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/base/src/test/LocalUnit.test.ets b/base/src/test/LocalUnit.test.ets new file mode 100644 index 0000000..ed22d4d --- /dev/null +++ b/base/src/test/LocalUnit.test.ets @@ -0,0 +1,33 @@ +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest',() => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/build-profile.json5 b/build-profile.json5 index e7164ce..5a43391 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -67,6 +67,18 @@ ] } ] + }, + { + "name": "base", + "srcPath": "./base", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] } ] } \ No newline at end of file diff --git a/common_ui/Index.ets b/common_ui/Index.ets index d96820f..9b3d1ea 100644 --- a/common_ui/Index.ets +++ b/common_ui/Index.ets @@ -1,8 +1,5 @@ export { TitleBar, TitleBarMenuType } from './src/main/ets/component/TitleBar' export { ComponentConst } from './src/main/ets/component/ComponentConst' -export { WebViewEventKey } from './src/main/ets/event/EventKey' -export { Emitter } from './src/main/ets/event/Emitter' +export { WebViewEventKey } from '../base/src/main/ets/event/EventKey' export { WebView, WebViewController, WebViewParam } from './src/main/ets/component/web/WebView' - export { CommonRes, CommonMediaName } from './src/main/ets/utils/CommonRes' -export { ScreenUtil } from './src/main/ets/utils/ScreenUtil' diff --git a/common_ui/oh-package.json5 b/common_ui/oh-package.json5 index 6c90df7..b4ffe47 100644 --- a/common_ui/oh-package.json5 +++ b/common_ui/oh-package.json5 @@ -7,5 +7,6 @@ "license": "Apache-2.0", "packageType": "InterfaceHar", "dependencies": { + "@devwiki/base": "file:../base" } } \ No newline at end of file diff --git a/readme.md b/readme.md index c47def8..cde3604 100644 --- a/readme.md +++ b/readme.md @@ -3,6 +3,7 @@ 本项目模块分为: - app: 主入口模块 - common_ui : 通用UI动态共享库模块 +- base: 最底层的依赖库, 包含最通用的代码封装. ## app模块 @@ -19,8 +20,8 @@ - 自定标题栏: [TitleBar](common_ui/src/main/ets/component/TitleBar.ets) 事件相关: -- 事件发射器 [Emitter](common_ui/src/main/ets/event/Emitter.ets) +- 事件发射器 [Emitter](base/src/main/ets/event/Emitter.ets) 工具类: - 公用资源调用类[CommonRes](common_ui/src/main/ets/utils/CommonRes.ets) -- 屏幕相关[ScreenUtil](common_ui/src/main/ets/utils/ScreenUtil.ets) \ No newline at end of file +- 屏幕相关[ScreenUtil](base/src/main/ets/utils/ScreenUtil.ets) \ No newline at end of file