• 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 promptAction from '@ohos.promptAction';
18import { EmptyPage } from '@ohos/common/src/main/ets/components/EmptyPage';
19import { KeyValuePair } from '../model/KeyValuePair';
20import { PlainArrayDataSource } from '../components/plainarraycomponents/PlainArrayDataSource';
21import { KeyValueItemView } from '../components/KeyValueItemView';
22import { Constant } from '../Constant';
23
24const MIN_LENGTH = 0;
25const isNaturalNumber = (str: string): boolean => {
26  let regExp: RegExp = new RegExp('^\\d+$');
27  return regExp.test(str);
28};
29
30@Component
31export struct PlainArrayView {
32  private dataSource: PlainArrayDataSource = new PlainArrayDataSource();
33  @State totalCount: number = MIN_LENGTH;
34
35  aboutToAppear() {
36    emitter.on({ eventId: Constant.EMITTER_ID_PLAIN_ARRAY }, (eventData) => {
37      let data = eventData.data as KeyValuePair;
38      if (isNaturalNumber(data.key)) {
39        this.dataSource.addData(eventData.data as KeyValuePair);
40        this.totalCount = this.dataSource.totalCount();
41      } else {
42        promptAction.showToast({ message: $r('app.string.key_limit') });
43      }
44    });
45  }
46
47  aboutToDisappear() {
48    emitter.off(Constant.EMITTER_ID_PLAIN_ARRAY);
49  }
50
51  build() {
52    Column() {
53      if (this.totalCount > MIN_LENGTH) {
54        List() {
55          LazyForEach(this.dataSource, (item: KeyValuePair, index: number) => {
56            ListItem() {
57              KeyValueItemView({ index: index, keyValuePair: item, deleteAction: () => {
58                this.dataSource.deleteData(index);
59                this.totalCount = this.dataSource.totalCount();
60              } })
61            }
62            .height(72)
63            .width('100%')
64            .margin({ bottom: 20 })
65          }, (item: KeyValuePair, index: number) => JSON.stringify(item) + index)
66        }
67        .width('100%')
68        .layoutWeight(1)
69
70      } else {
71        EmptyPage()
72      }
73    }
74    .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
75  }
76}