1/* 2 * Copyright (c) 2022 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 39@CustomDialog 40export default struct MultiCameraDialog { 41 private TAG: string = '[MultiCameraDialog]:' 42 private cameraService = CameraService.getInstance() 43 controller: CustomDialogController 44 cancel: () => void 45 confirm: () => void 46 private localList: string[] = [ 47 CameraId.FRONT, 48 CameraId.BACK 49 ] 50 @State private moreList: string[] = [] 51 private multiCameraMap = new Map() 52 @State state: any = {} 53 @State isShowMore: boolean = false 54 @State gridColumns: number = 12 55 @State useSizeTypeOffset: number = 4 56 @Link deviceType: string 57 @StorageLink('storageCameraId') storageCameraId: string = '' 58 59 aboutToAppear() { 60 Log.info(`${this.TAG} aboutToAppear.`) 61 getStore().connect(localState, localDispatcher)(this.state) 62 this.cameraService.getMultiCameraInfo(() => { 63 let cameraMap = new Map(this.cameraService.getCameraMap()) 64 let keys = cameraMap.keys() 65 this.moreList = [...keys] 66 Log.info(`${this.TAG} cameraMap length: ${this.moreList.length}`) 67 if (this.moreList.length !== 0) { 68 this.multiCameraMap = cameraMap 69 this.isShowMore = true 70 Log.info(`${this.TAG} getMultiCameraInfo finished.`) 71 } else { 72 this.isShowMore = false 73 Log.info(`${this.TAG} getMultiCameraInfo null.`) 74 } 75 }) 76 let localCameraInfo = this.cameraService.getLocalCameraMap() 77 if (!localCameraInfo.get('front')) { 78 this.localList.shift() 79 } else if (!localCameraInfo.get('back')) { 80 this.localList.pop() 81 } 82 if (this.deviceType === 'phone' || this .deviceType === 'default') { 83 this.gridColumns = 4 84 this.useSizeTypeOffset = 0 85 } else { 86 this.gridColumns = 12 87 this.useSizeTypeOffset = 4 88 } 89 } 90 91 private onChange(item): void { 92 Log.info(`${this.TAG} MultiCameraPosition ${JSON.stringify(item)}`) 93 if (item.includes('BACK')) { 94 this.storageCameraId = 'BACK' 95 } else if(item.includes('FRONT')) { 96 this.storageCameraId = 'FRONT' 97 } 98 this.state.changeCameraPosition(item) 99 this.cancel() 100 this.controller.close() 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) => { 130 ListItem() { 131 EntryComponentForMulti({ 132 item: item, 133 localList: this.localList.toString(), 134 onChange: (data) => 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) => { 157 ListItem() { 158 EntryComponentForMulti({ 159 item: item, 160 localList: this.localList.toString(), 161 onChange: (data) => 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 this.controller.close() 186 }) 187 } 188 .height(56) 189 .width('100%') 190 .margin({ top: 8 }).padding({ bottom: 16 }) 191 } 192 .width('100%') 193 .backgroundColor(Color.White) 194 .padding({ left: 24, right: 24 }) 195 .borderRadius($r("sys.float.ohos_id_corner_radius_default_xl")) 196 .useSizeType({ 197 xs: { span: 4, offset: this.useSizeTypeOffset }, 198 sm: { span: 4, offset: this.useSizeTypeOffset }, 199 md: { span: 4, offset: this.useSizeTypeOffset }, 200 lg: { span: 4, offset: this.useSizeTypeOffset } 201 }) 202 }.width('100%') 203 } 204 } 205}