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 } from '@ohos/common/src/main/ets/components/EmptyPage'; 18import { Information } from '../model/Information'; 19import { StackDataSource } from '../components/stackcomponents/StackDataSource'; 20import { InformationItemView } from '../components/InformationItemView'; 21import { Constant } from '../Constant'; 22 23const MIN_LENGTH = 0; 24 25@Component 26export struct StackView { 27 private dataSource: StackDataSource = new StackDataSource(); 28 @State totalCount: number = MIN_LENGTH; 29 30 aboutToAppear() { 31 emitter.on({ eventId: Constant.EMITTER_ID_STACK }, (eventData) => { 32 this.dataSource.addData(eventData.data as Information); 33 this.totalCount = this.dataSource.totalCount(); 34 }); 35 } 36 37 aboutToDisappear() { 38 emitter.off(Constant.EMITTER_ID_STACK); 39 } 40 41 build() { 42 Column() { 43 if (this.totalCount > MIN_LENGTH) { 44 List() { 45 LazyForEach(this.dataSource, (item: Information, index: number) => { 46 ListItem() { 47 InformationItemView({ 48 index: index, 49 information: item, 50 deleteAction: () => { 51 this.dataSource.deleteData(); 52 this.totalCount = this.dataSource.totalCount(); 53 } 54 }) 55 } 56 .height(72) 57 .width('100%') 58 .margin({ bottom: 20 }) 59 }, (item: Information, index: number) => JSON.stringify(item) + index) 60 } 61 .width('100%') 62 .layoutWeight(1) 63 64 } else { 65 EmptyPage() 66 } 67 } 68 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 69 } 70}