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