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 deviceInfo from '@ohos.deviceInfo';
17import { Action } from '@ohos/common/src/main/ets/default/redux/actions/Action';
18import { CameraSwitchButton } from '@ohos/common/src/main/ets/default/featurecommon/cameraswitcher/CameraSwitchButton';
19import { EventBus } from '@ohos/common/src/main/ets/default/worker/eventbus/EventBus';
20import { EventBusManager } from '@ohos/common/src/main/ets/default/worker/eventbus/EventBusManager';
21import { Dispatch, OhCombinedState } from '@ohos/common/src/main/ets/default/redux/store';
22import { getStore } from '@ohos/common/src/main/ets/default/redux/store';
23import { Log } from '@ohos/common/src/main/ets/default/utils/Log';
24import { ShutterButton } from '@ohos/common/src/main/ets/default/featurecommon/shutterbutton/ShutterButton';
25import { ThumbnailView } from '@ohos/common/src/main/ets/default/featurecommon/thumbnail/ThumbnailView';
26import { CameraPlatformCapability } from '@ohos/common/src/main/ets/default/camera/CameraPlatformCapability';
27
28class StateStruct {
29  public platformCapability: CameraPlatformCapability | undefined = undefined;
30  public isThirdPartyCall: boolean = false;
31  public videoState: string = '';
32  public showZoomLabelValue: boolean = true;
33  public mode: string = '';
34}
35
36class FootBarDispatcher {
37  private mDispatch: Dispatch = (data) => data;
38
39  public setDispatch(dispatch: Dispatch) {
40    this.mDispatch = dispatch;
41  }
42}
43
44@Component
45export struct FootBar {
46  private TAG: string = '[FootBar]:';
47  @State state: StateStruct = new StateStruct();
48  @State isRecording: boolean = false;
49  private appEventBus: EventBus = EventBusManager.getInstance().getEventBus();
50  private mAction: FootBarDispatcher = new FootBarDispatcher();
51
52  private async onRecordStart(): Promise<void> {
53    this.isRecording = true
54    Log.info(`${this.TAG} onRecordStart`)
55  }
56
57  private async onRecordStop(): Promise<void> {
58    this.isRecording = false
59    Log.info(`${this.TAG} onRecordStop`)
60  }
61
62  aboutToAppear(): void {
63    Log.info(`${this.TAG} aboutToAppear E`)
64    getStore().subscribe((state: OhCombinedState) => {
65      this.state = {
66        platformCapability: state.cameraInitReducer.platformCapability,
67        isThirdPartyCall: state.contextReducer.isThirdPartyCall,
68        videoState: state.recordReducer.videoState,
69        showZoomLabelValue: state.zoomReducer.showZoomLabelValue,
70        mode: state.modeReducer.mode,
71      };
72    }, (dispatch: Dispatch) => {
73      this.mAction.setDispatch(dispatch);
74    });
75    this.isRecording = false
76    this.appEventBus.on(Action.ACTION_RECORD_START, () => this.onRecordStart())
77    this.appEventBus.on(Action.ACTION_RECORD_STOP, () => this.onRecordStop())
78    Log.info(`${this.TAG} aboutToAppear X`)
79  }
80
81  aboutToDisappear(): void {
82    Log.info(`${this.TAG} aboutToDisappear E`)
83    this.appEventBus.off(Action.ACTION_RECORD_START, () => this.onRecordStart())
84    this.appEventBus.off(Action.ACTION_RECORD_STOP, () => this.onRecordStop())
85    Log.info(`${this.TAG} aboutToDisappear X`)
86  }
87
88  private isThumbnailViewVisibility(): boolean {
89    return!this.isRecording && !this.state.isThirdPartyCall &&
90    this.state.videoState !== "startTakeVideo" && this.state.videoState !== "pauseTakeVideo"
91  }
92
93  private isPhoneSwitchShow(): boolean {
94    if (this.state.platformCapability) {
95      return deviceInfo.deviceType !== 'default' && this.state.platformCapability?.mCameraCount > 1 &&
96      !this.isRecording
97    } else {
98      return false;
99    }
100  }
101
102  build() {
103    Flex({
104      direction: FlexDirection.Row,
105      alignItems: ItemAlign.Center,
106      justifyContent: FlexAlign.Center
107    }) {
108      Column() {
109        ThumbnailView()
110      }.width(44).aspectRatio(1).visibility(this.isThumbnailViewVisibility() ? Visibility.Visible : Visibility.Hidden)
111
112      ShutterButton()
113      if (this.isPhoneSwitchShow()) {
114        CameraSwitchButton()
115      } else {
116        Column() {
117        }
118        .width('44')
119        .aspectRatio(1)
120      }
121    }
122    .height(96)
123    .opacity(100)
124    .width('100%')
125    .padding({ top: 10, bottom: 10 })
126    .visibility(this.state.showZoomLabelValue ? Visibility.Visible : Visibility.Hidden)
127  }
128}