• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @ts-nocheck
2
3
4/*
5 * Copyright (c) 2022 Huawei Device Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19
20import mediaLibrary from '@ohos.multimedia.mediaLibrary'
21import { gridData } from '../model/GridData'
22import { myMedia } from '../model/myMedia'
23import { MediaItem } from './MediaItem'
24import { ToolKitItem } from '../common/ToolkitItem'
25
26@Component
27export struct Toolkit {
28  @State isBom: boolean = true
29  @State leftSliderIndex: number = -1
30  @Link myMedia: myMedia
31  @Link isOpacity: number
32  @Link isSurface: boolean
33  @Link btnEnabled: boolean
34  @Link streamOpacity: number
35  @Link isPlaying: boolean
36  @Link currentTime: number
37  @Link isShowMenu: boolean
38  @Link isXComponent: boolean
39  @Link isStreamShow: boolean
40  @Link streamBtnText: string | Resource
41  @State isTitle: string = ''
42  @Link itemData: mediaLibrary.FileAsset
43  @Link audioData: Array<mediaLibrary.FileAsset>
44  @Link videoData: Array<mediaLibrary.FileAsset>
45  private timer: number = undefined
46  private timerOut: number = undefined
47  private scroller: Scroller = new Scroller()
48  private mXComponentController: XComponentController
49  private deleteDialogShow: (index: number, mediaType) => void
50  private renameDialogShow: (index: number, mediaType) => void
51  private context: any
52  @State audioText: string = ''
53  @State videoText: string = ''
54
55  private GridData: gridData[] = [
56    { icon: $r('app.media.icon_color_music'), textStr: '音频' },
57    { icon: $r('app.media.icon_color_video'), textStr: '视频' },
58    { icon: $r('app.media.icon_color_search'), textStr: '搜索' },
59    { icon: $r('app.media.icon_color_favorite'), textStr: '收藏' },
60    { icon: $r('app.media.icon_color_me'), textStr: '我的' },
61    { icon: $r('app.media.icon_color_setting'), textStr: '设置' }
62  ]
63  clearVideoPlayer() {
64    this.currentTime = 0
65    clearInterval(this.timer)
66    this.myMedia.stop()
67    this.myMedia.release()
68  }
69  async aboutToAppear(){
70    this.audioText = await this.convertResourceToString($r('app.string.phoneMain_audio'))
71    this.videoText = await this.convertResourceToString($r('app.string.phoneMain_video'))
72  }
73  async convertResourceToString(resource: Resource){
74    return await this.context.resourceManager.getString(resource)
75  }
76  build() {
77    Flex({ direction: FlexDirection.Column }) {
78      if (!this.isBom) {
79        Row() {
80          Image(this.isBom ? '' : $r('app.media.icon_back'))
81            .width(24)
82            .height(24)
83            .onClick(() => {
84              this.isBom = true
85              this.isTitle = ''
86              this.isXComponent = true
87            })
88          Text(this.isTitle)
89            .margin({ left: 15 })
90            .fontSize(20)
91            .fontStyle(FontStyle.Normal)
92            .fontWeight(500)
93            .fontColor('#FFFFFFF')
94            .textAlign(TextAlign.Center)
95            .fontFamily('HarmonyHeiTi')
96            .height(28)
97        }
98        .padding(15)
99        .width('100%')
100      }
101      Column() {
102        if (this.isBom) {
103          Grid(this.scroller) {
104            ForEach(this.GridData, (item: gridData) => {
105              GridItem() {
106                ToolKitItem({ GridData: item })
107              }.onClick(() => {
108                this.isBom = !this.isBom
109                this.isTitle = item.textStr
110              })
111            }, item => item.textStr)
112          }
113          .columnsTemplate('1fr 1fr')
114          .margin({ top: 5 })
115          .width('94%')
116          .layoutWeight(1)
117          .padding(2)
118        }
119        if (this.isTitle == this.audioText) {
120          Column() {
121            List({ initialIndex: 0 }) {
122              ForEach(this.audioData, (item: mediaLibrary.FileAsset, index) => {
123                ListItem() {
124                  MediaItem({
125                    index: index,
126                    mediaItem: item,
127                    leftSliderIndex: $leftSliderIndex,
128                    deleteDialogShow: this.deleteDialogShow.bind(this),
129                    renameDialogShow: this.renameDialogShow.bind(this)
130                  })
131                }
132                .padding({ bottom: 8, left: 12 })
133                .onClick(() => {
134                  this.btnEnabled = true
135                  this.clearVideoPlayer()
136                  this.isXComponent = false
137                  this.isPlaying = true
138                  this.isOpacity = 1
139                  this.isStreamShow = false
140                  this.streamOpacity = 0.4
141                  this.streamBtnText = $r('app.string.tooKit_play')
142                  this.isShowMenu = true
143                  this.itemData = item
144                  this.myMedia.init(this.itemData)
145                  if (this.isPlaying) {
146                    this.timer = setInterval(() => {
147                      this.currentTime = this.myMedia.getCurrentTime()
148                    }, 1000)
149                  }
150                })
151              }, item => item.id)
152            }
153            .editMode(true)
154          }
155          .height('100%')
156        }
157        if (this.isTitle == this.videoText) {
158          Column() {
159            List({ initialIndex: 0 }) {
160              ForEach(this.videoData, (item: mediaLibrary.FileAsset, index) => {
161                ListItem() {
162                  MediaItem({
163                    index: index,
164                    mediaItem: item,
165                    leftSliderIndex: $leftSliderIndex,
166                    deleteDialogShow: this.deleteDialogShow.bind(this),
167                    renameDialogShow: this.renameDialogShow.bind(this)
168                  })
169                }
170                .padding({ bottom: 8, left: 12 })
171                .onClick(() => {
172                  this.btnEnabled = true
173                  this.clearVideoPlayer()
174                  this.isShowMenu = true
175                  this.isPlaying = true
176                  this.isOpacity = 1
177                  this.itemData = item
178                  this.isSurface = !this.isSurface
179                  this.isStreamShow = false
180                  this.streamBtnText = $r('app.string.tooKit_play')
181                  this.streamOpacity = 0.4
182                  this.timerOut = setTimeout(() => {
183                    let surfaceId = this.mXComponentController.getXComponentSurfaceId()
184                    this.myMedia.init(this.itemData, surfaceId)
185                  }, 100)
186
187                  if (this.isPlaying) {
188                    this.timer = setInterval(() => {
189                      this.currentTime = this.myMedia.getCurrentTime()
190                    }, 1000)
191                  }
192                })
193              }, item => item.id)
194            }
195            .editMode(true)
196          }
197          .height('100%')
198        }
199      }
200      .width('100%')
201      .height('100%')
202    }
203    .borderRadius(16)
204    .backgroundColor('rgba(255,255,255,0.06)')
205    .width('22%')
206    .height('100%')
207  }
208}