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 BitSet from './BitSet'; 16 17export default class GridLayoutUtil { 18 /** 19 * update GridLayoutInfo 20 * 21 * @param newLayoutRows new layout rows 22 * @param newLayoutColumns new layout columns 23 * 24 * @return new GridLayoutInfo 25 */ 26 static updateGridLayoutInfo(gridLayoutInfo: any, newLayoutRows: number, newLayoutColumns: number): any { 27 gridLayoutInfo.layoutDescription.pageCount = GridLayoutUtil.updateLayoutInfo( 28 gridLayoutInfo.layoutInfo, newLayoutRows, newLayoutColumns); 29 gridLayoutInfo.layoutDescription.row = newLayoutRows; 30 gridLayoutInfo.layoutDescription.column = newLayoutColumns; 31 return gridLayoutInfo; 32 } 33 34 /** 35 * update layoutInfo 36 * 37 * @param layoutInfo appLayoutInfo List 38 * @param newLayoutRows new layout rows 39 * @param newLayoutColumns new layout columns 40 * 41 * @return new layout pages num 42 */ 43 private static updateLayoutInfo(layoutInfo: any, newLayoutRows: number, newLayoutColumns: number): number { 44 let currentPage = -1; 45 let insertPages = 0; 46 const currentPageBitset = new BitSet(newLayoutRows * newLayoutColumns); 47 48 for (let i = 0; i < layoutInfo.length;) { 49 if (currentPage != layoutInfo[i].page) { 50 currentPage = layoutInfo[i].page; 51 currentPageBitset.clear(); 52 } 53 54 while (i < layoutInfo.length && layoutInfo[i].page == currentPage) { 55 if (GridLayoutUtil.updatePositionSuccess(layoutInfo[i], currentPageBitset, newLayoutRows, newLayoutColumns, currentPage + insertPages)) { 56 i++; 57 } else { 58 currentPageBitset.clear(); 59 insertPages++; 60 } 61 } 62 } 63 64 return currentPage + insertPages + 1; 65 } 66 67 /** 68 * update app position info 69 * 70 * @param appLayout appLayoutInfo List Item 71 * @param currentPageBitset current page bitset 72 * @param layoutRows new layout rows 73 * @param layoutColumns new layout columns 74 * @param currentPage app pages index 75 * 76 * @return whether update success 77 */ 78 private static updatePositionSuccess(appLayout: any, currentPageBitset: BitSet, 79 layoutRows: number, layoutColumns: number, currentPage: number): boolean { 80 for (let r = 0; r < layoutRows; r++) { 81 for (let c = 0; c < layoutColumns; c++) { 82 if (!currentPageBitset.get(r * layoutColumns + c)) { 83 appLayout.page = currentPage; 84 appLayout.row = r; 85 appLayout.column = c; 86 currentPageBitset.set(r * layoutColumns + c); 87 return true; 88 } 89 } 90 } 91 return false; 92 } 93}