• 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, logger } from '@ohos/common';
18import { Information } from '../model/Information'
19import { InformationItemView } from '../components/InformationItemView';
20import { DequeDataSource } from '../components/dequecomponents/DequeDataSource';
21import { Constant } from "../Constant";
22
23const TAG = 'DequeView';
24
25@Component
26export struct DequeView {
27  @State totalCount: number = 0;
28  private dataSource: DequeDataSource = new DequeDataSource();
29
30  aboutToAppear() {
31    emitter.on({ eventId: Constant.EMITTER_ID_DEQUE }, (eventData) => {
32      let item = eventData.data as Information;
33      this.dataSource.insertFront(item);
34      this.totalCount = this.dataSource.totalCount();
35    });
36  }
37
38  aboutToDisappear() {
39    emitter.off(Constant.EMITTER_ID_DEQUE);
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.deleteFront();
55                    this.totalCount = this.dataSource.totalCount();
56                  } else if (index === this.totalCount - 1) {
57                    this.dataSource.deleteEnd();
58                    this.totalCount = this.dataSource.totalCount();
59                  }
60                }
61              })
62            }
63            .height(72)
64            .width('100%')
65            .margin({ bottom: 20 })
66          }, (item: Information, index: number) => JSON.stringify(item) + index)
67        }
68        .width('100%')
69        .height('100%')
70        .padding({ top: 8, left: 12, right: 12 })
71      } else {
72        EmptyPage()
73      }
74    }
75    .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
76  }
77}