1/* 2 * Copyright (c) 2022-2023 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 { 17 AlbumDefine, 18 AlbumInfo, 19 BroadCast, 20 BroadCastConstants, 21 BroadCastManager, 22 Constants, 23 Log, 24 ScreenManager, 25 UiUtil 26} from '@ohos/common'; 27import router from '@ohos.router'; 28import { SelectParams } from '../utils/ThirdSelectConstants'; 29import { EmptyAlbumComponent } from '@ohos/browser'; 30import Router from '@system.router'; 31 32const TAG: string = 'thiSel_ThirdAlbumGridItem'; 33 34// The item of album grid, and it's new style. 35@Component 36export struct ThirdAlbumGridItem { 37 @State item: AlbumInfo = new AlbumInfo(undefined); 38 @Provide gridHeight: number = 0; 39 @Provide gridWidth: number = 0; 40 gridAspectRatio = Constants.CARD_ASPECT_RATIO; 41 albumCountMarginRight = Constants.ALBUM_SET_NEW_ICON_SIZE + Constants.ALBUM_SET_NEW_ICON_MARGIN * 2; 42 iconMarkAnchorX = Constants.ALBUM_SET_NEW_ICON_SIZE + Constants.ALBUM_SET_NEW_ICON_MARGIN; 43 iconMarkAnchorY = Constants.ALBUM_SET_NEW_ICON_SIZE + Constants.ALBUM_SET_NEW_ICON_MARGIN; 44 MASK_LAYER_HEIGHT = '50%'; 45 @State transformV: number = 0; 46 @State isEmptyAlbum: boolean = false; 47 isFirstEnter: boolean; 48 @Consume selectParams: SelectParams; 49 private staticIconList = new Map([ 50 [AlbumDefine.ALBUM_ID_VIDEO, $r('app.media.ic_video')], 51 [AlbumDefine.ALBUM_ID_FAVOR, $r('app.media.ic_favorite_overlay')] 52 ]); 53 private appBroadCast: BroadCast = BroadCastManager.getInstance().getBroadCast(); 54 private fp2vpUnit: number = px2vp(fp2px(Constants.NUMBER_1)); 55 onWindowSizeChangeCallBack = () => this.updateCardSize(); 56 57 aboutToAppear(): void { 58 Log.info(TAG, `aboutToAppear + ${this.item.coverUri}, select param ${JSON.stringify(this.selectParams)}`) 59 // 后续phone缩略图支持横竖屏后再放开 60 if (AppStorage.Get('deviceType') as string !== Constants.DEFAULT_DEVICE_TYPE) { 61 ScreenManager.getInstance().on(ScreenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeCallBack); 62 } 63 this.updateCardSize(); 64 } 65 66 aboutToDisappear(): void { 67 Log.info(TAG, 'aboutToDisappear'); 68 ScreenManager.getInstance().off(ScreenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeCallBack); 69 this.onWindowSizeChangeCallBack = null; 70 } 71 72 updateCardSize() { 73 let contentWidth = ScreenManager.getInstance().getWinWidth(); 74 let count: number = UiUtil.getAlbumGridCount(false); 75 this.gridWidth = ((contentWidth - Constants.ALBUM_SET_MARGIN * 2 - (Constants.ALBUM_SET_GUTTER * (count - 1))) / count); 76 let numberHeight = Constants.TEXT_SIZE_BODY2 * this.fp2vpUnit; 77 let nameHeight = Constants.TEXT_SIZE_SUB_TITLE1 * this.fp2vpUnit; 78 79 UiUtil.getResourceNumber($r('sys.float.ohos_id_elements_margin_vertical_m')).then((value: number) => { 80 let bottomHeight = px2vp(value) + nameHeight + Constants.NUMBER_2 + numberHeight; 81 this.iconMarkAnchorY = Constants.ALBUM_SET_NEW_ICON_SIZE + Constants.ALBUM_SET_NEW_ICON_MARGIN + bottomHeight; 82 this.gridHeight = this.gridWidth + bottomHeight; 83 Log.info(TAG, `updateCardSize gridWidth : ${this.gridWidth}, count : ${count}`); 84 this.transformV = 0; 85 }); 86 } 87 88 @Builder buildNormalCard() { 89 Flex({ 90 direction: FlexDirection.Row, 91 justifyContent: FlexAlign.Center, 92 alignItems: ItemAlign.Start 93 }) { 94 Image(this.item.coverUri) 95 .width(this.gridWidth) 96 .aspectRatio(this.gridAspectRatio) 97 .translate({ x: this.transformV }) 98 .border({ radius: $r('sys.float.ohos_id_corner_radius_default_m') }) 99 .rotate({ 100 centerX: this.gridWidth / Constants.NUMBER_2, 101 centerY: this.gridWidth / Constants.NUMBER_2, 102 z: 1, 103 angle: 0 104 }) 105 .onError(() => { 106 Log.debug(TAG, 'album is empty or its cover is error'); 107 this.isEmptyAlbum = true; 108 }) 109 } 110 .height(this.gridHeight) 111 } 112 113 @Builder buildOthers() { 114 Column() { 115 Text(this.item.albumName) 116 .margin({ right: $r('app.float.album_set_name_margin_right') }) 117 .textOverflow({ overflow: TextOverflow.Ellipsis }) 118 .maxLines(1) 119 .fontWeight(FontWeight.Medium) 120 .fontSize($r('sys.float.ohos_id_text_size_body2')) 121 .fontColor($r('sys.color.ohos_id_color_text_primary')) 122 123 Text(String(this.item.count)) 124 .margin({ right: this.albumCountMarginRight }) 125 .textOverflow({ overflow: TextOverflow.Ellipsis }) 126 .margin({ top: $r('app.float.photo_grid_gap') }) 127 .maxLines(1) 128 .fontColor($r('sys.color.ohos_id_color_text_secondary')) 129 .fontWeight(FontWeight.Regular) 130 .fontSize($r('sys.float.ohos_id_text_size_body3')) 131 } 132 .width('100%') 133 .alignItems(HorizontalAlign.Start) 134 .padding({ left: $r('sys.float.ohos_id_elements_margin_horizontal_m') 135 }) 136 137 138 if (this.staticIconList.has(this.item.albumName)) { 139 Image(this.staticIconList.get(this.item.albumName)) 140 .fillColor($r('sys.color.ohos_id_color_primary_dark')) 141 .height($r('app.float.album_set_new_style_icon')) 142 .aspectRatio(1) 143 .position({ x: '0%', y: '100%' }) 144 .markAnchor({ x: 0 - Constants.ALBUM_SET_NEW_ICON_MARGIN, y: this.iconMarkAnchorY }) 145 } 146 147 Column() 148 .height('100%') 149 .width('100%') 150 .border({ radius: $r('sys.float.ohos_id_corner_radius_default_m') }) 151 .backgroundColor($r('app.color.transparent')) 152 } 153 154 155 build() { 156 Stack({ alignContent: Alignment.Bottom }) { 157 if (this.isEmptyAlbum) { 158 EmptyAlbumComponent() 159 .margin({ bottom: this.gridHeight - this.gridWidth }) 160 } 161 162 this.buildNormalCard() 163 164 this.buildOthers() 165 } 166 .clip(true) 167 .width(this.gridWidth) 168 .height(this.gridHeight) 169 .border({ 170 radius: $r('sys.float.ohos_id_corner_radius_default_m') 171 }) 172 .onClick(this.handleClick.bind(this)) 173 } 174 175 private handleClick() { 176 if (this.selectParams.isFromFa && (!this.selectParams.isFromFaPhoto)) { 177 this.appBroadCast.emit(BroadCastConstants.SAVE_FORM_EDITOR_DATA, 178 [this.item.albumName, this.item.uri, this.item.albumName, '', true]); 179 } else { 180 let params: any = this.selectParams; 181 params.isFirstEnter = false; 182 params.itemDisplayName = this.item.albumName; 183 params.itemCount = this.item.count; 184 params.uri = this.item.uri; 185 params.itemCoverUri = this.item.coverUri; 186 router.pushUrl({ 187 url: 'pages/ThirdSelectPhotoGridPage', 188 params: params 189 }, router.RouterMode.Single); 190 } 191 } 192}