• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 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 */
15
16import router from '@ohos.router';
17import CommonEvent from '@ohos.commonEvent';
18import {
19  AppItemInfo,
20  CardItemInfo,
21  CheckEmptyUtils,
22  EventConstants,
23  CommonConstants,
24  FormModel,
25  Logger,
26  FormCardItem
27} from '@ohos/base';
28import { DesktopLayoutModel } from '@ohos/desktop';
29
30
31const TAG: string = 'FormManagerComponent'
32
33@Component
34export default struct FormManagerComponent {
35  @StorageLink('formAppInfo') formAppInfo: AppItemInfo = new AppItemInfo();
36  @StorageLink('formMgrItem') formItem: Array<CardItemInfo> = [];
37  @StorageLink('DesktopLayoutModel') desktopLayoutModel: DesktopLayoutModel | undefined = undefined;
38  private mSwiperController: SwiperController = new SwiperController();
39  private mSwiperIndex: number = 0;
40  private mFormIdList: number[] = [];
41
42  build() {
43    Column() {
44      Text(this.formAppInfo?.appName)
45        .fontColor(Color.White)
46        .fontSize(22)
47      Column({ space: 5 }) {
48        Swiper(this.mSwiperController) {
49          ForEach(this.formItem, (formItem: CardItemInfo) => {
50            ForEach(formItem.supportDimensions, (dimensionItem: number) => {
51              Column() {
52                Text(formItem.description)
53                  .width('70%')
54                  .fontColor(0xe5ffffff)
55                  .fontSize(20)
56                  .textAlign(TextAlign.Center)
57                Column() {
58                  if (this.formAppInfo?.bundleName == formItem.bundleName) {
59                    FormComponent({
60                      id: formItem.cardId,
61                      name: formItem.cardName,
62                      bundle: formItem.bundleName,
63                      ability: formItem.abilityName,
64                      module: formItem.moduleName,
65                      dimension: dimensionItem,
66                    })
67                      .clip(new Rect({
68                        width: CommonConstants.FORM_COMPONENT_WIDTH[dimensionItem - 1],
69                        height: CommonConstants.FORM_COMPONENT_HEIGHT[dimensionItem - 1],
70                        radius: CommonConstants.DEFAULT_CARD_RADIUS
71                      }))
72                      .size({
73                        width: CommonConstants.FORM_COMPONENT_WIDTH[dimensionItem - 1],
74                        height: CommonConstants.FORM_COMPONENT_HEIGHT[dimensionItem - 1]
75                      })
76                      .onAcquired((form) => {
77                        this.mFormIdList.push(form.id);
78                        Logger.info(TAG, `onAcquired mFormIdList: ${JSON.stringify(this.mFormIdList)}`);
79                      })
80                  }
81                }
82                .height('80%')
83                .justifyContent(FlexAlign.Center)
84              }.width('100%')
85            }, (dimensionItem: number) => JSON.stringify(dimensionItem))
86          }, (formItem: CardItemInfo) => JSON.stringify(formItem))
87        }
88        .width('100%')
89        .height('100%')
90        .loop(false)
91        .index(this.mSwiperIndex)
92        .indicatorStyle({
93          selectedColor: Color.White
94        })
95        .onChange((index: number) => {
96          this.mSwiperIndex = index
97        })
98      }.alignItems(HorizontalAlign.Center)
99      .height('70%')
100
101      Blank()
102      Button({ type: ButtonType.Capsule }) {
103        Row() {
104          Text($r('app.string.add_to_desktop'))
105            .fontColor(Color.White)
106            .fontSize(22)
107        }
108      }
109      .backgroundColor(0x66ffffff)
110      .width(220)
111      .height(50)
112      .margin({
113        top: 20,
114        bottom: 20,
115        left: 20,
116        right: 20 })
117      .onClick(async () => {
118        Logger.info(TAG, `add card to desktop`);
119        let selectForm = this.getSelectedFormInfo();
120        if (!CheckEmptyUtils.isEmpty(selectForm)) {
121          await this.desktopLayoutModel?.createCardToDeskTop(this.getSelectedFormInfo());
122          CommonEvent.publish(EventConstants.EVENT_ENTER_HOME, () => {
123            Logger.info(TAG, 'publish EVENT_ENTER_HOME');
124          })
125        }
126        router.back();
127      })
128    }
129  }
130
131  aboutToAppear(): void {
132    Logger.info(TAG, `aboutToAppear formAppInfo: ${JSON.stringify(this.formAppInfo)}`);
133    this.getCurrentFormInfo();
134  }
135
136  /**
137   * Get current form information by bundle name.
138   */
139  private async getCurrentFormInfo() {
140    let currentBundleFormsInfo: Array<CardItemInfo> = await FormModel.getFormsInfoByBundleName(this.formAppInfo?.bundleName);
141    AppStorage.SetOrCreate('formMgrItem', currentBundleFormsInfo);
142  }
143
144  getSelectedFormInfo() {
145    if (CheckEmptyUtils.isEmptyArr(this.formItem) || this.mSwiperIndex > this.formItem.length) {
146      return undefined;
147    }
148    let formCardItem = new FormCardItem();
149    let count = 0;
150    let isStop = false;
151    for (let i = 0; i < this.formItem.length; i++) {
152      if (isStop) {
153        break;
154      }
155      for (let j = 0; j < this.formItem[i].supportDimensions.length; j++) {
156        if (count === this.mSwiperIndex) {
157          formCardItem.cardId = this.mFormIdList[this.mSwiperIndex];
158          formCardItem.appName = this.formAppInfo.appName!;
159          formCardItem.cardName = this.formItem[i].cardName;
160          formCardItem.bundleName = this.formItem[i].bundleName;
161          formCardItem.abilityName = this.formItem[i].abilityName;
162          formCardItem.moduleName = this.formItem[i].moduleName;
163          formCardItem.dimension = this.formItem[i].supportDimensions[j];
164          formCardItem.formConfigAbility = this.formItem[i].formConfigAbility;
165          formCardItem.appLabelId = this.formAppInfo.appLabelId;
166          isStop = true;
167          break;
168        }
169        count++;
170      }
171    }
172    Logger.info(TAG, `getSelectedFormInfo formCardItem: ${JSON.stringify(formCardItem)}`);
173    return formCardItem;
174  }
175}