18779efd5Sopenharmony_ci/**
28779efd5Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License.
58779efd5Sopenharmony_ci * You may obtain a copy of the License at
68779efd5Sopenharmony_ci *
78779efd5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88779efd5Sopenharmony_ci *
98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and
138779efd5Sopenharmony_ci * limitations under the License.
148779efd5Sopenharmony_ci */
158779efd5Sopenharmony_ciimport { SearchContactsBean } from '../bean/SearchContactsBean';
168779efd5Sopenharmony_ciimport { FavoriteListBean } from '../bean/FavoriteListBean';
178779efd5Sopenharmony_ciimport BasicDataSource from './BasicDataSource';
188779efd5Sopenharmony_ciimport { HiLog } from '../../../../../../common/src/main/ets/util/HiLog';
198779efd5Sopenharmony_ciimport { ArrayUtil } from '../../../../../../common/src/main/ets/util/ArrayUtil';
208779efd5Sopenharmony_ci
218779efd5Sopenharmony_ciconst TAG = 'SearchContactsSource ';
228779efd5Sopenharmony_ci
238779efd5Sopenharmony_ciexport default class SearchContactsSource extends BasicDataSource {
248779efd5Sopenharmony_ci  private contactList: SearchContactsBean[] = [];
258779efd5Sopenharmony_ci  public contactObj: { [key: string]: SearchContactsBean[] } = {};
268779efd5Sopenharmony_ci  public contactIndexObj: { [key: string]: number } = {};
278779efd5Sopenharmony_ci
288779efd5Sopenharmony_ci  public totalCount(): number {
298779efd5Sopenharmony_ci    return this.contactList.length;
308779efd5Sopenharmony_ci  }
318779efd5Sopenharmony_ci
328779efd5Sopenharmony_ci  public getData(index: number): FavoriteListBean {
338779efd5Sopenharmony_ci    if (ArrayUtil.isEmpty(this.contactList) || index >= this.contactList.length) {
348779efd5Sopenharmony_ci      HiLog.i(TAG, 'getData contactlist is empty');
358779efd5Sopenharmony_ci      return null;
368779efd5Sopenharmony_ci    } else {
378779efd5Sopenharmony_ci      let contact: SearchContactsBean = this.contactList[index];
388779efd5Sopenharmony_ci      let preContact: SearchContactsBean = this.contactList[index - 1];
398779efd5Sopenharmony_ci      let showIndex: boolean = (index === 0 || !(contact.sortFirstLetter === preContact.sortFirstLetter));
408779efd5Sopenharmony_ci      let showDivider: boolean = false;
418779efd5Sopenharmony_ci      if (index < this.contactList.length - 1) {
428779efd5Sopenharmony_ci        let nextContact: SearchContactsBean = this.contactList[index + 1];
438779efd5Sopenharmony_ci        showDivider = (contact.sortFirstLetter === nextContact.sortFirstLetter);
448779efd5Sopenharmony_ci      } else {
458779efd5Sopenharmony_ci        showDivider = false;
468779efd5Sopenharmony_ci      }
478779efd5Sopenharmony_ci      return new FavoriteListBean(index, showIndex, showDivider, null, contact);
488779efd5Sopenharmony_ci    }
498779efd5Sopenharmony_ci  }
508779efd5Sopenharmony_ci
518779efd5Sopenharmony_ci  public refresh(contactList: SearchContactsBean[]): void {
528779efd5Sopenharmony_ci    HiLog.i(TAG, ' refresh!');
538779efd5Sopenharmony_ci    this.contactList = contactList;
548779efd5Sopenharmony_ci    this.notifyDataReload();
558779efd5Sopenharmony_ci  }
568779efd5Sopenharmony_ci}