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 { MediaItem } from '../common/MediaItem' 17import { myMedia } from '../model/myMedia' 18import { gridData } from '../model/GridData' 19import mediaLibrary from '@ohos.multimedia.mediaLibrary' 20import { GridDataItem } from '../phoneView/GridDataItem' 21 22@Component 23export struct Toolkit { 24 @State isBom: boolean = true 25 @State isTitle: string = '' 26 @State leftSliderIndex: number = -1 27 @State clickIndex: number = -1 28 @State itemData: mediaLibrary.FileAsset = undefined 29 @Link myMedia: myMedia 30 @Link currentTime: number 31 @Link isXComponent: boolean 32 @Link audioData: Array<mediaLibrary.FileAsset> 33 @Link videoData: Array<mediaLibrary.FileAsset> 34 @Link isPlaying: boolean 35 @Link surfaceId: number 36 private timer: number = undefined 37 private scroller: Scroller = new Scroller() 38 private deleteDialogShow: (index: number, mediaType) => void 39 private renameDialogShow: (index: number, mediaType) => void 40 private GridData: gridData[] = [ 41 { icon: $r('app.media.icon_color_music'), textStr: '音频' }, 42 { icon: $r('app.media.icon_color_video'), textStr: '视频' }, 43 { icon: $r('app.media.icon_color_favorite'), textStr: '收藏' }, 44 { icon: $r('app.media.icon_color_me'), textStr: '我的' }, 45 ] 46 private context: any 47 @State audioText: string = '' 48 @State videoText: string = '' 49 async aboutToAppear(){ 50 this.audioText = await this.convertResourceToString($r('app.string.phoneMain_audio')) 51 this.videoText = await this.convertResourceToString($r('app.string.phoneMain_video')) 52 } 53 async convertResourceToString(resource: Resource){ 54 return await this.context.resourceManager.getString(resource) 55 } 56 build() { 57 58 Flex({ direction: FlexDirection.Column }) { 59 Flex({ justifyContent: FlexAlign.SpaceAround }) { 60 ForEach(this.GridData, (item: gridData, index) => { 61 GridDataItem({ 62 item: item, 63 index: index, 64 clickIndex: $clickIndex, 65 isBom: $isBom, 66 isTitle: $isTitle, 67 context: this.context, 68 isXComponent: $isXComponent 69 }) 70 }, item => item.textStr) 71 }.padding({ top: 10, bottom: 16 }) 72 73 if (this.isTitle == this.audioText || this.isTitle == this.videoText) { 74 Scroll(this.scroller) { 75 Column() { 76 List({ initialIndex: 0 }) { 77 ForEach(this.isTitle == this.audioText ? this.audioData : this.videoData, (item: mediaLibrary.FileAsset, index) => { 78 ListItem() { 79 MediaItem({ 80 index: index, 81 mediaItem: item, 82 leftSliderIndex: $leftSliderIndex, 83 deleteDialogShow: this.deleteDialogShow.bind(this), 84 renameDialogShow: this.renameDialogShow.bind(this) 85 }) 86 } 87 .width('96%') 88 .padding({ bottom: 12, left: 4 }) 89 .onClick(() => { 90 this.isPlaying = true 91 this.itemData = item 92 if (this.isTitle == this.audioText) { 93 this.isXComponent = false 94 this.myMedia.init(this.itemData) 95 } else { 96 this.myMedia.init(this.itemData, this.surfaceId) 97 } 98 if (this.isPlaying) { 99 this.timer = setInterval(() => { 100 this.currentTime = this.myMedia.getCurrentTime() 101 }, 1000) 102 } 103 }) 104 }, item => item.id) 105 } 106 .alignListItem(ListItemAlign.Center) 107 .editMode(true) 108 }.height('57%') 109 } 110 } 111 }.height('100%') 112 } 113}