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