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