1/** 2 * Copyright (c) 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 BasicDataSource from './BasicDataSource'; 16import { ContactVo } from '../bean/ContactVo'; 17import { HiLog } from '../../../../../../common/src/main/ets/util/HiLog'; 18import { ArrayUtil } from '../../../../../../common/src/main/ets/util/ArrayUtil'; 19import { StringUtil } from '../../../../../../common/src/main/ets/util/StringUtil'; 20 21const TAG = "ContactListDataSource"; 22 23export default class ContactListDataSource extends BasicDataSource { 24 private contactList: ContactVo[] = []; 25 private contactsCount: number = 0; 26 27 public totalCount(): number { 28 return this.contactList.length; 29 } 30 31 public getData(index: number): any { 32 if (ArrayUtil.isEmpty(this.contactList) || index >= this.contactList.length) { 33 HiLog.i(TAG, "getData contactlist is empty"); 34 return null; 35 } else { 36 let contact: ContactVo = this.contactList[index]; 37 let preContact: ContactVo = this.contactList[index - 1]; 38 let showIndex: boolean = (index == 0 || !(contact.namePrefix == preContact.namePrefix)); 39 let showDivifer: boolean = false; 40 if (index < this.contactList.length - 1) { 41 let nextContact: ContactVo = this.contactList[index + 1]; 42 showDivifer = (contact.namePrefix == nextContact.namePrefix); 43 } else { 44 showDivifer = false; 45 } 46 contact.title = (StringUtil.isEmpty(contact.showName) ? contact.phoneNum : contact.showName); 47 let subtitleConcat: string = (!StringUtil.isEmpty(contact.company) && 48 !StringUtil.isEmpty(contact.position)) ? ' | ' : ""; 49 contact.subTitle = (StringUtil.isEmpty(contact.company) ? '' : 50 contact.company).concat(subtitleConcat).concat(StringUtil.isEmpty(contact.position) ? '' : contact.position); 51 return { 52 showIndex: showIndex, 53 showDivifer: showDivifer, 54 contact: contact 55 }; 56 } 57 } 58 59 public refresh(contactList: ContactVo[]) { 60 HiLog.i(TAG, ' refresh!'); 61 this.contactList = contactList; 62 this.notifyDataReload(); 63 } 64}