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 { Log } from '../../../../../../common/src/main/ets/default/utils/Log' 17import getStore, { OhCombinedState } from '../../../../../../common/src/main/ets/default/redux/store' 18import { Action } from '../../../../../../common/src/main/ets/default/redux/actions/Action' 19import { SettingManager } from '../../../../../../common/src/main/ets/default/setting/SettingManager' 20import wantConstant from '@ohos.ability.wantConstant'; 21import { EventBus } from '../../../../../../common/src/main/ets/default/worker/eventbus/EventBus' 22import EventBusManager from '../../../../../../common/src/main/ets/default/worker/eventbus/EventBusManager' 23import ComponentPosition from '../../../../../../common/src/main/ets/default/utils/ComponentPosition'; 24import { PreferencesService, PersistType } from '../../../../../../common/src/main/ets/default/featurecommon/preferences/PreferencesService' 25 26let controlLandState = (state: OhCombinedState) => { 27 return { 28 isThirdPartyCall: state.ContextReducer.isThirdPartyCall, 29 isFaCall: state.ContextReducer.isFaCall, 30 action: state.ContextReducer.action, 31 uiEnable: state.ContextReducer.uiEnable, 32 modeIndex: state.ModeReducer.modeIndex, 33 mode: state.ModeReducer.mode, 34 isShowMoreList: state.ModeReducer.isShowMoreList, 35 } 36} 37 38let controlLandDispatcher = (dispatch) => { 39 return { 40 changeToMode: (mode: string) => { 41 dispatch(Action.uiState(false)) 42 dispatch(Action.updateMode(mode)) 43 dispatch(Action.changeMode(mode)) 44 dispatch(Action.updateShowBigTextFlag(true)) 45 }, 46 updateModeIndex: (index: number) => { 47 dispatch(Action.updateModeIndex(index)) 48 }, 49 updateShowMoreList: (isShowMoreList) => { 50 dispatch(Action.updateShowMoreList(isShowMoreList)) 51 }, 52 thirdPartyCall: (isThirdPartyCall: boolean, action: string) => { 53 dispatch(Action.thirdPartyCall(isThirdPartyCall, action)) 54 }, 55 initAction: (action: string) => { 56 dispatch(Action.initAction(action)) 57 }, 58 initMode: (mode: string) => { 59 dispatch(Action.initMode(mode)) 60 }, 61 changeXComponentSize: (xComponentWidth: number, xComponentHeight: number) => { 62 dispatch(Action.changeXComponentSize(xComponentWidth, xComponentHeight)) 63 }, 64 } 65} 66 67 68@Component 69export struct ControlLand { 70 private TAG: string = '[ControlLand]:' 71 appEventBus: EventBus = EventBusManager.getInstance().getEventBus() 72 private scroller: Scroller = new Scroller() 73 private settingManager = SettingManager.getInstance() 74 private mConnect: any 75 private modeArray: Array<string> = ['MULTI', 'PHOTO', 'VIDEO'] //, 'MORE' 76 private touchOff: boolean = true 77 private scrollDistance: number = 0 78 protected mPreferencesService: PreferencesService = PreferencesService.getInstance() 79 80 @State state: any = {} 81 @State startScroll: number = 0 82 @State endScroll: number = 0 83 @State index: number = 0 84 @Link screenSize: any 85 86 87 aboutToAppear(): void { 88 Log.info(`${this.TAG} aboutToAppear E`) 89 this.mConnect = getStore().connect(controlLandState, controlLandDispatcher)(this.state) 90 this.appEventBus.on(Action.ACTION_SWIPE_MODE, this.swipeChangeMode.bind(this)) 91 Log.info(`${this.TAG} aboutToAppear X`) 92 } 93 94 aboutToDisappear(): void { 95 Log.info(`${this.TAG} aboutToDisappear E`) 96 this.appEventBus.off(Action.ACTION_SWIPE_MODE, this.swipeChangeMode.bind(this)) 97 this.mConnect?.destroy() 98 Log.info(`${this.TAG} aboutToDisappear X`) 99 } 100 101 private closeMoreListView() { 102 if (this.state.isShowMoreList) { 103 this.state.updateShowMoreList(false) 104 } 105 } 106 107 private changeToMode(modeIndex: number, callType?: string) { 108 Log.debug(`${this.TAG} changeToMode modeIndex: ${modeIndex} E`) 109 this.scroller.scrollToIndex(modeIndex) 110 if (callType === 'begin') return; 111 if (this.modeArray[modeIndex] !== this.state.mode) { 112 Log.debug(`${this.TAG} this.state.changeToMode(${this.modeArray[modeIndex]})`) 113 this.state.changeToMode(this.modeArray[modeIndex]) 114 let xComponentSize = this.settingManager.getPreviewDisplaySize(this.state.mode) 115 this.state.changeXComponentSize(xComponentSize.width, xComponentSize.height) 116 this.mPreferencesService.putModeValue(PersistType.FOR_AWHILE, modeIndex) 117 this.mPreferencesService.flushMode() 118 } 119 Log.debug(`${this.TAG} changeToMode X`) 120 } 121 122 private getModeFontWeight(modeIndex: number): FontWeight { 123 if (this.state.mode === this.modeArray[modeIndex]) { 124 return FontWeight.Bold 125 } else { 126 return FontWeight.Regular 127 } 128 } 129 130 private swipeChangeMode(data) { 131 this.changeToMode(data.swipeModeIndex) 132 } 133 134 private scrollSwitchMode(callType: string) { 135 if (this.index == 1 && Math.abs(this.scrollDistance) <= px2vp(20)) { 136 this.changeToMode(1, callType) 137 } 138 if (this.index == 1 && (this.scrollDistance) > px2vp(20)) { 139 this.changeToMode(0, callType) 140 } 141 if (this.index == 1 && (this.scrollDistance) < px2vp(-20)) { 142 this.changeToMode(2, callType) 143 } 144 if (this.index == 0 && (this.scrollDistance > px2vp(-25))) { 145 this.changeToMode(0, callType) 146 } 147 if (this.index == 0 && this.scrollDistance >= px2vp(-50) && this.scrollDistance <= px2vp(-25)) { 148 this.changeToMode(1, callType) 149 } 150 if (this.index == 0 && this.scrollDistance < px2vp(-50)) { 151 this.changeToMode(2, callType) 152 } 153 if (this.index == 2 && (this.scrollDistance < px2vp(25))) { 154 this.changeToMode(2, callType) 155 } 156 if (this.index == 2 && this.scrollDistance >= px2vp(25) && this.scrollDistance <= px2vp(50)) { 157 this.changeToMode(1, callType) 158 } 159 if (this.index == 2 && this.scrollDistance > px2vp(50)) { 160 this.changeToMode(0, callType) 161 } 162 } 163 164 build() { 165 Stack({ alignContent: Alignment.TopStart }) { 166 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.End }) { 167 if ((this.state.isThirdPartyCall || this.state.isFaCall) && this.state.mode === 'PHOTO') { 168 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.End }) { 169 Text($r('app.string.photo_mode')) 170 .fontSize($r("sys.float.ohos_id_text_size_body1")) 171 .fontColor(Color.White) 172 .fontWeight(FontWeight.Bold) 173 }.layoutWeight(1).height('100%') 174 } else if ((this.state.isThirdPartyCall || this.state.isFaCall) && this.state.mode === 'VIDEO') { 175 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.End }) { 176 Text($r('app.string.video_mode')) 177 .fontSize($r("sys.float.ohos_id_text_size_body1")) 178 .fontColor(Color.White) 179 .fontWeight(FontWeight.Bold) 180 }.layoutWeight(1).height('100%') 181 } else { 182 List({ initialIndex: this.state.modeIndex, scroller: this.scroller }) { 183 if(ComponentPosition.isUnfoldControl(this.screenSize.width, this.screenSize.height)) { 184 ListItem() { 185 }.width('100%').height(32) 186 } 187 ListItem() { 188 }.width('100%').height(32) 189 190 ListItem() { 191 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.End }) { 192 Text($r('app.string.multi_mode')) 193 .fontSize($r("sys.float.ohos_id_text_size_body1")) 194 .fontColor(Color.White) 195 .enabled(this.state.uiEnable) 196 .onClick(async () => { 197 this.changeToMode(0) 198 }) 199 .fontWeight(this.getModeFontWeight(0)) 200 }.width('100%').height('100%') 201 }.width('100%').height(32) 202 203 ListItem() { 204 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.End }) { 205 Text($r('app.string.photo_mode')) 206 .fontSize($r("sys.float.ohos_id_text_size_body1")) 207 .fontColor(Color.White) 208 .enabled(this.state.uiEnable) 209 .onClick(async () => { 210 this.changeToMode(1) 211 }) 212 .fontWeight(this.getModeFontWeight(1)) 213 }.width('100%').height('100%') 214 }.width('100%').height(32) 215 216 ListItem() { 217 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.End }) { 218 Text($r('app.string.video_mode')) 219 .fontSize($r("sys.float.ohos_id_text_size_body1")) 220 .fontColor(Color.White) 221 .enabled(this.state.uiEnable) 222 .onClick(async () => { 223 this.changeToMode(2) 224 }) 225 .fontWeight(this.getModeFontWeight(2)) 226 }.width('100%').height('100%') 227 }.width('100%').height(32) 228 ListItem() { 229 }.width('100%').height(32) 230 231 ListItem() { 232 }.width('100%').height(32) 233 } 234 .layoutWeight(1) 235 .height('100%') 236 .edgeEffect(EdgeEffect.None) 237 .chainAnimation(false) 238 .onScrollIndex((firstIndex: number, lastIndex: number) => { 239 Log.debug(`${this.TAG} Control scroll index first: ${firstIndex}, last: ${lastIndex}`) 240 this.state.updateModeIndex(firstIndex) 241 Log.debug(`${this.TAG} onScrollIndex this.state.modeIndex: ${this.state.modeIndex}`) 242 }) 243 // @ts-ignore 244 .onScrollBegin(() => { 245 if (!this.touchOff) this.scrollSwitchMode('begin') 246 }) 247 .enabled(this.state.uiEnable) 248 .onTouch((event: TouchEvent) => { 249 if (event.type === TouchType.Down) { 250 this.touchOff = true 251 this.index = this.modeArray.indexOf(this.state.mode) 252 this.startScroll = event.touches[0].screenY 253 } 254 if (event.type === TouchType.Up) { 255 this.endScroll = event.touches[0].screenY 256 this.scrollDistance = px2vp(this.endScroll - this.startScroll) 257 this.touchOff = false 258 this.scrollSwitchMode('touch') 259 } 260 }) 261 } 262 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Center }) { 263 Column() { 264 }.borderRadius(3).width(6).height(6).backgroundColor('#007DFF') 265 // .shadow({radius: 5, color: 'argb(#7F000000)', offsetX: 0, offsetY: 0}) 266 }.width(18).height('100%').margin({ left: 8 }) 267 }.width('100%').height('100%') 268 }.width(98).height(ComponentPosition.getControlHeight(this.screenSize.width, this.screenSize.height)).zIndex(2) 269 } 270}