1/* 2 * Copyright (c) 2022 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 display from '@ohos.display' 17 18import { Log } from '../../../../../../common/src/main/ets/default/utils/Log' 19import getStore, { OhCombinedState } from '../../../../../../common/src/main/ets/default/redux/store' 20import { Action } from '../../../../../../common/src/main/ets/default/redux/actions/Action' 21import { EventBus } from '../../../../../../common/src/main/ets/default/worker/eventbus/EventBus' 22import EventBusManager from '../../../../../../common/src/main/ets/default/worker/eventbus/EventBusManager' 23 24let localState = (state: OhCombinedState) => { 25 return { 26 recordingTime: state.RecordReducer.recordingTime, 27 recordingTimeDisplay: state.RecordReducer.recordingTimeDisplay, 28 isRecordingPaused: state.RecordReducer.isRecordingPaused, 29 isRecordingSpotVisible: state.RecordReducer.isRecordingSpotVisible, 30 } 31} 32 33let localDispatcher = (dispatch) => { 34 return { 35 updateRecordingTime: (recordingTime: number) => { 36 dispatch(Action.updateRecordingTime(recordingTime)) 37 }, 38 updateSmallVideoTimerVisible: (visible: boolean) => { 39 dispatch(Action.updateSmallVideoTimerVisible(visible)) 40 }, 41 updateBigVideoTimerVisible: (visible: boolean) => { 42 dispatch(Action.updateBigVideoTimerVisible(visible)) 43 }, 44 updateRecordingSpotVisible: (visible: boolean) => { 45 dispatch(Action.updateRecordingSpotVisible(visible)) 46 }, 47 updateRecordingTimeDisplay: (timeDisplay: string) => { 48 dispatch(Action.updateRecordingTimeDisplay(timeDisplay)) 49 } 50 } 51} 52 53@Component 54export struct BigVideoTimer { 55 private TAG: string = '[BigVideoTimer]' 56 private timer: number = 0 57 private timerTick: number = 0 58 private appEventBus: EventBus = EventBusManager.getInstance().getEventBus() 59 @State state: any = {} 60 61 private async onRecordPausedBig(data) { 62 Log.info(`${this.TAG} onRecordPaused timer id: ${this.timer} E`) 63 clearInterval(this.timer) 64 Log.info(`${this.TAG} onRecordPaused X`) 65 } 66 67 private async onRecordResumedBig(data) { 68 Log.info(`${this.TAG} onRecordResumed E`) 69 if (this.state.recordingTime <= 2) { 70 this.setIntervalTimer() 71 } 72 Log.info(`${this.TAG} onRecordResumed X`) 73 } 74 75 aboutToAppear(): void { 76 Log.info(`${this.TAG} aboutToAppear E`) 77 getStore().connect(localState, localDispatcher)(this.state) 78 this.setIntervalTimer() 79 this.appEventBus.on(Action.ACTION_RECORD_PAUSE, this.onRecordPausedBig.bind(this)) 80 this.appEventBus.on(Action.ACTION_RECORD_RESUME, this.onRecordResumedBig.bind(this)) 81 Log.info(`${this.TAG} aboutToAppear X`) 82 } 83 84 aboutToDisappear(): void{ 85 Log.info(`${this.TAG} aboutToDisappear E`) 86 this.appEventBus.off(Action.ACTION_RECORD_PAUSE, this.onRecordPausedBig.bind(this)) 87 this.appEventBus.off(Action.ACTION_RECORD_RESUME, this.onRecordResumedBig.bind(this)) 88 Log.info(`${this.TAG} clearInterval timer id: ${this.timer}`) 89 clearInterval(this.timer) 90 Log.info(`${this.TAG} aboutToDisappear X`) 91 } 92 93 private setIntervalTimer() { 94 clearInterval(this.timer) 95 this.timer = setInterval(() => { 96 this.timerTick++ 97 if (this.timerTick % 2 === 0) { 98 this.state.updateRecordingTime(this.state.recordingTime + 1) 99 let shownSec = '00' 100 let shownMin = '00' 101 let sec = this.state.recordingTime % 60 102 if (sec < 10) { 103 shownSec = `0${sec}` 104 } else { 105 shownSec = `${sec}` 106 } 107 let minute = Math.floor(this.state.recordingTime / 60) 108 if (minute < 10) { 109 shownMin = `0${minute}` 110 } else { 111 shownMin = `${minute}` 112 } 113 this.state.updateRecordingTimeDisplay(`${shownMin}:${shownSec}`) 114 if (this.state.recordingTime > 2) { 115 clearInterval(this.timer) 116 this.state.updateSmallVideoTimerVisible(true) 117 this.state.updateBigVideoTimerVisible(false) 118 } 119 } 120 this.state.updateRecordingSpotVisible(!this.state.isRecordingSpotVisible) 121 }, 500) 122 } 123 124 build() { 125 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 126 Text('').layoutWeight(1) 127 if (this.state.isRecordingPaused) { 128 Image($r('app.media.ic_video_recording')).width(12).height(12).fillColor(Color.White) 129 } else { 130 if (this.state.isRecordingSpotVisible) { 131 Column() { 132 }.width(12).height(12).backgroundColor('#FF0000').borderRadius(6).visibility(Visibility.Visible) 133 } else { 134 Column() { 135 }.width(12).height(12).backgroundColor('#FF0000').borderRadius(6).visibility(Visibility.Hidden) 136 } 137 } 138 Text(`${this.state.recordingTimeDisplay}`).margin({ left: 8, right: 8 }).fontSize('50fp').fontWeight(300).fontColor('#FFFFFF').textAlign(TextAlign.Center) 139 Text('').width(12).height(12) 140 Text('').layoutWeight(1) 141 }.width('100%').height(96) 142 } 143}