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