16e80583aSopenharmony_ci/**
26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46e80583aSopenharmony_ci * you may not use this file except in compliance with the License.
56e80583aSopenharmony_ci * You may obtain a copy of the License at
66e80583aSopenharmony_ci *
76e80583aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86e80583aSopenharmony_ci *
96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e80583aSopenharmony_ci * See the License for the specific language governing permissions and
136e80583aSopenharmony_ci * limitations under the License.
146e80583aSopenharmony_ci */
156e80583aSopenharmony_ci
166e80583aSopenharmony_ciimport { GridLayoutInfo } from '../interface';
176e80583aSopenharmony_ciimport BitSet from './BitSet';
186e80583aSopenharmony_ci
196e80583aSopenharmony_ciexport default class GridLayoutUtil {
206e80583aSopenharmony_ci  /**
216e80583aSopenharmony_ci   * update GridLayoutInfo
226e80583aSopenharmony_ci   *
236e80583aSopenharmony_ci   * @param newLayoutRows new layout rows
246e80583aSopenharmony_ci   * @param newLayoutColumns new layout columns
256e80583aSopenharmony_ci   *
266e80583aSopenharmony_ci   * @return new GridLayoutInfo
276e80583aSopenharmony_ci   */
286e80583aSopenharmony_ci  static updateGridLayoutInfo(gridLayoutInfo: GridLayoutInfo, newLayoutRows: number,
296e80583aSopenharmony_ci                              newLayoutColumns: number): GridLayoutInfo {
306e80583aSopenharmony_ci    gridLayoutInfo.layoutDescription.pageCount = GridLayoutUtil.updateLayoutInfo(
316e80583aSopenharmony_ci      gridLayoutInfo.layoutInfo, newLayoutRows, newLayoutColumns);
326e80583aSopenharmony_ci    gridLayoutInfo.layoutDescription.row = newLayoutRows;
336e80583aSopenharmony_ci    gridLayoutInfo.layoutDescription.column = newLayoutColumns;
346e80583aSopenharmony_ci    return gridLayoutInfo;
356e80583aSopenharmony_ci  }
366e80583aSopenharmony_ci
376e80583aSopenharmony_ci  /**
386e80583aSopenharmony_ci   * update layoutInfo
396e80583aSopenharmony_ci   *
406e80583aSopenharmony_ci   * @param layoutInfo appLayoutInfo List
416e80583aSopenharmony_ci   * @param newLayoutRows new layout rows
426e80583aSopenharmony_ci   * @param newLayoutColumns new layout columns
436e80583aSopenharmony_ci   *
446e80583aSopenharmony_ci   * @return new layout pages num
456e80583aSopenharmony_ci   */
466e80583aSopenharmony_ci  private static updateLayoutInfo(layoutInfo: any, newLayoutRows: number, newLayoutColumns: number): number {
476e80583aSopenharmony_ci    let currentPage = -1;
486e80583aSopenharmony_ci    let insertPages = 0;
496e80583aSopenharmony_ci    const currentPageBitset = new BitSet(newLayoutRows * newLayoutColumns);
506e80583aSopenharmony_ci
516e80583aSopenharmony_ci    for (let i = 0; i < layoutInfo.length;) {
526e80583aSopenharmony_ci      if (currentPage != layoutInfo[i].page) {
536e80583aSopenharmony_ci        currentPage = layoutInfo[i].page;
546e80583aSopenharmony_ci        currentPageBitset.clear();
556e80583aSopenharmony_ci      }
566e80583aSopenharmony_ci
576e80583aSopenharmony_ci      while (i < layoutInfo.length && layoutInfo[i].page == currentPage) {
586e80583aSopenharmony_ci        if (GridLayoutUtil.updatePositionSuccess(layoutInfo[i], currentPageBitset, newLayoutRows, newLayoutColumns, currentPage + insertPages)) {
596e80583aSopenharmony_ci          i++;
606e80583aSopenharmony_ci        } else {
616e80583aSopenharmony_ci          currentPageBitset.clear();
626e80583aSopenharmony_ci          insertPages++;
636e80583aSopenharmony_ci        }
646e80583aSopenharmony_ci      }
656e80583aSopenharmony_ci    }
666e80583aSopenharmony_ci
676e80583aSopenharmony_ci    return currentPage + insertPages + 1;
686e80583aSopenharmony_ci  }
696e80583aSopenharmony_ci
706e80583aSopenharmony_ci  /**
716e80583aSopenharmony_ci   * update app position info
726e80583aSopenharmony_ci   *
736e80583aSopenharmony_ci   * @param appLayout appLayoutInfo List Item
746e80583aSopenharmony_ci   * @param currentPageBitset current page bitset
756e80583aSopenharmony_ci   * @param layoutRows new layout rows
766e80583aSopenharmony_ci   * @param layoutColumns new layout columns
776e80583aSopenharmony_ci   * @param currentPage app pages index
786e80583aSopenharmony_ci   *
796e80583aSopenharmony_ci   * @return whether update success
806e80583aSopenharmony_ci   */
816e80583aSopenharmony_ci  private static updatePositionSuccess(appLayout: any, currentPageBitset: BitSet,
826e80583aSopenharmony_ci    layoutRows: number, layoutColumns: number, currentPage: number): boolean {
836e80583aSopenharmony_ci    for (let r = 0; r < layoutRows; r++) {
846e80583aSopenharmony_ci      for (let c = 0; c < layoutColumns; c++) {
856e80583aSopenharmony_ci        if (!currentPageBitset.get(r * layoutColumns + c)) {
866e80583aSopenharmony_ci          appLayout.page = currentPage;
876e80583aSopenharmony_ci          appLayout.row = r;
886e80583aSopenharmony_ci          appLayout.column = c;
896e80583aSopenharmony_ci          currentPageBitset.set(r * layoutColumns + c);
906e80583aSopenharmony_ci          return true;
916e80583aSopenharmony_ci        }
926e80583aSopenharmony_ci      }
936e80583aSopenharmony_ci    }
946e80583aSopenharmony_ci    return false;
956e80583aSopenharmony_ci  }
966e80583aSopenharmony_ci}