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