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