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 { Log } from '../../utils/Log' 17import { getStore } from '../../redux/store' 18import window from '@ohos.window'; 19import { Action } from '../../redux/actions/Action' 20import { SettingManager } from '../../setting/SettingManager' 21import Timer from '../../setting/settingitem/Timer' 22import { EventBus } from '../../worker/eventbus/EventBus' 23import { EventBusManager } from '../../worker/eventbus/EventBusManager' 24 25let localState = (state) => { 26 return { 27 mode: state.ModeReducer.mode, 28 } 29} 30 31let localDispatcher = (dispatch) => { 32 return { 33 changeTimeLapse: (isShowtimeLapse: boolean) => { 34 dispatch(Action.changeTimeLapse(isShowtimeLapse)) 35 }, 36 capture: () => { 37 dispatch(Action.updateShowFlashBlackFlag(true)) 38 dispatch(Action.capture()) 39 }, 40 startRecording: () => { 41 dispatch(Action.startRecording()) 42 dispatch(Action.updateVideoState('startTakeVideo')) 43 dispatch(Action.updateBigVideoTimerVisible(true)) 44 dispatch(Action.updateScreenStatus(true)) 45 } 46 } 47} 48 49class StateStruct { 50 mode 51 changeTimeLapse: Function 52 capture: Function 53 startRecording: Function 54} 55 56@Component 57export struct TimeLapseView { 58 private TAG: string = '[TimeLapseView]:' 59 private timer: number = 0 60 private settingManager = SettingManager.getInstance() 61 appEventBus: EventBus = EventBusManager.getInstance().getEventBus() 62 63 @State state: StateStruct = new StateStruct() 64 @State timerLapse: number = 0 65 66 private onKeepScreen(data) { 67 Log.info(`${this.TAG} onKeepScreen E`) 68 if (data) { 69 globalThis.cameraWinClass.setKeepScreenOn(data.isKeepScreenOn).then((v) => { 70 Log.info('Succeeded in setting the screen to be always on. Data: ' + JSON.stringify(v)) 71 }).catch((err) => { 72 Log.error('Failed to set the screen to be always on. Cause: ' + JSON.stringify(err)); 73 }); 74 } 75 Log.info(`${this.TAG} onKeepScreen X`) 76 } 77 78 aboutToAppear() { 79 Log.debug(`${this.TAG} aboutToAppear E`) 80 getStore().connect(localState, localDispatcher)(this.state) 81 this.appEventBus.on(Action.ACTION_KEEP_SCREEN_ON, this.onKeepScreen.bind(this)) 82 switch(JSON.stringify(this.settingManager.getTimeLapse())){ 83 case JSON.stringify(Timer.RESOURCE_OFF): 84 this.timerLapse = -1 85 break; 86 case JSON.stringify(Timer.RESOURCE_TWO_SECONDS): 87 this.timerLapse = 2 88 break; 89 case JSON.stringify(Timer.RESOURCE_FIVE_SECONDS): 90 this.timerLapse = 5 91 break; 92 case JSON.stringify(Timer.RESOURCE_TEN_SECONDS): 93 this.timerLapse = 10 94 break; 95 default: 96 this.timerLapse = 10 97 break; 98 } 99 Log.info(`${this.TAG} calculate timerLapse= ${this.timerLapse}`) 100 if (this.timerLapse > 0) { 101 clearInterval(this.timer) 102 this.timer = setInterval(() => { 103 this.timerLapse -- 104 if(this.timerLapse < 1){ 105 clearInterval(this.timer) 106 this.state.changeTimeLapse(false) 107 Log.info(`${this.TAG} calculate mode= ${this.state.mode}`) 108 if (!this.state.mode || this.state.mode === 'PHOTO' || this.state.mode === 'MULTI') { 109 this.state.capture() 110 } else if (this.state.mode === 'VIDEO') { 111 this.state.startRecording() 112 } 113 } 114 }, 1000) 115 } 116 Log.debug(`${this.TAG} aboutToAppear X`) 117 } 118 119 120 aboutToDisappear(): void { 121 Log.info(`${this.TAG} aboutToDisappear E`) 122 clearInterval(this.timer) 123 Log.info(`${this.TAG} aboutToDisappear X`) 124 } 125 126 127 build() { 128 Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.End}){ 129 Text(this.timerLapse.toString()) 130 .fontSize(120) 131 .fontColor(Color.White) 132 .fontWeight(300) 133 .textAlign(TextAlign.Center) 134 }.width('100%').height('100%') 135 } 136}