1/* 2* Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 TreeMap from '@ohos.util.TreeMap'; 17import ArrayList from '@ohos.util.ArrayList'; 18import { BaseDataSource } from '@ohos/common/src/main/ets/components/BaseDataSource'; 19import { KeyValuePair } from '../../model/KeyValuePair'; 20 21export class TreeMapDataSource extends BaseDataSource { 22 private dataArr: TreeMap<string, string> = new TreeMap(); 23 private arr: ArrayList<string> = new ArrayList(); 24 25 public totalCount(): number { 26 return this.arr.length; 27 } 28 29 public getData(index: number): KeyValuePair { 30 let key: string = this.arr[index]; 31 let value: string = this.dataArr.get(key); 32 this.dataArr.get(this.arr[index]); 33 let keyValuePair: KeyValuePair = new KeyValuePair(`${this.arr[index]}`, value); 34 return keyValuePair; 35 } 36 37 public addData(keyValuePair: KeyValuePair): void { 38 if (!this.dataArr.hasKey(keyValuePair.key)) { 39 this.arr.add(keyValuePair.key) 40 } 41 this.dataArr.set(keyValuePair.key, keyValuePair.value); 42 this.notifyDataChange(this.dataArr.length - 1); 43 this.notifyDataReload(); 44 } 45 46 public deleteData(key: string): void { 47 this.dataArr.remove(key); 48 let index = this.arr.getIndexOf(key); 49 this.arr.remove(key); 50 this.notifyDataDelete(index); 51 this.notifyDataReload(); 52 } 53}