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 mediaLibrary from '@ohos.multimedia.mediaLibrary' 16import { getDurationString } from '../model/TimeUtils' 17 18@Component 19export struct MediaItem { 20 private media: mediaLibrary.FileAsset = undefined 21 private index: number = 0 22 private btnAction: (operate: string, index: number) => void 23 24 tempNum(number) { 25 if (number < 10) { 26 return "0" + number 27 } 28 return number.toString() 29 } 30 31 getTimeString() { 32 let date = new Date() 33 date.setMilliseconds(this.media.dateAdded) 34 return `${date.getFullYear()}/${this.tempNum(date.getMonth() + 1)}/${this.tempNum(date.getDate())}` 35 } 36 37 getImgSrc() { 38 switch (this.media.mediaType) { 39 case mediaLibrary.MediaType.VIDEO: 40 return $r('app.media.video_poster') 41 case mediaLibrary.MediaType.AUDIO: 42 return $r('app.media.ic_play') 43 case mediaLibrary.MediaType.FILE: 44 return $r('app.media.ic_document') 45 break 46 default: 47 return this.media.uri 48 break 49 } 50 } 51 52 build() { 53 Row() { 54 Image(this.getImgSrc()) 55 .size({ width: 100, height: 100 }) 56 .aspectRatio(1) 57 .borderRadius(10) 58 .objectFit(ImageFit.Fill) 59 Column() { 60 Text(this.media.title) 61 .width('100%') 62 .fontSize(22) 63 Text(this.getTimeString()) 64 .width('100%') 65 .fontSize(22) 66 .margin({ top: 10 }) 67 if (this.media.mediaType === mediaLibrary.MediaType.VIDEO) { 68 Text(getDurationString(this.media.duration)) 69 .width('100%') 70 .fontSize(22) 71 .margin({ top: 10 }) 72 } 73 } 74 .margin({ left: 10 }) 75 .layoutWeight(1) 76 77 Button({ type: ButtonType.Circle, stateEffect: true }) { 78 Column() { 79 Image($r('app.media.ic_rename')) 80 .objectFit(ImageFit.Contain) 81 .size({ width: 40, height: 40 }) 82 } 83 } 84 .key('rename') 85 .size({ width: 50, height: 50 }) 86 .backgroundColor('#F5F5F5') 87 .onClick(() => { 88 this.btnAction('rename', this.index) 89 }) 90 91 Button({ type: ButtonType.Circle, stateEffect: true }) { 92 Column() { 93 Image($r('app.media.ic_delete')) 94 .objectFit(ImageFit.Contain) 95 .size({ width: 40, height: 40 }) 96 } 97 } 98 .key('delete') 99 .size({ width: 50, height: 50 }) 100 .margin({ left: 15, right: 10 }) 101 .backgroundColor('#F5F5F5') 102 .onClick(() => { 103 this.btnAction('delete', this.index) 104 }) 105 } 106 .padding({ top: 10, bottom: 10, left: 10, right: 10 }) 107 .margin({ left: 15, right: 15, top: 10 }) 108 .backgroundColor('#FFFFFF') 109 .borderRadius(20) 110 } 111}