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 { SettingManager } from '../../../setting/SettingManager';
18import Timer from '../../../setting/settingitem/Timer';
19import AspectRatio from '../../../setting/settingitem/AspectRatio';
20import Resolution from '../../../setting/settingitem/Resolution';
21import { Dispatch, OhCombinedState } from '../../../redux/store';
22import { getStore } from '../../../redux/store';
23
24class StateStruct {
25  mode: string = '';
26}
27
28class EntryComponentDispatcher {
29  private mDispatch: Dispatch = (data) => data;
30
31  public setDispatch(dispatch: Dispatch) {
32    this.mDispatch = dispatch;
33  }
34}
35
36@Component
37export default struct EntryComponent {
38  private TAG: string = '[EntryComponent]:';
39  @State itemValue: Resource = $r('app.string.photo_ratio_4_3');
40  @State checkedValue: string = '';
41  @State settingAlias: string = '';
42  private getValue: Resource = $r('app.string.resolution_1280_720');
43  private onChange: Function = () => {};
44  private settingManager = SettingManager.getInstance();
45  @State state: StateStruct = new StateStruct();
46  private mAction: EntryComponentDispatcher = new EntryComponentDispatcher();
47
48  aboutToAppear(): void {
49    Log.info(`${this.TAG} aboutToAppear calle1d = ${this.settingAlias}`)
50    getStore().subscribe((state: OhCombinedState) => {
51      this.state = {
52        mode: state.modeReducer.mode
53      };
54    }, (dispatch: Dispatch) => {
55      this.mAction.setDispatch(dispatch);
56    });
57
58    try {
59      this.getValue = JSON.parse(JSON.stringify(this.settingManager.getSettingValue(this.settingAlias)))
60      Log.log(`${this.TAG} EntryComponent.getValue=${this.getValue}`)
61    } catch {
62      Log.log(`${this.TAG} catch this.settingAlias=${this.settingAlias}`)
63      if (this.settingAlias === Resolution.ALIAS) {
64        this.getValue = Resolution.DEFAULT_VALUE
65      } else if (this.settingAlias === Timer.ALIAS) {
66        this.getValue = Timer.DEFAULT_VALUE
67      } else {
68        this.getValue = AspectRatio.DEFAULT_VALUE
69      }
70      Log.error(`${this.TAG} catch this.getValue=${this.getValue}`)
71    }
72  }
73
74  build() {
75    Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
76      Text(this.itemValue)
77        .fontColor('#E6000000')
78        .fontSize($r('sys.float.ohos_id_text_size_body1'))
79        .fontWeight(FontWeight.Regular)
80
81      Radio({ group: 'settingChildren', value: this.itemValue.toString() })
82        .width(24)
83        .height(24)
84        .margin({ right: 14 })
85        .borderColor('#007DFF')
86        .checked(JSON.stringify(this.itemValue) === JSON.stringify(this.getValue))
87        .enabled(true)
88        .onClick(() => {
89          Log.info(`${this.TAG} onChange settingAlias:${this.settingAlias}  itemValue:${this.itemValue}`)
90          this.settingManager.setSettingValue(this.settingAlias, this.itemValue, this.state.mode)
91          this.onChange()
92        })
93    }
94    .width('100%')
95  }
96}