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