• 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  aboutToDisappear(): void {
57    delete this.mSwiperController;
58    this.mSwiperController = null;
59    this.mFormDialogController = null;
60    this.cancel = null;
61    this.confirm = null;
62  }
63
64  /**
65   * Get current form information by bundle name.
66   */
67  private async getCurrentFormInfo() {
68    this.mFormModel.getFormsInfoByBundleName(this.bundleName);
69  }
70
71  /**
72   * Get choose card info from current form information.
73   *
74   * @return <any> formCardItem
75   */
76  private getChooseCard() {
77    let formCardItem: any = {};
78    formCardItem.id = this.mFormIdList[this.mSwiperIndex];
79    let count = 0;
80    let isStop = false;
81    for (let i = 0; i < this.formItem.length; i++) {
82      if (isStop) break;
83      for (let j = 0; j < this.formItem[i].supportDimensions.length; j++) {
84        if (count === this.mSwiperIndex) {
85          formCardItem.name = this.formItem[i].cardName;
86          formCardItem.bundleName = this.formItem[i].bundleName;
87          formCardItem.abilityName = this.formItem[i].abilityName;
88          formCardItem.moduleName = this.formItem[i].moduleName;
89          formCardItem.dimension = this.formItem[i].supportDimensions[j];
90          formCardItem.formConfigAbility = this.formItem[i].formConfigAbility;
91          formCardItem.appLabelId = this.appLabelId;
92          isStop = true;
93          break;
94        }
95        count++;
96      }
97    }
98    return formCardItem;
99  }
100
101  /**
102   * Keep the form which be added to the desktop, and delete the remaining forms.
103   */
104  private clearNoUseFormById(): void {
105    let id = this.mFormIdList[this.mSwiperIndex];
106    for (let i = 0; i < this.mFormIdList.length; i++) {
107      if (i != this.mSwiperIndex) {
108        this.mFormModel.deleteFormByFormID(this.mFormIdList[i]);
109      }
110    }
111  }
112
113  /**
114   * Delete all form by id.
115   */
116  private clearAllFormById(): void {
117    for (let i = 0; i < this.mFormIdList.length; i++) {
118        this.mFormModel.deleteFormByFormID(this.mFormIdList[i]);
119    }
120  }
121
122  build() {
123    Column() {
124      Text(this.appName)
125        .width('70%')
126        .fontSize(StyleConstants.DEFAULT_FORM_MGR_TEXT_FONT_SIZE)
127        .margin({ top: StyleConstants.DEFAULT_FORM_MARGIN, bottom: StyleConstants.DEFAULT_FORM_MARGIN })
128        .textAlign(TextAlign.Center)
129      Column({ space: 5 }) {
130        Swiper(this.mSwiperController) {
131          ForEach(this.formItem, (formItem) => {
132            ForEach(formItem.supportDimensions, (dimensionItem) => {
133              Column() {
134                Text(formItem.description)
135                  .width('70%')
136                  .fontSize(StyleConstants.DEFAULT_FORM_MGR_TEXT_FONT_SIZE)
137                  .margin({ top: StyleConstants.DEFAULT_FORM_MARGIN, bottom: StyleConstants.DEFAULT_FORM_MARGIN })
138                  .textAlign(TextAlign.Center)
139              Column() {
140                Flex({
141                  direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
142                  if (this.bundleName == formItem.bundleName) {
143                    FormComponent({
144                      id: formItem.cardId,
145                      name: formItem.cardName,
146                      bundle: formItem.bundleName,
147                      ability: formItem.abilityName,
148                      module: formItem.moduleName,
149                      dimension: dimensionItem,
150                    })
151                      .enabled(false)
152                      .focusable(false)
153                    .clip(new Rect({
154                      width: this.mFormComponentWidth[dimensionItem - 1],
155                      height: this.mFormComponentHeight[dimensionItem - 1],
156                      radius: 24
157                    }))
158                    .size({
159                      width: this.mFormComponentWidth[dimensionItem - 1],
160                      height: this.mFormComponentHeight[dimensionItem - 1]
161                    })
162                    .allowUpdate(this.mAllowUpdate)
163                    .visibility(Visibility.Visible)
164                    .onAcquired((form) => {
165                      Log.showDebug(TAG, `FormComponent card id is: ${form.id}`);
166                      this.mFormIdList.push(form.id);
167                    })
168                    .onError((error) => {
169                      Log.showDebug(TAG, `FormComponent error msg: ${error.msg}`);
170                    })
171                  }
172                }
173              }
174              .height('70%')
175              .hitTestBehavior(HitTestMode.Block)
176              }
177            }, (dimensionItem) => JSON.stringify(dimensionItem))
178          }, (formItem) => JSON.stringify(formItem))
179        }
180        .height('100%')
181        .loop(false)
182        .index(0)
183        .onChange((index: number) => {
184          this.mSwiperIndex = index;
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