• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { getStore } from '../../redux/store'
19import { Log } from '../../utils/Log'
20import MultiCameraDialog from '../customdialog/MultiCameraDialog'
21import deviceInfo from '@ohos.deviceInfo'
22
23let localState = (state) => {
24  return {
25    mode: state.ModeReducer.mode,
26    uiEnable: state.ContextReducer.uiEnable,
27    cameraPosition: state.CameraReducer.cameraPosition,
28    videoState: state.RecordReducer.videoState,
29  }
30}
31
32let localDispatcher = (dispatch) => {
33  return {
34    changeCameraPosition: (cameraPosition: string) => {
35      dispatch(Action.uiState(false))
36      dispatch(Action.switchCamera(cameraPosition))
37      dispatch(Action.resetZoomRatio(1))
38    },
39  }
40}
41
42let storageCameraId = AppStorage.Link('storageCameraId')
43
44class StateStruct {
45  mode
46  uiEnable
47  cameraPosition
48  videoState
49  changeCameraPosition : Function
50}
51
52@Component
53export struct CameraSwitchButton {
54  private TAG: string = '[CameraSwitchButton]:'
55  @State state: StateStruct = new StateStruct()
56  @State deviceType: string = deviceInfo.deviceType
57  @StorageLink('storageCameraId') storageCameraId: string = ''
58
59  icon: Resource = $r('app.media.small_switch_camera')
60  mWidth: number
61  mHeight: number
62  mMargin: number
63  type: ButtonType
64  stateEffect: boolean
65  cameraSwitchController: CameraSwitchController = new CameraSwitchController()
66  multiDialogController: CustomDialogController = new CustomDialogController({
67    builder: MultiCameraDialog({
68      cancel: this.existView.bind(this),
69      deviceType: $deviceType
70    }),
71    autoCancel: true,
72    alignment:DialogAlignment.Center,
73    customStyle: true,
74    cancel: this.existView
75  })
76
77  aboutToAppear() {
78    Log.info(`${this.TAG} aboutToAppear E`)
79    getStore().connect(localState, localDispatcher)(this.state)
80    this.cameraSwitchController.getParam()
81    this.icon = this.cameraSwitchController.icon
82    this.mWidth = this.cameraSwitchController.width
83    this.mHeight = this.cameraSwitchController.height
84    this.mMargin = this.cameraSwitchController.margin
85    this.type = this.cameraSwitchController.type
86    this.stateEffect = this.cameraSwitchController.stateEffect
87    Log.info(`${this.TAG} aboutToAppear X`)
88  }
89
90  private openMultiDialog() {
91    Log.info(`${this.TAG} openMultiDialog E`)
92    this.multiDialogController.open()
93    Log.info(`${this.TAG} openMultiDialog X`)
94  }
95
96  private existView() {}
97
98  build() {
99    Column() {
100      Stack() {
101        Image($r('app.media.small_switch_camera'))
102          .width('67.5%').aspectRatio(1)
103          .clip(new Circle({ width: '100%', height: '100%' }))
104        Column() {}.width(44).height(44)
105          .border({ width: 1, color: Color.White, radius: 22, style: BorderStyle.Solid })
106      }
107      .width('100%').height('100%').enabled(this.state.uiEnable)
108      .onClick(() => {
109        Log.info(`${this.TAG} onClick invoke E`)
110        Log.info(`${this.TAG} this.state.videoState: ${this.state.videoState}, this.state.mode: ${this.state.mode}`)
111        Log.info(`${this.TAG} this.state.cameraPosition: ${this.state.cameraPosition}`)
112        if (this.state.videoState === 'beforeTakeVideo') {
113          if (this.state.mode === 'MULTI') {
114            this.openMultiDialog()
115          } else {
116            globalThis.switchCameraTime = new Date().getTime()
117            if (this.state.cameraPosition !== 'BACK') {
118              this.state.changeCameraPosition('BACK')
119              this.storageCameraId = 'BACK'
120            } else {
121              this.state.changeCameraPosition('FRONT')
122              this.storageCameraId = 'FRONT'
123            }
124          }
125        }
126        Log.info(`${this.TAG} onClick invoke X`)
127      })
128    }.width(44).aspectRatio(1)
129  }
130}