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