• 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 LightWeightSet from '@ohos.util.LightWeightSet';
17import emitter from '@ohos.events.emitter';
18import { EmptyPage, logger } from '@ohos/common';
19import { ValueItemView } from '../components/ValueItemView';
20import { LightWeightSetDataSource } from '../components/lightweightsetcomponents/LightWeightSetDataSource';
21import { Constant } from '../Constant';
22
23const TAG = 'LightWeightSetView';
24
25@Component
26export struct LightWeightSetView {
27  @State totalCount: number = 0;
28  @State lightWeightSet: LightWeightSet<string> = new LightWeightSet();
29  private dataSource: LightWeightSetDataSource = new LightWeightSetDataSource();
30
31  aboutToAppear() {
32    emitter.on({ eventId: Constant.EMITTER_ID_LIGHT_WEIGHT_SET }, (eventData: emitter.EventData) => {
33      if (eventData.data === undefined) {
34        return;
35      }
36      let item: string = eventData.data.value;
37      this.dataSource.addData(item);
38      this.lightWeightSet.add(item);
39      this.totalCount = this.dataSource.totalCount();
40    });
41  }
42
43  aboutToDisappear() {
44    emitter.off(Constant.EMITTER_ID_LIGHT_WEIGHT_SET);
45  }
46
47  build() {
48    Column() {
49      if (this.totalCount != 0) {
50        List({ space: 12 }) {
51          LazyForEach(this.dataSource, (item: string, index: number) => {
52            ListItem() {
53              ValueItemView({
54                index: index,
55                value: item,
56                deleteAction: () => {
57                  logger.info(TAG, `item = ${JSON.stringify(item)}`)
58                  this.dataSource.deleteData(item, index);
59                  this.totalCount = this.dataSource.totalCount();
60                  this.lightWeightSet.remove(item);
61                }
62              })
63            }
64            .height(72)
65            .width('100%')
66          }, (item: string, 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}