1/** 2 * Copyright (c) 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 */ 15import BasicDataSource from './BasicDataSource'; 16import type { FavoriteBean } from '../bean/FavoriteBean'; 17import { FavoriteListBean } from '../bean/FavoriteListBean'; 18import { HiLog } from '../../../../../../common/src/main/ets/util/HiLog'; 19import { ArrayUtil } from '../../../../../../common/src/main/ets/util/ArrayUtil'; 20 21const TAG = 'FavoriteDataSource '; 22 23export default class FavoriteDataSource extends BasicDataSource { 24 private favoriteList: FavoriteBean[] = []; 25 26 public totalCount(): number { 27 return this.favoriteList.length; 28 } 29 30 public getFavoriteList(): FavoriteBean[] { 31 return this.favoriteList; 32 } 33 34 public getData(index: number): FavoriteListBean { 35 if (ArrayUtil.isEmpty(this.favoriteList) || index >= this.favoriteList.length) { 36 HiLog.i(TAG, 'getData contactlist is empty'); 37 return null; 38 } else { 39 let favorite: FavoriteBean = this.favoriteList[index]; 40 let preContact: FavoriteBean = this.favoriteList[index - 1]; 41 let addContact: FavoriteBean = this.favoriteList[index + 1]; 42 let showIndex: boolean = (index === 0 || !(favorite.namePrefix === preContact.namePrefix)); 43 let showDivider: boolean = false; 44 if (index < this.favoriteList.length - 1) { 45 showDivider = !addContact.isUsuallyShow; 46 } else { 47 showDivider = false; 48 } 49 return new FavoriteListBean(index, showIndex, showDivider, favorite, null); 50 } 51 } 52 53 public refresh(favoriteList: FavoriteBean[]): void { 54 HiLog.i(TAG, 'refresh!'); 55 this.favoriteList = favoriteList; 56 this.notifyDataReload(); 57 } 58}