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 router from '@ohos.router' 16import Logger from '../model/Logger' 17import TitleBar from '../view/TitleBar' 18import MediaUtils from '../model/MediaUtils' 19import { MediaView } from '../view/MediaView' 20 21export default interface Album { 22 albumName: string, 23 count: string 24} 25 26const PERMISSIONS: Array<string> = [ 27 'ohos.permission.CAMERA', 28 'ohos.permission.MICROPHONE', 29 'ohos.permission.READ_MEDIA', 30 'ohos.permission.WRITE_MEDIA', 31 'ohos.permission.MEDIA_LOCATION'] 32const TAG: string = 'Index' 33 34@Entry 35@Component 36struct Index { 37 private mediaUtils: MediaUtils = MediaUtils.getInstance(getContext(this)) 38 @State albums: Array<Album> = [] 39 @State selectIndex: number = 0 40 @State operateVisible: boolean = false 41 getAlbums = async () => { 42 this.albums = await this.mediaUtils.getAlbums() 43 } 44 45 async aboutToAppear() { 46 let context = getContext(this) as any 47 await context.requestPermissionsFromUser(PERMISSIONS) 48 Logger.info(TAG, 'grantPermission,requestPermissionsFromUser') 49 if (this.albums.length === 0) { 50 await this.getAlbums() 51 this.mediaUtils.onDateChange(this.getAlbums) 52 } 53 } 54 55 @Builder OperateBtn(src, zIndex, translate, handleClick) { 56 Button() { 57 Image(src) 58 .size({ width: '70%', height: '70%' }) 59 } 60 .type(ButtonType.Circle) 61 .size({ width: '40%', height: '40%' }) 62 .backgroundColor('#0D9FFB') 63 .zIndex(zIndex) 64 .translate({ x: translate.x, y: translate.y }) 65 .transition({ type: TransitionType.Insert, translate: { x: 0, y: 0 } }) 66 .transition({ type: TransitionType.Delete, opacity: 0 }) 67 .onClick(handleClick) 68 } 69 70 build() { 71 Stack({ alignContent: Alignment.BottomEnd }) { 72 Column() { 73 TitleBar() 74 MediaView({ albums: $albums }) 75 } 76 .width('100%') 77 .height('100%') 78 79 Stack({ alignContent: Alignment.Center }) { 80 this.OperateBtn($r('app.media.add'), 1, { x: 0, y: 0 }, () => { 81 animateTo({ duration: 500, curve: Curve.Ease }, () => { 82 this.operateVisible = !this.operateVisible 83 }) 84 }) 85 if (this.operateVisible) { 86 this.OperateBtn($r('app.media.icon_camera'), 0, { x: 0, y: '-120%' }, () => { 87 this.operateVisible = !this.operateVisible 88 router.push({ url: 'pages/CameraPage' }) 89 }) 90 this.OperateBtn($r('app.media.icon_record'), 0, { x: '-120%', y: 0 }, () => { 91 this.operateVisible = !this.operateVisible 92 router.push({ url: 'pages/RecordPage' }) 93 }) 94 this.OperateBtn($r('app.media.icon_document'), 0, { x: 0, y: '120%' }, () => { 95 this.operateVisible = !this.operateVisible 96 router.push({ url: 'pages/DocumentPage' }) 97 }) 98 } 99 } 100 .size({ width: '30%', height: '30%' }) 101 .translate({ x: 10 }) 102 .margin({ bottom: 50 }) 103 } 104 .width('100%') 105 .height('100%') 106 } 107 108 aboutToDisappear() { 109 this.mediaUtils.offDateChange() 110 } 111}