• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { CameraService } from '../../camera/CameraService'
17import { CameraId } from '../../setting/settingitem/CameraId'
18import { Log } from '../../utils/Log'
19import EntryComponentForMulti from './EntryComponentForMulti'
20import { OhCombinedState } from '../../redux/store'
21import { getStore } from '../../redux/store'
22import { Action } from '../../redux/actions/Action'
23import { Dispatch } from '../../redux/core/redux/types/store'
24
25let storageCameraId: string = AppStorage.Link('storageCameraId') as string;
26
27class StateStruct {
28}
29
30class MultiCameraDispatcher {
31  private mDispatch: Dispatch = (data) => data;
32
33  public setDispatch(dispatch: Dispatch) {
34    this.mDispatch = dispatch;
35  }
36
37  public changeCameraPosition(cameraPosition: string): void {
38    this.mDispatch(Action.uiState(false));
39    this.mDispatch(Action.switchCamera(cameraPosition));
40    this.mDispatch(Action.resetZoomRatio(1));
41  }
42}
43
44
45@CustomDialog
46export default struct MultiCameraDialog {
47  private TAG: string = '[MultiCameraDialog]:';
48  private cameraService = CameraService.getInstance();
49  controller?: CustomDialogController;
50  cancel: () => void = () => {};
51  confirm: () => void = () => {};
52  private localList: string[] = [
53  CameraId.FRONT,
54  CameraId.BACK
55  ];
56  @State private moreList: string[] = [];
57  @State state: StateStruct = new StateStruct();
58  @State isShowMore: boolean = false;
59  @State gridColumns: number = 12;
60  @State useSizeTypeOffset: number = 4;
61  @Link deviceType: string;
62  @StorageLink('storageCameraId') storageCameraId: string = '';
63  private mAction: MultiCameraDispatcher = new MultiCameraDispatcher();
64
65  aboutToAppear() {
66    Log.info(`${this.TAG} aboutToAppear.`)
67    getStore().subscribe((state: OhCombinedState) => {
68      this.state = {
69      };
70    }, (dispatch: Dispatch) => {
71      this.mAction.setDispatch(dispatch);
72    });
73
74    let localCameraInfo = this.cameraService.getLocalCameraMap()
75    if (!localCameraInfo.get('front')) {
76      this.localList.shift()
77    } else if (!localCameraInfo.get('back')) {
78      this.localList.pop()
79    }
80    if (this.deviceType === 'phone' || this .deviceType === 'default') {
81      this.gridColumns = 4
82      this.useSizeTypeOffset = 0
83    } else {
84      this.gridColumns = 12
85      this.useSizeTypeOffset = 4
86    }
87  }
88
89  private onChange(item: string): void {
90    Log.info(`${this.TAG} MultiCameraPosition ${JSON.stringify(item)}`)
91    if (item.includes('BACK')) {
92      this.storageCameraId = 'BACK'
93    } else if(item.includes('FRONT')) {
94      this.storageCameraId = 'FRONT'
95    }
96    this.mAction.changeCameraPosition(item);
97    this.cancel();
98    if (this.controller) {
99      this.controller.close();
100    }
101  }
102
103  build() {
104    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
105      GridContainer({ columns: this.gridColumns, gutter: 12, margin: 12 }) {
106        Column() {
107          Row() {
108            Text($r('app.string.select_camera'))
109              .fontSize($r("sys.float.ohos_id_text_size_dialog_tittle"))
110              .fontColor('#E6000000')
111              .opacity(0.9)
112              .fontWeight(FontWeight.Medium)
113              .opacity($r("sys.float.ohos_id_alpha_content_primary"))
114          }
115          .width('100%')
116          .height('56vp')
117
118          Row() {
119            Text($r('app.string.local_device'))
120              .fontSize($r("sys.float.ohos_id_text_size_body2"))
121              .fontColor("#99182431")
122              .fontWeight(FontWeight.Medium)
123          }
124          .width('100%')
125          .height('48vp')
126          .padding({top: 20, bottom: 8})
127
128          List() {
129            ForEach(this.localList, (item: string) => {
130              ListItem() {
131                EntryComponentForMulti({
132                  item: item,
133                  localList: this.localList.toString(),
134                  onChange: (data:string) => this.onChange(data)
135                })
136              }
137              .width('100%')
138              .height(48)
139            })
140          }
141          .listDirection(Axis.Vertical)
142          .divider({ strokeWidth: '1vp', color: "#33182431", startMargin: 0, endMargin: 12})
143
144          Row() {
145            Text($r('app.string.more_devices'))
146              .fontSize($r("sys.float.ohos_id_text_size_body2"))
147              .fontColor("#99182431")
148              .fontWeight(FontWeight.Medium)
149          }
150          .width('100%')
151          .height('48vp')
152          .padding({top: 20, bottom: 8})
153
154          if (this.isShowMore) {
155            List() {
156              ForEach(this.moreList, (item: string) => {
157                ListItem() {
158                  EntryComponentForMulti({
159                    item: item,
160                    localList: this.localList.toString(),
161                    onChange: (data: string) => this.onChange(data)
162                  })
163                }
164                .width('100%')
165                .height(48)
166              })
167            }
168            .listDirection(Axis.Vertical)
169            .divider({ strokeWidth: 0.5, color: '#33000000', startMargin: 0, endMargin: 12 })
170          }
171
172          Column() {
173            Button({ type: ButtonType.Capsule, stateEffect: true }) {
174              Text($r('app.string.cancel'))
175                .fontSize($r("sys.float.ohos_id_text_size_sub_title2"))
176                .fontColor('#1095E8')
177                .fontWeight(FontWeight.Medium)
178                .height('100%')
179                .height('100%')
180            }
181            .width('100%')
182            .height('100%')
183            .backgroundColor('#00ffffff')
184            .onClick(() => {
185              if (this.controller) {
186                this.controller.close();
187              }
188            })
189          }
190          .height(56)
191          .width('100%')
192          .margin({ top: 8 }).padding({ bottom: 16 })
193        }
194        .width('100%')
195        .backgroundColor(Color.White)
196        .padding({ left: 24, right: 24 })
197        .borderRadius($r("sys.float.ohos_id_corner_radius_default_xl"))
198        .useSizeType({
199          xs: { span: 4, offset: this.useSizeTypeOffset },
200          sm: { span: 4, offset: this.useSizeTypeOffset },
201          md: { span: 4, offset: this.useSizeTypeOffset },
202          lg: { span: 4, offset: this.useSizeTypeOffset }
203        })
204      }.width('100%')
205    }
206  }
207}