• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 { Log } from '@ohos/base/src/main/ets/utils/Log';
17import { Broadcast } from '@ohos/base/src/main/ets/utils/Broadcast';
18import { BroadcastConstants } from '@ohos/base/src/main/ets/constants/BroadcastConstants';
19import { AlbumDataItem } from '@ohos/base/src/main/ets/data/AlbumDataItem';
20
21const TAG = "AlbumListCard"
22
23@Component
24export struct AlbumListCard {
25    @State useAlt: boolean = false;
26    @Consume broadCast: Broadcast;
27    item: AlbumDataItem;
28    @State thumbnail: string = "";
29    @State mediaRes: Resource = undefined;
30
31    aboutToAppear(): void {
32        Log.info(TAG, `album mediaSet ${JSON.stringify(this.item)}`);
33        this.item.load().then(() => {
34            this.thumbnail = this.item.getThumbnail();
35        })
36        this.item.getVideoCount().then((videoCount: number) => {
37            this.mediaRes = this.showMediaRes(videoCount);
38        })
39    }
40
41    mediaOperation() {
42        this.broadCast.emit(BroadcastConstants.MEDIA_OPERATION, [this.item]);
43    }
44
45    showMediaRes(videoCount: number): Resource {
46        if (videoCount == 0) {
47            return $r('app.plural.show_photo_num', this.item.count, this.item.count);
48        } else if (videoCount == this.item.count) {
49            return $r('app.plural.show_video_num', this.item.count, this.item.count);
50        } else if (videoCount == 1) {
51            return $r('app.plural.show_one_video_with_photo_num', this.item.count - videoCount,
52                this.item.count - videoCount, videoCount);
53        } else {
54            return $r('app.plural.show_multi_video_with_photo_num', this.item.count - videoCount,
55                this.item.count - videoCount, videoCount)
56        }
57    }
58
59    build() {
60        Row() {
61            if (this.useAlt) {
62                Row() {
63                    Image($r('app.media.ic_goto_photos'))
64                        .height($r('app.float.icon_size'))
65                        .width($r('app.float.icon_size'))
66                        .objectFit(ImageFit.Cover)
67                }
68                .justifyContent(FlexAlign.Center)
69                .backgroundColor($r('app.color.default_background_color'))
70                .borderRadius($r('sys.float.ohos_id_corner_radius_default_s'))
71                .height($r('app.float.list_card_image_size'))
72                .width($r('app.float.list_card_image_size'))
73            } else {
74                Image(this.thumbnail)
75                    .height($r('app.float.list_card_image_size'))
76                    .width($r('app.float.list_card_image_size'))
77                    .borderRadius($r('sys.float.ohos_id_corner_radius_default_s'))
78                    .onError(() => {
79                        if (this.thumbnail) {
80                            this.useAlt = true;
81                        }
82                        Log.error(TAG, 'alt Image error');
83                    })
84            }
85            Column() {
86                Text(this.item.displayName)
87                    .fontSize($r('sys.float.ohos_id_text_size_body1'))
88                    .fontColor($r('sys.color.ohos_id_color_primary'))
89                    .padding({
90                        bottom: $r('sys.float.ohos_id_text_margin_vertical')
91                    })
92                if (this.item.count != 0) {
93                    Text(this.mediaRes)
94                        .fontSize($r('sys.float.ohos_id_text_size_body2'))
95                        .fontColor($r('sys.color.ohos_id_color_text_secondary'))
96                }
97            }
98            .alignItems(HorizontalAlign.Start)
99            .padding({
100                left: $r('app.float.crop_vertical_padding'),
101            })
102        }
103        .alignItems(VerticalAlign.Center)
104        .width('100%')
105        .height($r('app.float.list_card_height'))
106        .onClick(() => {
107            this.mediaOperation();
108        })
109    }
110}