• 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 */
15import router from '@system.router';
16import { Log } from '@ohos/base/src/main/ets/utils/Log';
17import screenManager from '@ohos/base/src/main/ets/manager/ScreenManager'
18import { WindowConstants } from '@ohos/base/src/main/ets/constants/WindowConstants';
19import { MediaConstants } from '@ohos/base/src/main/ets/constants/MediaConstants';
20import { Broadcast } from '@ohos/base/src/main/ets/utils/Broadcast';
21import { BroadcastConstants } from '@ohos/base/src/main/ets/constants/BroadcastConstants';
22import broadcastManager from '@ohos/base/src/main/ets/manager/BroadcastManager';
23import { EmptyAlbumComponent } from '@ohos/base/src/main/ets/components/EmptyAlbumComponent';
24import { AlbumDataItem } from '@ohos/base/src/main/ets/data/AlbumDataItem';
25// The item of album grid, and it's new style.
26const TAG = "ThirdAlbumGridItem"
27
28@Component
29export struct ThirdAlbumGridItem {
30    private staticIconList = new Map([
31        [MediaConstants.ALBUM_ID_VIDEO, $r('app.media.ic_video')],
32        [MediaConstants.ALBUM_ID_FAVOR, $r('app.media.ic_favorite_overlay')]
33    ]);
34    item: AlbumDataItem;
35    @Consume isMultiPick: boolean;
36    @Provide isBigCard: boolean = false;
37    @Provide gridHeight: number = 0;
38    isFromWallpaper: boolean;
39    @Consume maxSelectCount: number;
40    isFromFa: boolean;
41    isFromFaPhoto: boolean;
42    private appBroadcast: Broadcast = broadcastManager.getBroadcast();
43    private gridAspectRatio = WindowConstants.CARD_ASPECT_RATIO;
44    private albumCountMarginRight = WindowConstants.ALBUM_SET_NEW_ICON_SIZE + WindowConstants.ALBUM_SET_NEW_ICON_MARGIN * 2;
45    private iconMarkAnchorX = WindowConstants.ALBUM_SET_NEW_ICON_SIZE + WindowConstants.ALBUM_SET_NEW_ICON_MARGIN;
46    private iconMarkAnchorY = WindowConstants.ALBUM_SET_NEW_ICON_SIZE + WindowConstants.ALBUM_SET_NEW_ICON_MARGIN;
47    @State bigWidth: number = 0;
48    @State cardWidth: number = 0;
49    @State cardHeight: number = 0;
50    @State transformV: number = 0;
51    @State isEmptyAlbum: boolean = false;
52    @State thumbnail: string = "";
53    bundleName: string = "";
54    filterMediaType: number;
55    onWindowSizeChangeCallBack = () => this.updateCardSize();
56
57    aboutToAppear(): void {
58        Log.info(TAG, `aboutToAppear + ${this.item.uri}`)
59        screenManager.on(screenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeCallBack);
60        this.updateCardSize();
61        this.item.load().then(() => {
62            this.thumbnail = this.item.getThumbnail()
63        })
64    }
65
66    aboutToDisappear(): void {
67        screenManager.off(screenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeCallBack);
68        this.onWindowSizeChangeCallBack = null;
69    }
70
71    updateCardSize() {
72        let contentWidth = screenManager.getWinWidth();
73        let maxCardWidth = WindowConstants.ALBUM_SET_COVER_SIZE * WindowConstants.GRID_MAX_SIZE_RATIO;
74        let count: number = Math.ceil((contentWidth - WindowConstants.ALBUM_SET_MARGIN * 2 + WindowConstants.ALBUM_SET_GUTTER) /
75        (maxCardWidth + WindowConstants.ALBUM_SET_GUTTER));
76        let gridWidth = ((contentWidth - WindowConstants.ALBUM_SET_MARGIN * 2 + WindowConstants.ALBUM_SET_GUTTER) / count) -
77        WindowConstants.ALBUM_SET_GUTTER;
78        this.gridHeight = gridWidth / this.gridAspectRatio;
79        this.bigWidth = gridWidth * 2 + WindowConstants.ALBUM_SET_GUTTER;
80
81        if (this.isBigCard) {
82            this.cardWidth = this.bigWidth;
83        } else {
84            this.cardWidth = gridWidth;
85        }
86        this.cardHeight = this.gridHeight;
87        this.transformV = 0;
88
89        Log.info(TAG, `is big card : ${this.isBigCard}`);
90        Log.info(TAG, `little grid size is: ${gridWidth}x${this.gridHeight}`);
91    }
92
93    build() {
94        Stack({ alignContent: Alignment.Bottom }) {
95            if (this.isEmptyAlbum) {
96                EmptyAlbumComponent()
97            }
98
99            if (this.isBigCard) {
100                Row() {
101                    Image(this.thumbnail)
102                        .alt($r('app.media.ic_goto_photos'))
103                        .width(this.cardWidth)
104                        .height(this.cardHeight)
105                        .transform({ y: this.transformV })
106                        .border({ radius: $r('sys.float.ohos_id_corner_radius_default_l') })
107                        .rotate({
108                            centerX: this.cardWidth / 2,
109                            centerY: this.cardHeight / 2,
110                            z: 1,
111                            angle: 0
112                        })
113                        .fillColor($r('app.color.empty_or_recycle_album_icon'))
114                        .onError(() => {
115                            Log.error(TAG, 'album is empty or its cover is error');
116                            this.isEmptyAlbum = true;
117                        })
118                }
119                .width(this.bigWidth)
120                .height(this.gridHeight)
121                .justifyContent(FlexAlign.Center)
122                .alignItems(VerticalAlign.Center)
123            } else {
124                Row() {
125                    Image(this.thumbnail)
126                        .alt($r('app.media.ic_goto_photos'))
127                        .width(this.cardWidth)
128                        .height(this.cardHeight)
129                        .translate({ x: this.transformV })
130                        .border({ radius: $r('sys.float.ohos_id_corner_radius_default_l') })
131                        .rotate({
132                            centerX: this.cardWidth / 2,
133                            centerY: this.cardHeight / 2,
134                            z: 1,
135                            angle: 0
136                        })
137                        .fillColor($r('app.color.empty_or_recycle_album_icon'))
138                        .onError(() => {
139                            Log.error(TAG, 'album is empty or its cover is error');
140                            this.isEmptyAlbum = true;
141                        })
142                }
143                .height(this.gridHeight)
144                .justifyContent(FlexAlign.Center)
145                .alignItems(VerticalAlign.Center)
146            }
147
148            Column() {
149                Text(this.item.displayName)
150                    .margin({ right: $r('app.float.album_set_name_margin_right') })
151                    .textOverflow({ overflow: TextOverflow.Ellipsis })
152                    .maxLines(1)
153                    .fontWeight(FontWeight.Medium)
154                    .fontSize($r('sys.float.ohos_id_text_size_sub_title1'))
155                    .fontColor($r('app.color.white'))
156
157                Text(String(this.item.count))
158                    .margin({ right: this.albumCountMarginRight })
159                    .textOverflow({ overflow: TextOverflow.Ellipsis })
160                    .maxLines(1)
161                    .fontColor($r('app.color.white'))
162                    .fontWeight(FontWeight.Regular)
163                    .fontSize($r('sys.float.ohos_id_text_size_body2'))
164            }
165            .width('100%')
166            .alignItems(HorizontalAlign.Start)
167            .margin({ left: $r('app.float.album_set_count_margin_left'),
168                bottom: $r('app.float.album_set_count_margin_bottom') })
169
170
171            if (this.staticIconList.has(this.item.id)) {
172                Image(this.staticIconList.get(this.item.id))
173                    .height($r('app.float.album_set_new_style_icon'))
174                    .aspectRatio(1)
175                    .position({ x: '100%', y: '100%' })
176                    .markAnchor({ x: this.iconMarkAnchorX, y: this.iconMarkAnchorY })
177            }
178
179            Column()
180                .height('100%')
181                .width('100%')
182                .border({ radius: $r('sys.float.ohos_id_corner_radius_default_l') })
183                .backgroundColor($r('app.color.transparent'))
184        }
185        .height(this.gridHeight)
186        .border({
187            radius: $r('sys.float.ohos_id_corner_radius_default_l'),
188            width: $r('app.float.album_cover_stroke_width'),
189            color: $r('app.color.album_cover_stroke_color')
190        })
191        .onClick(() => {
192            if (this.isFromFa && (!this.isFromFaPhoto)) {
193                this.appBroadcast.emit(BroadcastConstants.SAVE_FORM_EDITOR_DATA,
194                    [this.item.displayName, this.item.id, this.item.displayName, 0, true]);
195            } else {
196                router.push({
197                    uri: 'feature/thirdSelect/view/ThirdSelectPhotoGridPage',
198                    params: {
199                        itemCoverUri: this.item.uri,
200                        itemId: this.item.id,
201                        itemCount: this.item.count,
202                        isMultiPick: this.isMultiPick,
203                        isFromWallpaper: this.isFromWallpaper,
204                        maxSelectCount: this.maxSelectCount,
205                        itemDisplayName: this.item.displayName,
206                        isFromFa: this.isFromFa,
207                        isFromFaPhoto: this.isFromFaPhoto,
208                        bundleName: this.bundleName,
209                        filterMediaType: this.filterMediaType
210                    }
211                });
212            }
213        })
214    }
215}