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