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