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