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 45 aboutToAppear(): void { 46 Log.info(TAG, 'aboutToAppear new opp'); 47 48 screenManager.on(screenManager.ON_WIN_SIZE_CHANGED, () => { 49 this.updateCardSize(); 50 }) 51 this.updateCardSize(); 52 this.item.load().then(() => { 53 this.thumbnail = this.item.getThumbnail() 54 }) 55 } 56 57 updateCard() { 58 Log.debug(TAG, 'Album changed and update card size.'); 59 this.updateCardSize(); 60 } 61 62 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 72 if (this.isBigCard) { 73 this.cardWidth = this.bigWidth; 74 } else { 75 this.cardWidth = gridWidth; 76 } 77 this.cardHeight = this.gridHeight; 78 this.transformV = 0; 79 } 80 81 private async jumpToBrowser() { 82 Log.info(TAG, `After onClick : ${JSON.stringify(this.item)}`); 83 let name = this.deviceName ? `${this.deviceName}(${this.item.displayName})` : this.item.displayName; 84 let relativePath = await this.item.getRelativePath() 85 let simpleItem: SimpleAlbumDataItem = new SimpleAlbumDataItem(this.item.id, this.item.displayName, relativePath, this.deviceId, this.deviceName) 86 router.push({ 87 uri: 'feature/photoGrid/view/PhotoGridPage', 88 params: { 89 item: JSON.stringify(simpleItem), 90 isDistributedAlbum: true, 91 distributedAlbumName: name 92 } 93 }); 94 } 95 96 build() { 97 Column() { 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: 0 115 }) 116 .onError(() => { 117 Log.debug(TAG, 'album is empty or its cover is error'); 118 this.isEmptyAlbum = true; 119 }) 120 .onComplete((msg: { 121 width: number, 122 height: number, 123 componentWidth: number, 124 componentHeight: number 125 }) => { 126 Log.debug(TAG, `image load success.Size: ${msg.componentWidth}x${msg.componentHeight}`); 127 }) 128 } 129 .width(this.bigWidth) 130 .height(this.gridHeight) 131 .justifyContent(FlexAlign.Center) 132 .alignItems(VerticalAlign.Center) 133 } else { 134 Row() { 135 Image(this.thumbnail) 136 .width(this.cardWidth) 137 .height(this.cardHeight) 138 .translate({ x: this.transformV }) 139 .border({ radius: $r('sys.float.ohos_id_corner_radius_default_l') }) 140 .rotate({ 141 centerX: this.cardWidth / 2, 142 centerY: this.cardHeight / 2, 143 z: 1, 144 angle: 0 145 }) 146 .onError(() => { 147 Log.debug(TAG, 'album is empty or its cover is error'); 148 this.isEmptyAlbum = true; 149 }) 150 .onComplete((msg: { 151 width: number, 152 height: number, 153 componentWidth: number, 154 componentHeight: number 155 }) => { 156 Log.debug(TAG, `image load success. Size: ${msg.width}x${msg.height},\ 157 componentSize: ${msg.componentWidth}x${msg.componentHeight}`); 158 }) 159 } 160 .height(this.gridHeight) 161 .justifyContent(FlexAlign.Center) 162 .alignItems(VerticalAlign.Center) 163 } 164 165 Column() { 166 Text(this.item.displayName) 167 .margin({ right: $r('app.float.album_set_name_margin_right') }) 168 .textOverflow({ overflow: TextOverflow.Ellipsis }) 169 .maxLines(1) 170 .fontWeight(FontWeight.Medium) 171 .fontSize($r('sys.float.ohos_id_text_size_sub_title1')) 172 .fontColor($r('app.color.white')) 173 174 Text(String(this.item.count)) 175 .margin({ right: this.albumCountMarginRight }) 176 .textOverflow({ overflow: TextOverflow.Ellipsis }) 177 .maxLines(1) 178 .fontColor($r('app.color.white')) 179 .fontWeight(FontWeight.Regular) 180 .fontSize($r('sys.float.ohos_id_text_size_body2')) 181 } 182 .width('100%') 183 .alignItems(HorizontalAlign.Start) 184 .margin({ 185 left: $r('app.float.album_set_count_margin_left'), 186 bottom: $r('app.float.album_set_count_margin_bottom') 187 }) 188 } 189 .border({ 190 radius: $r('sys.float.ohos_id_corner_radius_default_l'), 191 width: $r('app.float.album_cover_stroke_width'), 192 color: $r('app.color.album_cover_stroke_color') 193 }) 194 .onClick(() => { 195 this.jumpToBrowser() 196 }) 197 } 198 } 199}