1/* 2 * Copyright (c) 2023 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 { Action } from '../../redux/actions/Action'; 17import { CameraSwitchController } from './CameraSwitchController'; 18import { Dispatch, getStore, OhCombinedState } from '../../redux/store'; 19import { Log } from '../../utils/Log'; 20import MultiCameraDialog from '../customdialog/MultiCameraDialog'; 21import deviceInfo from '@ohos.deviceInfo'; 22import { GlobalContext } from '../../utils/GlobalContext'; 23import { CameraId } from '../../setting/settingitem/CameraId'; 24 25let storageCameraId: string = AppStorage.Link('storageCameraId') as string; 26 27class StateStruct { 28 mode: string = 'PHOTO'; 29 uiEnable: boolean = true; 30 cameraPosition: CameraId = CameraId.BACK; 31 videoState: string = 'beforeTakeVideo'; 32} 33 34 35class CameraSwitchDispatcher { 36 public setDispatch(dispatch: Dispatch) { 37 this.mDispatch = dispatch; 38 } 39 40 public changeCameraPosition(cameraPosition: string): void { 41 this.mDispatch(Action.uiState(false)); 42 this.mDispatch(Action.switchCamera(cameraPosition)); 43 this.mDispatch(Action.resetZoomRatio(1)); 44 } 45 46 private mDispatch: Dispatch = (data) => data; 47} 48 49 50@Component 51export struct CameraSwitchButton { 52 @State state: StateStruct = new StateStruct() 53 @State deviceType: string = deviceInfo.deviceType 54 @StorageLink('storageCameraId') storageCameraId: string = '' 55 icon: Resource = $r('app.media.small_switch_camera') 56 mWidth: number = 0; 57 mHeight: number = 0; 58 mMargin: number = 0; 59 type: ButtonType = ButtonType.Capsule; 60 stateEffect: boolean = false; 61 cameraSwitchController: CameraSwitchController = new CameraSwitchController() 62 multiDialogController: CustomDialogController = new CustomDialogController({ 63 builder: MultiCameraDialog({ 64 cancel: () => this.existView(), 65 deviceType: $deviceType 66 }), 67 autoCancel: true, 68 alignment: DialogAlignment.Center, 69 customStyle: true, 70 cancel: this.existView 71 }) 72 private TAG: string = '[CameraSwitchButton]:' 73 private mAction: CameraSwitchDispatcher = new CameraSwitchDispatcher(); 74 75 aboutToAppear() { 76 Log.info(`${this.TAG} aboutToAppear E`); 77 getStore().subscribe((state: OhCombinedState) => { 78 this.state = { 79 mode: state.modeReducer.mode, 80 uiEnable: state.contextReducer.uiEnable, 81 cameraPosition: state.cameraReducer.cameraPosition, 82 videoState: state.recordReducer.videoState 83 }; 84 }, (dispatch: Dispatch) => { 85 this.mAction.setDispatch(dispatch); 86 }); 87 this.cameraSwitchController.getParam() 88 this.icon = this.cameraSwitchController.icon 89 this.mWidth = this.cameraSwitchController.width 90 this.mHeight = this.cameraSwitchController.height 91 this.mMargin = this.cameraSwitchController.margin 92 this.type = this.cameraSwitchController.type 93 this.stateEffect = this.cameraSwitchController.stateEffect 94 Log.info(`${this.TAG} aboutToAppear X`) 95 } 96 97 build() { 98 Column() { 99 Stack() { 100 Image($r('app.media.small_switch_camera')) 101 .width('67.5%').aspectRatio(1) 102 .clip(new Circle({ width: '100%', height: '100%' })) 103 Column() { 104 }.width(44).height(44) 105 .border({ 106 width: 1, 107 color: Color.White, 108 radius: 22, 109 style: BorderStyle.Solid 110 }) 111 } 112 .width('100%').height('100%').enabled(this.state.uiEnable) 113 .onClick(() => { 114 Log.info(`${this.TAG} onClick invoke E`) 115 Log.info(`${this.TAG} this.state.videoState: ${this.state.videoState}, this.state.mode: ${this.state.mode}`) 116 Log.info(`${this.TAG} this.state.cameraPosition: ${this.state.cameraPosition}`) 117 if (this.state.videoState === 'beforeTakeVideo') { 118 if (this.state.mode === 'MULTI') { 119 this.openMultiDialog() 120 } else { 121 GlobalContext.get().setObject('switchCameraTime', new Date().getTime()) 122 if (this.state.cameraPosition !== 'BACK') { 123 this.mAction.changeCameraPosition('BACK') 124 this.storageCameraId = 'BACK' 125 } else { 126 this.mAction.changeCameraPosition('FRONT') 127 this.storageCameraId = 'FRONT' 128 } 129 } 130 } 131 Log.info(`${this.TAG} onClick invoke X`) 132 }) 133 }.width(44).aspectRatio(1) 134 } 135 136 private openMultiDialog() { 137 Log.info(`${this.TAG} openMultiDialog E`) 138 this.multiDialogController.open() 139 Log.info(`${this.TAG} openMultiDialog X`) 140 } 141 142 private existView(): void { 143 } 144}