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