• 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';
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: this.keyValuePair
100          };
101
102          emitter.emit(event, eventData);
103          router.back();
104        })
105      }
106      .width('100%')
107      .height(56)
108
109      Row() {
110        Text('Key')
111          .width(44)
112          .margin({ left: 19 })
113          .fontSize(16)
114          .fontColor($r('app.color.text_color_primary'))
115        Column() {
116          TextInput({ placeholder: $r('app.string.input_key') })
117            .id('add_key')
118            .height(48)
119            .fontSize(16)
120            .backgroundColor($r('sys.color.ohos_id_color_background_transparent'))
121            .onChange((value: string) => {
122              this.keyValuePair.key = value;
123            })
124        }
125        .layoutWeight(1)
126        .margin({ left: 52, right: 16 })
127      }
128      .height(64)
129      .width('100%')
130      .borderRadius(16)
131      .backgroundColor($r('app.color.bg_white'))
132      .margin({ top: 12 })
133
134      Row() {
135        Text('Value')
136          .margin({ left: 19 })
137          .fontSize(16)
138          .fontColor($r('app.color.text_color_primary'))
139        Column() {
140          TextInput({ placeholder: $r('app.string.input_value') })
141            .id('add_value')
142            .height(48)
143            .fontSize(16)
144            .backgroundColor($r('sys.color.ohos_id_color_background_transparent'))
145            .onChange((value: string) => {
146              this.keyValuePair.value = value;
147            })
148        }
149        .layoutWeight(1)
150        .margin({ left: 52, right: 16 })
151      }
152      .height(64)
153      .width('100%')
154      .borderRadius(16)
155      .backgroundColor($r('app.color.bg_white'))
156      .margin({ top: 12 })
157    }
158    .height('100%')
159    .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
160    .padding({ left: 12, right: 12 })
161  }
162}