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 HashMap from '@ohos.util.HashMap'; 17import emitter from '@ohos.events.emitter'; 18import { EmptyPage, logger } from '@ohos/common'; 19import { KeyValueItemView } from '../components/KeyValueItemView'; 20import { KeyValuePair } from '../model/KeyValuePair'; 21import { HashMapDataSource } from '../components/hashmapcomponents/HashMapDataSource'; 22import { Constant } from '../Constant'; 23 24const TAG = 'HashMapView'; 25 26@Component 27export struct HashMapView { 28 @State totalCount: number = 0; 29 private hashMap: HashMap<string, string> = new HashMap(); 30 private dataSource: HashMapDataSource = new HashMapDataSource(); 31 32 aboutToAppear() { 33 emitter.on({ eventId: Constant.EMITTER_ID_HASH_MAP }, (eventData) => { 34 let item: KeyValuePair = eventData.data as KeyValuePair; 35 this.dataSource.addData(item); 36 this.totalCount = this.dataSource.totalCount(); 37 this.hashMap.set(item.key, item.value); 38 }); 39 } 40 41 aboutToDisappear() { 42 emitter.off(Constant.EMITTER_ID_HASH_MAP); 43 } 44 45 build() { 46 Column() { 47 if (this.totalCount != 0) { 48 List({ space: 12 }) { 49 LazyForEach(this.dataSource, (item: KeyValuePair, index: number) => { 50 ListItem() { 51 KeyValueItemView({ 52 index: index, 53 keyValuePair: item, 54 deleteAction: () => { 55 logger.info(TAG, `item = ${JSON.stringify(item)}`) 56 this.dataSource.deleteData(item, index); 57 this.totalCount = this.dataSource.totalCount(); 58 this.hashMap.remove(item.key); 59 } 60 }) 61 } 62 .height(72) 63 .width('100%') 64 }, (item: KeyValuePair, index: number) => JSON.stringify(item) + index) 65 } 66 .width('100%') 67 .height('100%') 68 .padding({ top: 8, left: 12, right: 12 }) 69 } else { 70 EmptyPage() 71 } 72 } 73 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 74 } 75}