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 { SearchContactsBean } from '../bean/SearchContactsBean';
16import { FavoriteListBean } from '../bean/FavoriteListBean';
17import BasicDataSource from './BasicDataSource';
18import { HiLog } from '../../../../../../common/src/main/ets/util/HiLog';
19import { ArrayUtil } from '../../../../../../common/src/main/ets/util/ArrayUtil';
20
21const TAG = 'SearchContactsSource ';
22
23export default class SearchContactsSource extends BasicDataSource {
24  private contactList: SearchContactsBean[] = [];
25  public contactObj: { [key: string]: SearchContactsBean[] } = {};
26  public contactIndexObj: { [key: string]: number } = {};
27
28  public totalCount(): number {
29    return this.contactList.length;
30  }
31
32  public getData(index: number): FavoriteListBean {
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: SearchContactsBean = this.contactList[index];
38      let preContact: SearchContactsBean = this.contactList[index - 1];
39      let showIndex: boolean = (index === 0 || !(contact.sortFirstLetter === preContact.sortFirstLetter));
40      let showDivider: boolean = false;
41      if (index < this.contactList.length - 1) {
42        let nextContact: SearchContactsBean = this.contactList[index + 1];
43        showDivider = (contact.sortFirstLetter === nextContact.sortFirstLetter);
44      } else {
45        showDivider = false;
46      }
47      return new FavoriteListBean(index, showIndex, showDivider, null, contact);
48    }
49  }
50
51  public refresh(contactList: SearchContactsBean[]): void {
52    HiLog.i(TAG, ' refresh!');
53    this.contactList = contactList;
54    this.notifyDataReload();
55  }
56}