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