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 { AlbumDefine, AlbumInfo, BroadCast, Constants, Log, ScreenManager, SelectManager, UiUtil } from '@ohos/common'; 17import { EmptyAlbumComponent } from './EmptyAlbumComponent'; 18import router from '@ohos.router'; 19 20const TAG: string = 'browser_AlbumSelectGridItemNewStyle'; 21 22// The item of album grid, and it's new style. 23@Component 24export struct AlbumSelectGridItemNewStyle { 25 @State @Watch('updateCard') item: AlbumInfo = new AlbumInfo(); 26 @State isEmptyAlbum: boolean = false; 27 @Provide gridHeight: number = 0; 28 @Provide gridWidth: number = 0; 29 @Consume broadCast: BroadCast; 30 mSelectManager: SelectManager | null = null; 31 gridAspectRatio = Constants.CARD_ASPECT_RATIO; 32 albumCountMarginRight = Constants.ALBUM_SET_NEW_ICON_SIZE + Constants.ALBUM_SET_NEW_ICON_MARGIN * 2; 33 iconMarkAnchorX = Constants.ALBUM_SET_NEW_ICON_SIZE + Constants.ALBUM_SET_NEW_ICON_MARGIN; 34 iconMarkAnchorY = Constants.ALBUM_SET_NEW_ICON_SIZE + Constants.ALBUM_SET_NEW_ICON_MARGIN; 35 @State transformV: number = 0; 36 private fp2vpUnit: number = px2vp(fp2px(Constants.NUMBER_1)); 37 private staticIconList = new Map([ 38 [AlbumDefine.ALBUM_ID_VIDEO, $r('app.media.ic_video')], 39 [AlbumDefine.ALBUM_ID_FAVOR, $r('app.media.ic_favorite_overlay')] 40 ]); 41 private updateCardSizeFunc: Function = (): void => this.updateCardSize() 42 43 aboutToAppear(): void { 44 Log.info(TAG, `aboutToAppear + ${this.item.coverUri}`); 45 this.updateCardSize(); 46 // 后续phone缩略图支持横竖屏后再放开 47 if (AppStorage.get('deviceType') as string !== Constants.DEFAULT_DEVICE_TYPE) { 48 ScreenManager.getInstance().on(ScreenManager.ON_WIN_SIZE_CHANGED, this.updateCardSizeFunc); 49 } 50 } 51 52 aboutToDisappear(): void { 53 // 后续phone缩略图支持横竖屏后再放开 54 if (AppStorage.get('deviceType') as string !== Constants.DEFAULT_DEVICE_TYPE) { 55 ScreenManager.getInstance().off(ScreenManager.ON_WIN_SIZE_CHANGED, this.updateCardSizeFunc); 56 } 57 } 58 59 updateCard() { 60 Log.debug(TAG, 'Album changed and update card size.'); 61 this.updateCardSize(); 62 } 63 64 build() { 65 Column() { 66 Stack({ alignContent: Alignment.Bottom }) { 67 if (this.isEmptyAlbum) { 68 EmptyAlbumComponent() 69 .margin({ bottom: this.gridHeight - this.gridWidth }) 70 } 71 72 Image(this.item.coverUri) 73 .width(this.gridWidth) 74 .height(this.gridWidth) 75 .objectFit(ImageFit.Cover) 76 .translate({ x: this.transformV }) 77 .border({ radius: $r('sys.float.ohos_id_corner_radius_default_m') }) 78 .margin({ bottom: this.gridHeight - this.gridWidth }) 79 .onError(() => { 80 Log.debug(TAG, 'album is empty or its cover is error'); 81 this.isEmptyAlbum = true; 82 }) 83 84 Column() { 85 Text(this.item.albumName) 86 .margin({ right: $r('app.float.album_set_name_margin_right') }) 87 .textOverflow({ overflow: TextOverflow.Ellipsis }) 88 .maxLines(1) 89 .fontWeight(FontWeight.Medium) 90 .fontSize($r('sys.float.ohos_id_text_size_body2')) 91 .fontColor($r('sys.color.ohos_id_color_text_primary')) 92 93 Text(String(this.item.count)) 94 .margin({ right: this.albumCountMarginRight, top: $r('app.float.photo_grid_gap') }) 95 .textOverflow({ overflow: TextOverflow.Ellipsis }) 96 .maxLines(1) 97 .fontColor($r('sys.color.ohos_id_color_text_secondary')) 98 .fontWeight(FontWeight.Regular) 99 .fontSize($r('sys.float.ohos_id_text_size_body3')) 100 } 101 .width('100%') 102 .alignItems(HorizontalAlign.Start) 103 .padding({ 104 left: $r('sys.float.ohos_id_elements_margin_horizontal_m') 105 }) 106 107 if (this.staticIconList.has(this.item.albumName)) { 108 Image(this.staticIconList.get(this.item.albumName)) 109 .height($r('app.float.album_set_new_style_icon')) 110 .aspectRatio(1) 111 .position({ x: '0%', y: '100%' }) 112 .markAnchor({ x: 0 - Constants.ALBUM_SET_NEW_ICON_MARGIN, y: this.iconMarkAnchorY }) 113 } 114 } 115 .height(this.gridHeight) 116 } 117 .width(this.gridWidth) 118 .height(this.gridHeight) 119 .clip(true) 120 .border({ 121 radius: $r('sys.float.ohos_id_corner_radius_default_m') 122 }) 123 .onClick(() => { 124 Log.info(TAG, `After onClick, MediaSet is: ${JSON.stringify(this.item)}`); 125 router.pushUrl({ 126 url: 'pages/NewAlbumPage', 127 params: { 128 item: JSON.stringify(this.item) 129 } 130 }) 131 }) 132 } 133 134 private updateCardSize() { 135 let contentWidth = ScreenManager.getInstance().getWinWidth(); 136 let count = UiUtil.getAlbumGridCount(false); 137 this.gridWidth = ((contentWidth - Constants.ALBUM_SET_MARGIN * 2 - (Constants.ALBUM_SET_GUTTER * (count - 1))) / count); 138 let numberHeight = Constants.TEXT_SIZE_BODY2 * this.fp2vpUnit; 139 let nameHeight = Constants.TEXT_SIZE_SUB_TITLE1 * this.fp2vpUnit; 140 141 UiUtil.getResourceNumber($r('sys.float.ohos_id_elements_margin_vertical_m')).then((value: number) => { 142 let bottomHeight = px2vp(value) + nameHeight + Constants.NUMBER_2 + numberHeight; 143 this.iconMarkAnchorY = Constants.ALBUM_SET_NEW_ICON_SIZE + Constants.ALBUM_SET_NEW_ICON_MARGIN + bottomHeight; 144 145 this.gridHeight = this.gridWidth + bottomHeight; 146 Log.info(TAG, 'updateCardSize gridWidth : ' + this.gridWidth + ', gridHeight : ' + this.gridHeight); 147 this.transformV = 0; 148 }); 149 150 } 151}