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 56 aboutToAppear(): void { 57 Log.info(TAG, `aboutToAppear + ${this.item.uri}`) 58 screenManager.on(screenManager.ON_WIN_SIZE_CHANGED, (size) => this.updateCardSize()) 59 this.updateCardSize(); 60 this.item.load().then(() => { 61 this.thumbnail = this.item.getThumbnail() 62 }) 63 } 64 65 updateCardSize() { 66 let contentWidth = screenManager.getWinWidth(); 67 let maxCardWidth = WindowConstants.ALBUM_SET_COVER_SIZE * WindowConstants.GRID_MAX_SIZE_RATIO; 68 let count: number = Math.ceil((contentWidth - WindowConstants.ALBUM_SET_MARGIN * 2 + WindowConstants.ALBUM_SET_GUTTER) / 69 (maxCardWidth + WindowConstants.ALBUM_SET_GUTTER)); 70 let gridWidth = ((contentWidth - WindowConstants.ALBUM_SET_MARGIN * 2 + WindowConstants.ALBUM_SET_GUTTER) / count) - 71 WindowConstants.ALBUM_SET_GUTTER; 72 this.gridHeight = gridWidth / this.gridAspectRatio; 73 this.bigWidth = gridWidth * 2 + WindowConstants.ALBUM_SET_GUTTER; 74 75 if (this.isBigCard) { 76 this.cardWidth = this.bigWidth; 77 } else { 78 this.cardWidth = gridWidth; 79 } 80 this.cardHeight = this.gridHeight; 81 this.transformV = 0; 82 83 Log.info(TAG, `is big card : ${this.isBigCard}`); 84 Log.info(TAG, `little grid size is: ${gridWidth}x${this.gridHeight}`); 85 } 86 87 build() { 88 Stack({ alignContent: Alignment.Bottom }) { 89 if (this.isEmptyAlbum) { 90 EmptyAlbumComponent() 91 } 92 93 if (this.isBigCard) { 94 Row() { 95 Image(this.thumbnail) 96 .alt($r('app.media.ic_goto_photos')) 97 .width(this.cardWidth) 98 .height(this.cardHeight) 99 .transform({ y: this.transformV }) 100 .border({ radius: $r('sys.float.ohos_id_corner_radius_default_l') }) 101 .rotate({ 102 centerX: this.cardWidth / 2, 103 centerY: this.cardHeight / 2, 104 z: 1, 105 angle: 0 106 }) 107 .fillColor($r('app.color.empty_or_recycle_album_icon')) 108 .onError(() => { 109 Log.error(TAG, 'album is empty or its cover is error'); 110 this.isEmptyAlbum = true; 111 }) 112 } 113 .width(this.bigWidth) 114 .height(this.gridHeight) 115 .justifyContent(FlexAlign.Center) 116 .alignItems(VerticalAlign.Center) 117 } else { 118 Row() { 119 Image(this.thumbnail) 120 .alt($r('app.media.ic_goto_photos')) 121 .width(this.cardWidth) 122 .height(this.cardHeight) 123 .translate({ x: this.transformV }) 124 .border({ radius: $r('sys.float.ohos_id_corner_radius_default_l') }) 125 .rotate({ 126 centerX: this.cardWidth / 2, 127 centerY: this.cardHeight / 2, 128 z: 1, 129 angle: 0 130 }) 131 .fillColor($r('app.color.empty_or_recycle_album_icon')) 132 .onError(() => { 133 Log.error(TAG, 'album is empty or its cover is error'); 134 this.isEmptyAlbum = true; 135 }) 136 } 137 .height(this.gridHeight) 138 .justifyContent(FlexAlign.Center) 139 .alignItems(VerticalAlign.Center) 140 } 141 142 Column() { 143 Text(this.item.displayName) 144 .margin({ right: $r('app.float.album_set_name_margin_right') }) 145 .textOverflow({ overflow: TextOverflow.Ellipsis }) 146 .maxLines(1) 147 .fontWeight(FontWeight.Medium) 148 .fontSize($r('sys.float.ohos_id_text_size_sub_title1')) 149 .fontColor($r('app.color.white')) 150 151 Text(String(this.item.count)) 152 .margin({ right: this.albumCountMarginRight }) 153 .textOverflow({ overflow: TextOverflow.Ellipsis }) 154 .maxLines(1) 155 .fontColor($r('app.color.white')) 156 .fontWeight(FontWeight.Regular) 157 .fontSize($r('sys.float.ohos_id_text_size_body2')) 158 } 159 .width('100%') 160 .alignItems(HorizontalAlign.Start) 161 .margin({ left: $r('app.float.album_set_count_margin_left'), 162 bottom: $r('app.float.album_set_count_margin_bottom') }) 163 164 165 if (this.staticIconList.has(this.item.id)) { 166 Image(this.staticIconList.get(this.item.id)) 167 .height($r('app.float.album_set_new_style_icon')) 168 .aspectRatio(1) 169 .position({ x: '100%', y: '100%' }) 170 .markAnchor({ x: this.iconMarkAnchorX, y: this.iconMarkAnchorY }) 171 } 172 173 Column() 174 .height('100%') 175 .width('100%') 176 .border({ radius: $r('sys.float.ohos_id_corner_radius_default_l') }) 177 .backgroundColor($r('app.color.transparent')) 178 } 179 .height(this.gridHeight) 180 .border({ 181 radius: $r('sys.float.ohos_id_corner_radius_default_l'), 182 width: $r('app.float.album_cover_stroke_width'), 183 color: $r('app.color.album_cover_stroke_color') 184 }) 185 .onClick(() => { 186 if (this.isFromFa && (!this.isFromFaPhoto)) { 187 this.appBroadcast.emit(BroadcastConstants.SAVE_FORM_EDITOR_DATA, 188 [this.item.displayName, this.item.id, this.item.displayName, 0, true]); 189 } else { 190 router.push({ 191 uri: 'feature/thirdSelect/view/ThirdSelectPhotoGridPage', 192 params: { 193 itemCoverUri: this.item.uri, 194 itemId: this.item.id, 195 itemCount: this.item.count, 196 isMultiPick: this.isMultiPick, 197 isFromWallpaper: this.isFromWallpaper, 198 maxSelectCount: this.maxSelectCount, 199 itemDisplayName: this.item.displayName, 200 isFromFa: this.isFromFa, 201 bundleName: this.bundleName, 202 filterMediaType: this.filterMediaType 203 } 204 }); 205 } 206 }) 207 } 208}