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 isShow: boolean; 26 private isDataReload: boolean; 27 28 public totalCount(): number { 29 return this.contactList.length; 30 } 31 32 public getData(index: number): any { 33 if (ArrayUtil.isEmpty(this.contactList) || index >= this.contactList.length) { 34 HiLog.i(TAG, 'getData contactlist is empty'); 35 return null; 36 } else { 37 let contact: ContactVo = this.contactList[index]; 38 let preContact: ContactVo = this.contactList[index - 1]; 39 let showIndex: boolean = (index == 0 || !(contact.namePrefix == preContact.namePrefix)); 40 let showDivifer: boolean = false; 41 if (index < this.contactList.length - 1) { 42 let nextContact: ContactVo = this.contactList[index + 1]; 43 showDivifer = (contact.namePrefix == nextContact.namePrefix); 44 } else { 45 showDivifer = false; 46 } 47 contact.title = (StringUtil.isEmpty(contact.showName) ? contact.phoneNum : contact.showName); 48 let subtitleConcat: string = (!StringUtil.isEmpty(contact.company) && 49 !StringUtil.isEmpty(contact.position)) ? ' | ' : ''; 50 contact.subTitle = (StringUtil.isEmpty(contact.company) ? '' : 51 contact.company).concat(subtitleConcat).concat(StringUtil.isEmpty(contact.position) ? '' : contact.position); 52 return { 53 showIndex: showIndex, 54 showDivifer: showDivifer, 55 contact: contact 56 }; 57 } 58 } 59 60 public refreshAll(contactList: ContactVo[]) { 61 HiLog.i(TAG, ' refreshAll!'); 62 this.contactList = contactList; 63 this.setDataReload(true); 64 } 65 66 public setIsShow(isShow: boolean) { 67 if (this.isShow == isShow) { 68 return; 69 } 70 HiLog.i(TAG, ' setIsShow:' + isShow); 71 this.isShow = isShow; 72 this.setDataReload(this.isDataReload) 73 } 74 75 public refresh(start: number, count: number, contactList: ContactVo[]) { 76 HiLog.i(TAG, ' refresh!'); 77 this.contactList.splice(start, count, ...contactList); 78 this.setDataReload(true); 79 } 80 81 public remove(index: number, count?) { 82 if (index < 0 || index >= this.totalCount()) { 83 return; 84 } 85 HiLog.i(TAG, ' remove:' + index); 86 this.contactList.splice(index, count ? count : 1); 87 this.setDataReload(true); 88 } 89 90 private setDataReload(isDataReload: boolean) { 91 if (this.isShow) { 92 if (isDataReload) { 93 HiLog.i(TAG, 'notifyDataReload'); 94 this.notifyDataReload(); 95 } 96 this.isDataReload = false; 97 } else { 98 this.isDataReload = isDataReload; 99 } 100 } 101}