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