1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { PickerColorMode } from '@ohos.file.PhotoPickerComponent'; 17import photoAccessHelper from '@ohos.file.photoAccessHelper'; 18 19@Component 20export struct AlbumPickerComponent { 21 albumPickerOptions?: AlbumPickerOptions | undefined; 22 onAlbumClick?: (albumInfo: AlbumInfo) => boolean; 23 onEmptyAreaClick?: EmptyAreaClickCallback; 24 @Watch('onChanged') albumPickerController?: AlbumPickerController; 25 private proxy: UIExtensionProxy | undefined; 26 27 build() { 28 Row() { 29 Column() { 30 SecurityUIExtensionComponent({ 31 parameters: { 32 'ability.want.params.uiExtensionTargetType':'photoPicker', 33 targetPage: 'albumPage', 34 themeColorMode: this.albumPickerOptions?.themeColorMode, 35 filterType: this.albumPickerOptions?.filterType, 36 fontSize: this.albumPickerOptions?.fontSize, 37 } 38 }).height('100%').width('100%').onRemoteReady((proxy) => { 39 this.proxy = proxy; 40 console.info('AlbumPickerComponent onRemoteReady'); 41 }).onReceive((data) => { 42 let wantParam: Record<string, Object> = data as Record<string, Object>; 43 this.handleOnReceive(wantParam); 44 }).onError(() => { 45 console.info('AlbumPickerComponent onError'); 46 }); 47 } 48 .width('100%') 49 } 50 .height('100%') 51 } 52} 53 54private onChanged(): void { 55 if (!this.proxy) { 56 return; 57 } 58 let data = this.albumPickerController?.data; 59 if (data?.has('SET_FONT_SIZE')) { 60 this.proxy.send({ 'fontSize': data?.get('SET_FONT_SIZE') as number | string }); 61 console.info('AlbumPickerComponent onChanged: SET_FONT_SIZE'); 62 } 63 64private handleOnReceive(wantParam: Record<string, Object>): void { 65 let dataType = wantParam.dataType. as string; 66 if (dataType === 'selectAlbum') { 67 if (this.onAlbumClick) { 68 let albumInfo: AlbumInfo = new AlbumInfo(); 69 albumInfo.uri = wantParam.albumUri as string; 70 albumInfo.albumName = wantParam.albumName. as string; 71 this.onAlbumClick(albumInfo); 72 } 73 } else if (dataType === 'emptyAreaClick') { 74 if (this.onEmptyAreaClick) { 75 this.onEmptyAreaClick(); 76 } 77 } else { 78 console.info('AlbumPickerComponent onReceive: other case'); 79 } 80 console.info('AlbumPickerComponent onReceive ' + dataType); 81} 82 83export type EmptyAreaClickCallback = () => void; 84 85export class AlbumPickerOptions { 86 public themeColorMode?: PickerColorMode; 87 public filterType?: photoAccessHelper.PhotoViewMIMETypes; 88 public fontSize?: number | string; 89} 90 91export class AlbumInfo { 92 public uri?: string; 93 public albumName?: string; 94} 95 96@Observed 97export class AlbumPickerController { 98 data?: Map<string, Object>; 99 100 setFontSize(fontSize: number | string) { 101 this.data = new Map([['SET_FONT_SIZE', fontSize]]); 102 } 103}