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 */ 15 16import {ContactVo, NameVo } from '../../..//model/bean/ContactVo'; 17import { ArrayUtil } from '../../../../../../../common/src/main/ets/util/ArrayUtil'; 18import { HiLog } from '../../../../../../../common/src/main/ets/util/HiLog'; 19 20const TAG = 'AlphabetIndexerPresenter'; 21const CHINESE_CHAR_CODE = 255; 22 23export default class AlphabetIndexerPresenter { 24 private static sInstance: AlphabetIndexerPresenter; 25 alphabetIndexList: string[] = ['#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; 26 private alphabetObj: {[key: string] : NameVo[]} = {}; 27 private alphabetIndexObj: {[key: string] : number} = {}; 28 private contactList: ContactVo[] = []; 29 30 public static getInstance(): AlphabetIndexerPresenter { 31 if (AlphabetIndexerPresenter.sInstance == null) { 32 HiLog.i(TAG, 'alphabetIndexer getInstance!'); 33 AlphabetIndexerPresenter.sInstance = new AlphabetIndexerPresenter(); 34 } 35 return AlphabetIndexerPresenter.sInstance; 36 } 37 38 initContactList(list: ContactVo[]) { 39 this.contactList = list.slice(); 40 this.getAlphabetIndexData(); 41 } 42 43 getAlphabetIndexData() { 44 if (ArrayUtil.isEmpty(this.contactList)) { 45 return null; 46 } 47 // Get the position of the index in the list 48 // Get index data 49 this.contactList.forEach((item, index) => { 50 let preContact: ContactVo = null; 51 if (index > 0) { 52 preContact = this.contactList[index - 1]; 53 } 54 if (index == 0 || !(item.namePrefix == preContact.namePrefix)) { 55 this.alphabetObj[item.namePrefix] = []; 56 this.alphabetIndexObj[item.namePrefix] = this.contactList.indexOf(item); 57 } 58 let nameVo = new NameVo(item.emptyNameData, item.namePrefix, item.nameSuffix); 59 this.alphabetObj[item.namePrefix].push(nameVo); 60 }) 61 } 62 63 getAlphabetPopData(index: number): string[] { 64 let popData: string[] = []; 65 if (this.alphabetIndexList.length <= index) { 66 return popData; 67 } 68 69 // Get Index Popup Data 70 let selected = this.alphabetIndexList[index]; 71 let list = this.alphabetObj[selected]; 72 if (list && list.length !== 0) { 73 list.forEach(item => { 74 if(item.nameSuffix.charCodeAt(0) > CHINESE_CHAR_CODE) { 75 if (popData.length === 0) { 76 popData.push(item.nameSuffix); 77 } else { 78 let hasIndex = popData.indexOf(item.nameSuffix); 79 if (hasIndex === -1) { 80 popData.push(item.nameSuffix); 81 } 82 } 83 } 84 }); 85 } 86 return popData; 87 } 88 89 getListScrollIndex(selectedAlphabetIndex: number, popDataSource?: string[], popIndex?: number): number { 90 // get list scroll index 91 let selected = this.alphabetIndexList[selectedAlphabetIndex]; 92 let alphabetIndex = this.alphabetIndexObj[selected]; 93 let scrollIndex: number = alphabetIndex; 94 if (popIndex >= 0) { 95 let alphabetContacts = this.alphabetObj[selected]; 96 if (alphabetContacts) { 97 let popData = popDataSource[popIndex]; 98 for (let index = 0; index < alphabetContacts.length; index++) { 99 const element = alphabetContacts[index]; 100 if (element.nameSuffix === popData) { 101 scrollIndex = scrollIndex + index; 102 break; 103 } 104 } 105 } 106 } 107 return scrollIndex; 108 } 109 110 getAlphabetSelected(scrollIndex: number): number { 111 let selected = 0; 112 if (this.contactList.length > scrollIndex) { 113 let obj = this.contactList[scrollIndex]; 114 let namePrefix = obj.namePrefix; 115 selected = this.alphabetIndexList.indexOf(namePrefix); 116 } 117 return selected; 118 } 119}