• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device 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 prompt from '@ohos.promptAction'
18import contact from '@ohos.contact'
19import Logger from '../data/Logger'
20import { NewContact } from '../component/NewContact'
21
22const TAG: string = 'AddContact'
23
24@Entry
25@Component
26struct AddContact {
27  @State name: string = ''
28  @State email: string = ''
29  @State nickName: string = ''
30  @State phoneNumber: string = ''
31  @State postalAddress: string = ''
32
33  build() {
34    Column() {
35      Row() {
36        Image($r('app.media.no'))
37          .width(32)
38          .height(32)
39          .margin({ left: 32, right: 22 })
40          .objectFit(ImageFit.Contain)
41          .id('cancel')
42          .onClick(() => {
43            router.back()
44          })
45
46        Text($r('app.string.newContact'))
47          .fontSize(26)
48          .fontWeight(400)
49          .textAlign(TextAlign.End)
50          .fontColor(Color.Black)
51
52        Blank()
53
54        Image($r('app.media.yes'))
55          .id('confirm')
56          .width(32)
57          .height(32)
58          .margin({ right: 32 })
59          .objectFit(ImageFit.Contain)
60          .onClick(async () => {
61            const TEST_RULER = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}/
62            Logger.info(TAG, `this number is ${this.phoneNumber} this test result ${TEST_RULER.test(this.phoneNumber)}`)
63            if (TEST_RULER.test(this.phoneNumber) === true) {
64              let contacts = {
65                name: { fullName: this.name },
66                nickName: { nickName: this.nickName },
67                phoneNumbers: [{ phoneNumber: this.phoneNumber }],
68                emails: [{ email: this.email }],
69                postalAddresses: [{ postalAddress: this.postalAddress }]
70              }
71              let id = await contact.addContact(contacts)
72              router.replaceUrl({
73                url: 'pages/Index'
74              })
75              router.clear()
76            } else {
77              prompt.showToast({
78                message: 'Please input phone number.',
79                duration: 2000,
80              })
81            }
82          })
83      }
84      .height('6%')
85      .width('100%')
86      .margin({ top: 32 })
87      .constraintSize({ minHeight: 50 })
88
89      NewContact({
90        name: $name,
91        email: $email,
92        postalAddress: $postalAddress,
93        nickName: $nickName,
94        phoneNumber: $phoneNumber
95      })
96    }
97    .width('100%')
98    .height('100%')
99    .backgroundColor($r('app.color.add_contact_bg'))
100  }
101}