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