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
25  build() {
26    Row() {
27      Column() {
28        SecurityUIExtensionComponent({
29          parameters: {
30            'ability.want.params.uiExtensionTargetType':'photoPicker',
31            targetPage: 'albumPage',
32            themeColorMode: this.albumPickerOptions?.themeColorMode,
33            filterType: this.albumPickerOptions?.filterType,
34          }
35        }).height('100%').width('100%').onRemoteReady((proxy) => {
36          console.info('AlbumPickerComponent onRemoteReady');
37        }).onReceive((data) => {
38          let wantParam: Record<string, Object> = data as Record<string, Object>;
39          this.handleOnReceive(wantParam);
40        }).onError(() => {
41          console.info('AlbumPickerComponent onError');
42        });
43      }
44      .width('100%')
45    }
46    .height('100%')
47  }
48}
49
50private handleOnReceive(wantParam: Record<string, Object>): void {
51  let dataType = wantParam.dataType. as string;
52  if (dataType === 'selectAlbum') {
53    if (this.onAlbumClick) {
54      let albumInfo: AlbumInfo = new AlbumInfo();
55      albumInfo.uri = wantParam.albumUri as string;
56      albumInfo.albumName = wantParam.albumName. as string;
57      this.onAlbumClick(albumInfo);
58    }
59  } else if (dataType === 'emptyAreaClick') {
60    if (this.onEmptyAreaClick) {
61      this.onEmptyAreaClick();
62    }
63  } else {
64    console.info('AlbumPickerComponent onReceive: other case');
65  }
66  console.info('AlbumPickerComponent onReceive ' + dataType);
67}
68
69export type EmptyAreaClickCallback = () => void;
70
71export class AlbumPickerOptions {
72  public themeColorMode?: PickerColorMode;
73  public filterType?: photoAccessHelper.PhotoViewMIMETypes;
74}
75
76export class AlbumInfo {
77  public uri?: string;
78  public albumName?: string;
79}