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