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