1/** 2 * Copyright (c) 2021-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 */ 15import { Log } from '../utils/Log'; 16import { FormModel } from '../model/FormModel'; 17import { StyleConstants } from '../constants/StyleConstants'; 18import { CardItemInfo } from '../bean/CardItemInfo'; 19 20const TAG = 'FormManagerDialog'; 21 22/** 23 * Form manager view Component (pad adaptation). 24 */ 25@CustomDialog 26export struct FormManagerDialog { 27 @StorageLink('formMgrItem') formItem: Array<CardItemInfo> = []; 28 @State mAllowUpdate: boolean = false; 29 private mSwiperController: SwiperController = new SwiperController() 30 private mFormModel: FormModel = FormModel.getInstance(); 31 private mSwiperIndex: number = 0; 32 private mFormIdList: number[] = []; 33 private mFormComponentWidth: number[] = [ 34 StyleConstants.FORM_MANAGER_VIEW_CARD_WIDTH * 2, 35 StyleConstants.FORM_MANAGER_VIEW_CARD_WIDTH * 2, 36 StyleConstants.FORM_MANAGER_VIEW_CARD_WIDTH * 4, 37 StyleConstants.FORM_MANAGER_VIEW_CARD_WIDTH * 4, 38 StyleConstants.FORM_MANAGER_VIEW_CARD_WIDTH * 1, 39 StyleConstants.FORM_MANAGER_VIEW_CARD_WIDTH * 1, 40 StyleConstants.FORM_MANAGER_VIEW_CARD_WIDTH * 4, 41 ]; 42 private mFormComponentHeight: number[] = [ 43 StyleConstants.FORM_MANAGER_VIEW_CARD_HEIGHT * 1, 44 StyleConstants.FORM_MANAGER_VIEW_CARD_HEIGHT * 2, 45 StyleConstants.FORM_MANAGER_VIEW_CARD_HEIGHT * 2, 46 StyleConstants.FORM_MANAGER_VIEW_CARD_HEIGHT * 4, 47 StyleConstants.FORM_MANAGER_VIEW_CARD_HEIGHT * 2, 48 StyleConstants.FORM_MANAGER_VIEW_CARD_HEIGHT * 1, 49 StyleConstants.FORM_MANAGER_VIEW_CARD_HEIGHT * 6, 50 ]; 51 52 mFormDialogController?: CustomDialogController; 53 cancel = (callback?: Function) => {}; 54 confirm = (formCardItem: CardItemInfo) => {}; 55 bundleName: string = ''; 56 appName: string = ''; 57 appLabelId: number = 0; 58 59 aboutToAppear(): void { 60 this.mFormModel = FormModel.getInstance(); 61 this.cancel(this.clearAllFormById); 62 this.getCurrentFormInfo(); 63 } 64 65 aboutToDisappear(): void { 66 } 67 68 /** 69 * Get current form information by bundle name. 70 */ 71 private async getCurrentFormInfo() { 72 this.mFormModel.getFormsInfoByBundleName(this.bundleName); 73 } 74 75 /** 76 * Get choose card info from current form information. 77 * 78 * @return <any> formCardItem 79 */ 80 private getChooseCard() { 81 let formCardItem: CardItemInfo = new CardItemInfo(); 82 formCardItem.cardId = this.mFormIdList[this.mSwiperIndex]; 83 let count = 0; 84 let isStop = false; 85 for (let i = 0; i < this.formItem.length; i++) { 86 if (isStop || !this.formItem[i]?.supportDimensions.length) { 87 break; 88 } 89 for (let j = 0; j < this.formItem[i].supportDimensions.length; j++) { 90 if (count === this.mSwiperIndex) { 91 formCardItem.cardName = this.formItem[i].cardName; 92 formCardItem.bundleName = this.formItem[i].bundleName; 93 formCardItem.abilityName = this.formItem[i].abilityName; 94 formCardItem.moduleName = this.formItem[i].moduleName; 95 formCardItem.cardDimension = this.formItem[i].supportDimensions[j]; 96 formCardItem.formConfigAbility = this.formItem[i].formConfigAbility; 97 formCardItem.appLabelId = this.appLabelId; 98 isStop = true; 99 break; 100 } 101 count++; 102 } 103 } 104 return formCardItem; 105 } 106 107 /** 108 * Keep the form which be added to the desktop, and delete the remaining forms. 109 */ 110 private clearNoUseFormById(): void { 111 let id = this.mFormIdList[this.mSwiperIndex]; 112 for (let i = 0; i < this.mFormIdList.length; i++) { 113 if (i != this.mSwiperIndex) { 114 this.mFormModel.deleteFormByFormID(this.mFormIdList[i]); 115 } 116 } 117 } 118 119 /** 120 * Delete all form by id. 121 */ 122 private clearAllFormById = (): void => { 123 for (let i = 0; i < this.mFormIdList.length; i++) { 124 this.mFormModel.deleteFormByFormID(this.mFormIdList[i]); 125 } 126 } 127 128 build() { 129 Column() { 130 Text(this.appName) 131 .width('70%') 132 .fontSize(StyleConstants.DEFAULT_FORM_MGR_TEXT_FONT_SIZE) 133 .margin({ top: StyleConstants.DEFAULT_FORM_MARGIN, bottom: StyleConstants.DEFAULT_FORM_MARGIN }) 134 .textAlign(TextAlign.Center) 135 Column({ space: 5 }) { 136 Swiper(this.mSwiperController) { 137 ForEach(this.formItem, (formItem: CardItemInfo) => { 138 ForEach(formItem.supportDimensions, (dimensionItem: number) => { 139 Column() { 140 Text(formItem.description) 141 .width('70%') 142 .fontSize(StyleConstants.DEFAULT_FORM_MGR_TEXT_FONT_SIZE) 143 .margin({ top: StyleConstants.DEFAULT_FORM_MARGIN, bottom: StyleConstants.DEFAULT_FORM_MARGIN }) 144 .textAlign(TextAlign.Center) 145 Column() { 146 Flex({ 147 direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 148 if (this.bundleName == formItem.bundleName) { 149 FormComponent({ 150 id: formItem.cardId as number, 151 name: formItem.cardName as string, 152 bundle: formItem.bundleName as string, 153 ability: formItem.abilityName as string, 154 module: formItem.moduleName as string, 155 dimension: dimensionItem, 156 }) 157 .enabled(false) 158 .focusable(false) 159 .clip(new Rect({ 160 width: this.mFormComponentWidth[dimensionItem - 1], 161 height: this.mFormComponentHeight[dimensionItem - 1], 162 radius: 24 163 })) 164 .size({ 165 width: this.mFormComponentWidth[dimensionItem - 1], 166 height: this.mFormComponentHeight[dimensionItem - 1] 167 }) 168 .allowUpdate(this.mAllowUpdate) 169 .visibility(Visibility.Visible) 170 .onAcquired((form) => { 171 Log.showDebug(TAG, `FormComponent card id is: ${form.id}`); 172 this.mFormIdList.push(form.id); 173 }) 174 .onError((error) => { 175 Log.showDebug(TAG, `FormComponent error msg: ${error.msg}`); 176 }) 177 } 178 } 179 } 180 .height('70%') 181 .hitTestBehavior(HitTestMode.Block) 182 } 183 }, (dimensionItem: number) => JSON.stringify(dimensionItem)) 184 }, (formItem: CardItemInfo) => JSON.stringify(formItem)) 185 } 186 .height('100%') 187 .loop(false) 188 .duration(80) 189 .cachedCount(1) 190 .index(0) 191 .onChange((index: number) => { 192 if (this.mSwiperIndex !== index) { 193 this.mSwiperIndex = index; 194 } 195 }) 196 } 197 .height('85%') 198 Flex({ justifyContent: FlexAlign.SpaceAround }) { 199 Button() { 200 Text($r('app.string.cancel')) 201 .fontSize(StyleConstants.DEFAULT_BADGE_FONT_SIZE) 202 .fontColor(StyleConstants.BUTTON_FONT_COLOR) 203 } 204 .backgroundColor(StyleConstants.DEFAULT_BG_COLOR) 205 .height(StyleConstants.DEFAULT_BUTTON_HEIGHT) 206 .width(StyleConstants.DEFAULT_BUTTON_WIDTH) 207 .onClick(() => { 208 this.mFormDialogController?.close(); 209 this.clearAllFormById(); 210 }) 211 Divider() 212 .vertical(true) 213 .color(StyleConstants.DEFAULT_DIVIDER_COLOR) 214 .height(StyleConstants.DEFAULT_BUTTON_HEIGHT) 215 Button() { 216 Text($r('app.string.add_to_desktop')) 217 .fontSize(StyleConstants.DEFAULT_BADGE_FONT_SIZE) 218 .fontColor(StyleConstants.BUTTON_FONT_COLOR) 219 } 220 .backgroundColor(StyleConstants.DEFAULT_BG_COLOR) 221 .height(StyleConstants.DEFAULT_BUTTON_HEIGHT) 222 .width(StyleConstants.DEFAULT_BUTTON_WIDTH) 223 .onClick(() => { 224 this.mFormDialogController?.close(); 225 this.confirm(this.getChooseCard()); 226 this.clearNoUseFormById(); 227 }) 228 } 229 } 230 .backgroundColor(Color.White) 231 .padding({ 232 bottom: StyleConstants.DEFAULT_DIALOG_BOTTOM_MARGIN 233 }) 234 .border({ 235 radius: StyleConstants.DEFAULT_DIALOG_RADIUS 236 }) 237 .width(StyleConstants.FORM_MANAGER_VIEW_WIDTH) 238 .height(StyleConstants.FORM_MANAGER_VIEW_HEIGHT) 239 } 240} 241