From 7d55949f7f0b6e1dc5e490e160ef8824c3956591 Mon Sep 17 00:00:00 2001 From: tongzihan Date: Wed, 3 Sep 2025 19:32:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8B=89=E8=B5=B7photo=20pic?= =?UTF-8?q?ker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build-profile.json5 | 4 ++ commons/commons/.gitignore | 6 +++ commons/commons/Index.ets | 2 + commons/commons/build-profile.json5 | 10 +++++ commons/commons/hvigorfile.ts | 6 +++ commons/commons/oh-package.json5 | 9 ++++ .../commons/src/main/ets/utils/FileUtils.ets | 39 ++++++++++++++++ commons/commons/src/main/ets/utils/Logger.ets | 45 +++++++++++++++++++ commons/commons/src/main/module.json5 | 9 ++++ .../main/resources/base/element/float.json | 8 ++++ .../main/resources/base/element/string.json | 8 ++++ features/home/oh-package.json5 | 4 +- .../src/main/ets/view/CommentKeyboard.ets | 26 ++++++++--- oh-package.json5 | 5 +-- 14 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 commons/commons/.gitignore create mode 100644 commons/commons/Index.ets create mode 100644 commons/commons/build-profile.json5 create mode 100644 commons/commons/hvigorfile.ts create mode 100644 commons/commons/oh-package.json5 create mode 100644 commons/commons/src/main/ets/utils/FileUtils.ets create mode 100644 commons/commons/src/main/ets/utils/Logger.ets create mode 100644 commons/commons/src/main/module.json5 create mode 100644 commons/commons/src/main/resources/base/element/float.json create mode 100644 commons/commons/src/main/resources/base/element/string.json diff --git a/build-profile.json5 b/build-profile.json5 index 1782299..9bfb1ea 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -41,6 +41,10 @@ { "name": "home", "srcPath": "./features/home", + }, + { + "name": "commons", + "srcPath": "./commons/commons" } ] } \ No newline at end of file diff --git a/commons/commons/.gitignore b/commons/commons/.gitignore new file mode 100644 index 0000000..e2713a2 --- /dev/null +++ b/commons/commons/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/commons/commons/Index.ets b/commons/commons/Index.ets new file mode 100644 index 0000000..b4a404b --- /dev/null +++ b/commons/commons/Index.ets @@ -0,0 +1,2 @@ +export { Logger } from './src/main/ets/utils/Logger'; +export { selectImagesFromAlbum } from './src/main/ets/utils/FileUtils' diff --git a/commons/commons/build-profile.json5 b/commons/commons/build-profile.json5 new file mode 100644 index 0000000..79961f9 --- /dev/null +++ b/commons/commons/build-profile.json5 @@ -0,0 +1,10 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "targets": [ + { + "name": "default" + } + ] +} diff --git a/commons/commons/hvigorfile.ts b/commons/commons/hvigorfile.ts new file mode 100644 index 0000000..805c5d7 --- /dev/null +++ b/commons/commons/hvigorfile.ts @@ -0,0 +1,6 @@ +import { harTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: harTasks, /* 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/commons/commons/oh-package.json5 b/commons/commons/oh-package.json5 new file mode 100644 index 0000000..d50184a --- /dev/null +++ b/commons/commons/oh-package.json5 @@ -0,0 +1,9 @@ +{ + "name": "commons", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "Index.ets", + "author": "", + "license": "Apache-2.0", + "dependencies": {} +} diff --git a/commons/commons/src/main/ets/utils/FileUtils.ets b/commons/commons/src/main/ets/utils/FileUtils.ets new file mode 100644 index 0000000..a9d38a3 --- /dev/null +++ b/commons/commons/src/main/ets/utils/FileUtils.ets @@ -0,0 +1,39 @@ +/* + * 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 { photoAccessHelper } from '@kit.MediaLibraryKit'; +import { BusinessError } from '@kit.BasicServicesKit'; +import { Logger } from './Logger'; + +const TAG_LOG = 'FileUtil'; + +/** + * Select a images from the album + * + * @returns {string[]} return selected image uris + */ +export function selectImagesFromAlbum(maxNumber: number = 5): Promise { + const photoPicker = new photoAccessHelper.PhotoViewPicker(); + const photoSelectOptions: photoAccessHelper.PhotoSelectOptions = { + MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE, + maxSelectNumber: maxNumber + }; + return photoPicker.select(photoSelectOptions).then(photoSelectResult => { + return photoSelectResult.photoUris; + }).catch((e: BusinessError) => { + Logger.error(TAG_LOG, `Failed to select images from album: ${e.code}, ${e.message}`); + return []; + }); +} \ No newline at end of file diff --git a/commons/commons/src/main/ets/utils/Logger.ets b/commons/commons/src/main/ets/utils/Logger.ets new file mode 100644 index 0000000..c769a60 --- /dev/null +++ b/commons/commons/src/main/ets/utils/Logger.ets @@ -0,0 +1,45 @@ +/* + * 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 { hilog } from '@kit.PerformanceAnalysisKit'; + +export class Logger { + private static readonly DOMAIN: number = 0xFF00; + private static readonly TAG: string = 'com.example.customcamera'; + private static readonly PREFIX: string = '[camera-log]'; + + public static debug(logTag: string, messageFormat: string, ...args: Object[]): void { + hilog.debug(Logger.DOMAIN, Logger.TAG, `${Logger.PREFIX} ${logTag}: ${messageFormat}`, args); + } + + public static info(logTag: string, messageFormat: string, ...args: Object[]): void { + hilog.info(Logger.DOMAIN, Logger.TAG, `${Logger.PREFIX} ${logTag}: ${messageFormat}`, args); + } + + public static warn(logTag: string, messageFormat: string, ...args: Object[]): void { + hilog.warn(Logger.DOMAIN, Logger.TAG, `${Logger.PREFIX} ${logTag}: ${messageFormat}`, args); + } + + public static error(logTag: string, messageFormat: string, ...args: Object[]): void { + hilog.error(Logger.DOMAIN, Logger.TAG, `${Logger.PREFIX} ${logTag}: ${messageFormat}`, args); + } + + public static fatal(logTag: string, messageFormat: string, ...args: Object[]): void { + hilog.fatal(Logger.DOMAIN, Logger.TAG, `${Logger.PREFIX} ${logTag}: ${messageFormat}`, args); + } + + private constructor() { + } +} diff --git a/commons/commons/src/main/module.json5 b/commons/commons/src/main/module.json5 new file mode 100644 index 0000000..1adf09b --- /dev/null +++ b/commons/commons/src/main/module.json5 @@ -0,0 +1,9 @@ +{ + "module": { + "name": "commons", + "type": "har", + "deviceTypes": [ + "default" + ] + } +} diff --git a/commons/commons/src/main/resources/base/element/float.json b/commons/commons/src/main/resources/base/element/float.json new file mode 100644 index 0000000..33ea223 --- /dev/null +++ b/commons/commons/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/commons/commons/src/main/resources/base/element/string.json b/commons/commons/src/main/resources/base/element/string.json new file mode 100644 index 0000000..f51a9c8 --- /dev/null +++ b/commons/commons/src/main/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "page_show", + "value": "page from package" + } + ] +} diff --git a/features/home/oh-package.json5 b/features/home/oh-package.json5 index 3f480ab..ddab6f1 100644 --- a/features/home/oh-package.json5 +++ b/features/home/oh-package.json5 @@ -5,5 +5,7 @@ "main": "Index.ets", "author": "", "license": "Apache-2.0", - "dependencies": {} + "dependencies": { + "commons": "file:../../commons/commons" + } } diff --git a/features/home/src/main/ets/view/CommentKeyboard.ets b/features/home/src/main/ets/view/CommentKeyboard.ets index 49d0917..f3899b6 100644 --- a/features/home/src/main/ets/view/CommentKeyboard.ets +++ b/features/home/src/main/ets/view/CommentKeyboard.ets @@ -15,6 +15,9 @@ import { window } from "@kit.ArkUI"; import { NavigationDialog } from "./NavigationDialog"; +import { Logger, selectImagesFromAlbum } from 'commons' + +const TAG = 'CommentKeyboard'; interface OperateButton { icon: Resource @@ -59,6 +62,8 @@ export struct CommentKeyboard { @State isEmojiKeyboardVisible: boolean = false; @State isAtFriendListVisible: boolean = false; @Consume navDialogPageInfos: NavPathStack; + // When the photo picker is pulled up, the soft keyboard will also be dismissed, but no need to exit the comment page + @State isPhotoPickerVisible: boolean = false; // [Start comment_keyboard1] aboutToAppear(): void { @@ -79,7 +84,7 @@ export struct CommentKeyboard { addKeyboardHeightListener(win: window.Window) { win.on('keyboardHeightChange', height => { - console.info('keyboard height has changed', this.getUIContext().px2vp(height)); + Logger.info(TAG, 'keyboard height has changed', this.getUIContext().px2vp(height)); if (height !== 0) { this.keyboardHeight = this.getUIContext().px2vp(height); return; @@ -87,8 +92,10 @@ export struct CommentKeyboard { // [StartExclude comment_keyboard1] // if close keyboard, don't set keyboardHeight, avoid EmojiKeyboard height is 0 if (!this.isEmojiKeyboardVisible) { - // handle keyboard close button click - console.info('click soft keyboard close button'); + // When the photo picker is pulled up, the soft keyboard will also be dismissed, but no need to exit the comment page + if (this.isPhotoPickerVisible) return; + // handle system keyboard close button click + Logger.info(TAG, 'click soft keyboard close button'); this.navDialogPageInfos.pop(); } // [EndExclude comment_keyboard1] @@ -107,11 +114,20 @@ export struct CommentKeyboard { icon: this.isEmojiKeyboardVisible ? $r('app.media.keyboard_circle') : $r('app.media.face'), onClick: this.onEmojiButtonClick }, - { icon: $r('app.media.picture') }, + { icon: $r('app.media.picture'), onClick: this.onPictureButtonClick }, { icon: $r('app.media.paper_plane'), onClick: this.onSendComment } ]; } + onPictureButtonClick: () => void = () => { + this.isPhotoPickerVisible = true; + selectImagesFromAlbum(1).then(uris => { + Logger.info(TAG, JSON.stringify(uris)); + }).finally(() => { + this.isPhotoPickerVisible = false; + }); + } + // [Start comment_keyboard2] // features/home/src/main/ets/view/CommentKeyboard.ets onAtButtonClick: (event?: ClickEvent) => void = event => { @@ -162,7 +178,7 @@ export struct CommentKeyboard { richEditorSpans.push(richEditorSpan); }); // [StartExclude comment_keyboard4] - console.info('richEditorContent', JSON.stringify(richEditorSpans)); + Logger.info(TAG, 'richEditorContent', JSON.stringify(richEditorSpans)); this.navDialogPageInfos.pop(); this.navDialogPageInfos.pushPathByName('CommentSendDialog', richEditorSpans); // [EndExclude comment_keyboard4] diff --git a/oh-package.json5 b/oh-package.json5 index bb12751..0cec982 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -3,8 +3,5 @@ "description": "Please describe the basic information.", "dependencies": { }, - "devDependencies": { - "@ohos/hypium": "1.0.19", - "@ohos/hamock": "1.0.0" - } + "devDependencies": {} } -- Gitee