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