• 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 */
15
16import {
17  AppMenu,
18} from '@ohos/common/component'
19import {
20  Log,
21  Trace,
22  StyleConstants,
23  CommonConstants,
24  MenuInfo,
25  localEventManager,
26  EventConstants,
27  LauncherDragItemInfo
28} from '@ohos/common';
29import SwiperPage from './SwiperPage';
30import { PageDesktopViewModel } from '../../viewmodel/PageDesktopViewModel';
31
32const TAG = 'GridSwiper';
33
34interface AppListInfo {
35  appGridInfo: LauncherDragItemInfo[][]
36}
37
38@Component
39export default struct GridSwiper {
40  @StorageLink('NavigationBarStatusValue') navigationBarStatusValue: boolean = false;
41  @StorageLink('isDesktopLoadFinished') desktopLoadFinished: boolean = false;
42  @Prop gridConfig: string;
43  @StorageLink('pageIndex') pageIndex: number = 0;
44  private mPageDesktopViewModel?: PageDesktopViewModel;
45  @StorageLink('appListInfo') @Watch('updatePageList') appListInfo: AppListInfo = { appGridInfo: [[]] };
46  @State pageList: number[] = [0];
47  private swiperController: SwiperController = new SwiperController();
48  dialogController?: CustomDialogController | null;
49  @StorageLink('desktopEventResponse') desktopEventResponse: boolean = true;
50  @State desktopIndicator: DotIndicator | DigitIndicator =
51  Indicator.dot().selectedColor(StyleConstants.DEFAULT_FONT_COLOR);
52
53  updatePageList(): void {
54    if (this.pageList.length !== this.appListInfo.appGridInfo.length) {
55      if (this.pageList.length < this.appListInfo.appGridInfo.length) {
56        for (let i = this.pageList.length; i < this.appListInfo.appGridInfo.length; i++) {
57          if (this.pageList.indexOf(i) === -1) {
58            this.pageList.push(i);
59          }
60        }
61      } else {
62        for (let i = this.pageList.length - 1; i >= this.appListInfo.appGridInfo.length; i--) {
63          if (this.pageList.indexOf(i) !== -1) {
64            this.pageList.splice(i, 1);
65          }
66        }
67      }
68    }
69    const NINE = 9;
70    if (this.pageList?.length > NINE) {
71      this.desktopIndicator = Indicator.digit().fontColor(StyleConstants.DEFAULT_FORM_MGR_FONT_COLOR)
72        .selectedFontColor(StyleConstants.DEFAULT_FONT_COLOR);
73    } else {
74      this.desktopIndicator = Indicator.dot().selectedColor(StyleConstants.DEFAULT_FONT_COLOR);
75    }
76  }
77
78  aboutToAppear(): void {
79    this.mPageDesktopViewModel = PageDesktopViewModel.getInstance();
80    this.mPageDesktopViewModel.setSwiperController(this.swiperController);
81  }
82
83  aboutToDisappear(): void {
84    Log.showInfo(TAG, 'aboutToDisappear');
85  }
86
87  private buildLog(): boolean {
88    let isDesktopLoadFinished = AppStorage.get('isDesktopLoadFinished') as boolean;
89    Log.showDebug(TAG, `build start ${isDesktopLoadFinished} page ${this.pageIndex}`);
90    return isDesktopLoadFinished == true;
91  }
92
93  @Builder MenuBuilder() {
94    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
95      AppMenu({
96        getMenuInfoList: () => this.getMenu()
97      })
98    }
99    .width(StyleConstants.CONTEXT_MENU_WIDTH)
100    .borderRadius(StyleConstants.DEFAULT_12)
101  }
102
103  private getMenu(): MenuInfo[] {
104    let menuInfoList: MenuInfo[] = [];
105    let setting = new MenuInfo();
106    setting.menuType = CommonConstants.MENU_TYPE_FIXED
107    setting.menuImgSrc = '/common/pics/ic_public_settings.svg'
108    setting.menuText = $r('app.string.into_settings')
109    setting.onMenuClick = () => {
110      Trace.start(Trace.CORE_METHOD_START_SETTINGS);
111      this.mPageDesktopViewModel?.intoSetting();
112    }
113    menuInfoList.push(setting);
114
115    let addOrDeleteBlankPage = new MenuInfo();
116    addOrDeleteBlankPage.menuType = CommonConstants.MENU_TYPE_FIXED
117    addOrDeleteBlankPage.menuImgSrc = this.mPageDesktopViewModel?.getBlankPageBtnIcon()
118    addOrDeleteBlankPage.menuText = this.mPageDesktopViewModel?.getBlankPageBtnStr()
119    addOrDeleteBlankPage.onMenuClick = () => {
120      this.mPageDesktopViewModel?.addOrDeleteBlankPage();
121    }
122    menuInfoList.push(addOrDeleteBlankPage);
123    return menuInfoList;
124  }
125
126  build() {
127    Column() {
128      if (this.buildLog()) {}
129        if (this.desktopLoadFinished) {
130          Swiper(this.swiperController) {
131            ForEach(this.pageList, (item: number, index: number) => {
132              if (AppStorage.get('deviceType') == CommonConstants.DEFAULT_DEVICE_TYPE) {
133                Column() {
134                  SwiperPage({
135                    appListInfo: $appListInfo,
136                    swiperPage: index.valueOf(),
137                    gridConfig: this.gridConfig,
138                    mPageDesktopViewModel: this.mPageDesktopViewModel
139                  }).id(`SwiperPage_${item}${index}`)
140                }
141                .gesture(
142                LongPressGesture({ repeat: false })
143                  .onAction((event: GestureEvent) => {
144                    this.dialogController?.open();
145                  })
146                )
147                .bindContextMenu(this.MenuBuilder, ResponseType.RightClick)
148              } else {
149                SwiperPage({
150                  appListInfo: $appListInfo,
151                  swiperPage: index.valueOf(),
152                  gridConfig: this.gridConfig,
153                  mPageDesktopViewModel: this.mPageDesktopViewModel
154                }).id(`SwiperPage_${item}${index}`)
155                  .bindContextMenu(this.MenuBuilder, ResponseType.LongPress)
156                  .bindContextMenu(this.MenuBuilder, ResponseType.RightClick)
157              }
158            }, (item: number, index: number) => {
159              return `${item}${index}`;
160            })
161          }
162          .id(`${TAG}_Swiper`)
163          .padding({
164            top: this.navigationBarStatusValue ?
165              this.mPageDesktopViewModel!.getPageDesktopStyleConfig().mDesktopMarginTop :
166              this.mPageDesktopViewModel!.getPageDesktopStyleConfig().mDesktopMarginTop + 0
167          })
168          .height(StyleConstants.PERCENTAGE_100)
169          .width(StyleConstants.PERCENTAGE_100)
170          .indicator(this.desktopIndicator)
171          .loop(false)
172          .index(this.pageIndex)
173          .onChange((index) => {
174            if (typeof index !== 'undefined' && this.pageIndex !== index) {
175              this.pageIndex = index;
176              Log.showInfo(TAG, `swiper change to page ${index}`);
177              localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_FORM_ITEM_VISIBLE, null);
178            }
179          })
180          .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
181            this.desktopEventResponse = false;
182          })
183          .onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
184            this.desktopEventResponse = false;
185          })
186          .onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
187            this.desktopEventResponse = true;
188            if (typeof index !== 'undefined') {
189              this.pageIndex = index;
190              Log.showInfo(TAG, `swiper animation end to page ${index}`);
191              localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_FORM_ITEM_VISIBLE, null);
192            }
193          })
194          .hitTestBehavior(this.desktopEventResponse ? HitTestMode.Default : HitTestMode.Block)
195        }
196    }
197    .id(`${TAG}`)
198    .alignItems(HorizontalAlign.Center)
199    .justifyContent(FlexAlign.Center)
200    .height(StyleConstants.PERCENTAGE_100)
201    .width(StyleConstants.PERCENTAGE_100)
202  }
203}