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