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 emitter from '@ohos.events.emitter'; 17import Constants from '../constant'; 18import GlobalContext from '../GlobalContext'; 19import common from '@ohos.app.ability.common'; 20import { BusinessError } from '@ohos.base'; 21 22const TAG = '[DLPManager_Select]'; 23 24class Types_ { 25 value: Resource = { 26 id: 0, 27 type: 0, 28 params: [], 29 bundleName: '', 30 moduleName: '' 31 } 32 data: string = '' 33 index: number = 0 34} 35 36interface Staff { 37 authAccount: string, 38 avatar: string, 39 id: number 40} 41 42@Component 43struct permissionTypeSelect { 44 @State text: string = ""; 45 @State index: number = 0 46 @Prop isReadType: boolean = false; 47 @State permissionTypes: Types_[] = [ 48 { value: $r('app.string.PERMISSION_TYPE_SELECT_TARGET'), data: 'target', index: 0 }, 49 { value: $r('app.string.PERMISSION_TYPE_SELECT_ALL'), data: 'all', index: 1 }, 50 { value: $r('app.string.PERMISSION_TYPE_SELECT_SELF'), data: 'self', index: 2 } 51 ]; 52 @Link @Watch('selectedItemUpdated') selectedItem: Types_; 53 @Link staffArray: Staff[]; 54 @Prop @Watch('onCountUpdated') isDisable: boolean = false; 55 56 onCountUpdated(isDisable: boolean): void { 57 if (this.isReadType) { 58 if (this.isDisable) { 59 this.selectedItem = this.permissionTypes[Constants.ENCRYPTION_TYPE_SELECT]; 60 this.index = Constants.ENCRYPTION_TYPE_SELECT; 61 try { 62 (GlobalContext 63 .load('context') as common.UIAbilityContext).resourceManager.getStringValue($r('app.string.PERMISSION_TYPE_SELECT_ALL') 64 .id, (error: BusinessError, value: string) => { 65 if (error != null) { 66 console.log("error is " + error); 67 } else { 68 this.text = value; 69 } 70 }); 71 } catch (error) { 72 console.error(`callback getStringValue failed, error code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}.`); 73 } 74 } else { 75 this.selectedItem = this.permissionTypes[Constants.ENCRYPTION_TYPE_SELECT_INDEX]; 76 this.index = Constants.ENCRYPTION_TYPE_SELECT_INDEX; 77 try { 78 (GlobalContext 79 .load('context') as common.UIAbilityContext).resourceManager.getStringValue($r('app.string.PERMISSION_TYPE_SELECT_TARGET') 80 .id, (error: BusinessError, value: string) => { 81 if (error != null) { 82 console.log("error is " + error); 83 } else { 84 this.text = value; 85 } 86 }); 87 } catch (error) { 88 console.error(`callback getStringValue failed, error code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}.`); 89 } 90 } 91 } 92 } 93 94 selectedItemUpdated(selectedItem: object): void { 95 this.index = this.selectedItem.index; 96 try { 97 (GlobalContext 98 .load('context') as common.UIAbilityContext).resourceManager.getStringValue(this.selectedItem.value?.id!, (error: BusinessError, value: string) => { 99 if (error != null) { 100 console.log("error is " + error); 101 } else { 102 this.text = value; 103 } 104 }); 105 } catch (error) { 106 console.error(`callback getStringValue failed, error code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}.`); 107 } 108 } 109 110 aboutToAppear() { 111 if (this.isReadType) { 112 this.permissionTypes = this.permissionTypes.filter(item => { 113 return item.data !== 'self'; 114 }) 115 } 116 try { 117 (GlobalContext 118 .load('context') as common.UIAbilityContext).resourceManager.getStringValue(this.permissionTypes[this.index].value?.id!, (error: BusinessError, value: string) => { 119 if (error != null) { 120 console.log("error is " + error); 121 } else { 122 this.text = value; 123 } 124 }); 125 } catch (error) { 126 console.error(`callback getStringValue failed, error code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}.`); 127 } 128 let innerEvent: emitter.InnerEvent = { 129 eventId: Constants.ENCRYPTION_EMIT_LANGUAGE_VALUE 130 }; 131 let callback = (eventData: emitter.EventData) => { 132 try { 133 (GlobalContext 134 .load('context') as common.UIAbilityContext).resourceManager.getStringValue(this.permissionTypes[this.index].value?.id!, (error: BusinessError, value: string) => { 135 if (error != null) { 136 console.log("error is " + error); 137 } else { 138 this.text = value; 139 } 140 }); 141 } catch (error) { 142 console.error(`callback getStringValue failed, error code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}.`); 143 } 144 }; 145 emitter.on(innerEvent, callback); 146 setTimeout(() => { 147 this.selectedItem = this.permissionTypes[0]; 148 }, Constants.ENCRYPTION_SET_TIMEOUT_TIME) 149 } 150 151 private getSelectOptionArr(): Array<SelectOption> { 152 let ret: Array<SelectOption> = []; 153 this.permissionTypes.forEach( 154 (item) => { 155 ret.push({ value: item.value }) 156 } 157 ); 158 return ret; 159 } 160 161 build() { 162 Select(this.getSelectOptionArr()) 163 .font({ 164 size: $r('sys.float.ohos_id_text_size_body1'), 165 weight: FontWeight.Medium, 166 }) 167 .optionFont({ 168 weight: FontWeight.Regular 169 }) 170 .selectedOptionFont({ 171 weight: FontWeight.Regular 172 }) 173 .selectedOptionBgColor(Color.Transparent) 174 .selected(this.index) 175 .value(`${this.text}${ 176 this.selectedItem?.data === 'target' && this.staffArray.length > 0 ? 177 ` (${this.staffArray.length})` : 178 '' 179 }`) 180 .onSelect((index: number, text?: string) => { 181 this.selectedItem = this.permissionTypes[index] 182 this.index = index; 183 this.text = text as string; 184 }) 185 .opacity(this.isDisable ? Constants.DU_LINE_WIDTH : Constants.FOOTER_OPACITY_ONE) 186 .enabled(this.isDisable ? false : true) 187 } 188} 189 190export default permissionTypeSelect;