1/** 2 * Copyright (c) 2023-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 ArrayList from '@ohos.util.ArrayList'; 17import { CardItemInfo } from '@ohos/common'; 18import RecommendCardsDataPool from '../datapool/RecommendCardsDataPool'; 19 20const TAG = 'BaseDataSource'; 21 22/** 23 * 推荐卡片资源池数据源:基础数据源 24 */ 25export default abstract class BaseDataSource { 26 public mRecommendCardsDataPool: RecommendCardsDataPool = RecommendCardsDataPool.getInstance(); 27 28 /** 29 * 获取卡片资源池的数据列表 30 * 31 * @return 卡片资源池的数据列表 32 */ 33 public getDataList(): ArrayList<CardItemInfo> { 34 // 1、获取所有卡片map 35 let allDataMap: Map<string, CardItemInfo> = this.getAllDataMap(); 36 37 // 2、获取数据源数据 38 let sourceDataList: string[] = this.getSourceDataList(); 39 40 // 3、获取阈值内的数据 41 return this.getThresholdDataList(sourceDataList, allDataMap); 42 } 43 44 /** 45 * 获取卡片资源池阈值 46 * 47 * @return 卡片资源池阈值 48 */ 49 public abstract getThreshold(): number; 50 51 /** 52 * 获取卡片资源池数据源名 53 * 54 * @return 卡片资源池数据源名 55 */ 56 public abstract getName(): string; 57 58 /** 59 * 获取所有卡片map 60 * 61 * @return 所有卡片map 62 */ 63 private getAllDataMap(): Map<string, CardItemInfo> { 64 return this.mRecommendCardsDataPool.getAllDataMap(); 65 } 66 67 /** 68 * 获取阈值内的数据 69 * 70 * @param dataSourceList 数据源的数据 71 * @param allDataMap 所有数据集合 72 * @returns 阈值内的数据 73 */ 74 protected getThresholdDataList(dataSourceList: string[], 75 allDataMap: Map<string, CardItemInfo>): ArrayList<CardItemInfo> { 76 let dataList: ArrayList<CardItemInfo> = new ArrayList(); 77 for (let i: number = 0; i < dataSourceList.length; i++) { 78 if (dataSourceList.length === this.getThreshold()) { 79 break; 80 } 81 let cardItemBundleName = dataSourceList[i]; 82 if (!allDataMap.has(cardItemBundleName)) { 83 continue; 84 } 85 let cardItemInfo: CardItemInfo = allDataMap.get(cardItemBundleName); 86 dataList.add(cardItemInfo); 87 } 88 return dataList; 89 } 90 91 /** 92 * 获取数据源的数据 93 * 94 * @returns 数据源的数据 95 */ 96 protected abstract getSourceDataList(): string[]; 97} 98