• 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 router from '@ohos.router';
17import emitter from '@ohos.events.emitter';
18import { Constant, KeyValuePair } from '@ohos/capabilities';
19import { getString } from '@ohos/common/src/main/ets/util/ResourceUtil';
20
21@Entry
22@Component
23export struct AddKeyValueView {
24  @State keyValuePair: KeyValuePair = new KeyValuePair('', '')
25  @State title: string = '';
26  @State flag: string = '';
27
28  aboutToAppear() {
29    let tem: string = (router.getParams() as Record<string, Object>)['title'] as string;
30    if (!tem) {
31      return;
32    }
33    this.flag = tem;
34    let matchArr: RegExpMatchArray | null = tem.match(new RegExp('.*(?=\\(|()'));
35    if (matchArr !== null) {
36      this.title = `${getString($r('app.string.add'))}${matchArr[0]}`;
37    }
38  }
39
40  build() {
41    Column() {
42      Row() {
43        Row() {
44          Image($r("app.media.ic_public_back"))
45            .height(24)
46            .aspectRatio(1)
47            .objectFit(ImageFit.Contain)
48        }
49        .height('100%')
50        .aspectRatio(1)
51        .padding({ left: 24 })
52        .onClick(() => {
53          router.back();
54        })
55
56        Text(this.title)
57          .fontColor($r('app.color.text_color_primary'))
58          .fontSize(20)
59          .margin({ left: 24 })
60
61        Blank()
62
63        Row() {
64          Image($r("app.media.ic_public_confirm"))
65            .height(24)
66            .aspectRatio(1)
67            .objectFit(ImageFit.Contain)
68        }
69        .id('add_confirm')
70        .height('100%')
71        .aspectRatio(1)
72        .padding({ right: 24 })
73        .onClick(() => {
74          let eventId = Constant.EMITTER_ID_DEFAULT;
75          switch (this.flag) {
76            case getString($r('app.string.hash_map')):
77              eventId = Constant.EMITTER_ID_HASH_MAP;
78              break;
79            case getString($r('app.string.light_weight_map')):
80              eventId = Constant.EMITTER_ID_LIGHT_WEIGHT_MAP;
81              break;
82            case getString($r('app.string.plain_array')):
83              eventId = Constant.EMITTER_ID_PLAIN_ARRAY;
84              break;
85            case getString($r('app.string.tree_map')):
86              eventId = Constant.EMITTER_ID_TREE_MAP;
87              break;
88            default:
89              eventId = Constant.EMITTER_ID_DEFAULT;
90              break;
91          }
92
93          let event: emitter.InnerEvent = {
94            eventId: eventId,
95            priority: emitter.EventPriority.HIGH
96          };
97
98          let eventData: emitter.EventData = {
99            data: {
100              'key': this.keyValuePair.key,
101              'value': this.keyValuePair.value
102            }
103          };
104
105          emitter.emit(event, eventData);
106          router.back();
107        })
108      }
109      .width('100%')
110      .height(56)
111
112      Row() {
113        Text('Key')
114          .width(44)
115          .margin({ left: 19 })
116          .fontSize(16)
117          .fontColor($r('app.color.text_color_primary'))
118        Column() {
119          TextInput({ placeholder: $r('app.string.input_key') })
120            .id('add_key')
121            .height(48)
122            .fontSize(16)
123            .backgroundColor($r('sys.color.ohos_id_color_background_transparent'))
124            .onChange((value: string) => {
125              this.keyValuePair.key = value;
126            })
127        }
128        .layoutWeight(1)
129        .margin({ left: 52, right: 16 })
130      }
131      .height(64)
132      .width('100%')
133      .borderRadius(16)
134      .backgroundColor($r('app.color.bg_white'))
135      .margin({ top: 12 })
136
137      Row() {
138        Text('Value')
139          .margin({ left: 19 })
140          .fontSize(16)
141          .fontColor($r('app.color.text_color_primary'))
142        Column() {
143          TextInput({ placeholder: $r('app.string.input_value') })
144            .id('add_value')
145            .height(48)
146            .fontSize(16)
147            .backgroundColor($r('sys.color.ohos_id_color_background_transparent'))
148            .onChange((value: string) => {
149              this.keyValuePair.value = value;
150            })
151        }
152        .layoutWeight(1)
153        .margin({ left: 52, right: 16 })
154      }
155      .height(64)
156      .width('100%')
157      .borderRadius(16)
158      .backgroundColor($r('app.color.bg_white'))
159      .margin({ top: 12 })
160    }
161    .height('100%')
162    .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
163    .padding({ left: 12, right: 12 })
164  }
165}