• 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 {
19  arrayToString,
20  contactDataShareUtil,
21  Contact,
22  ContactInfo,
23  logger,
24  OPERATE_STATUS,
25  TitleBar
26} from '@ohos/common'
27
28const TAG: string = 'ContactEdit'
29
30@Entry
31@Component
32struct ContactEdit {
33  private contact: Contact = undefined
34  private phone: string = ''
35  @State name: string = ''
36  @State email: string = ''
37  @State address: string = ''
38  @State remark: string = ''
39  @State phones: string[] = ['']
40  @State title: Resource = $r('app.string.create_contact')
41
42  build() {
43    Column() {
44      TitleBar({ title: $title, handleOnClick: this.submit })
45      Column() {
46        ContactInfo({
47          name: $name,
48          phones: $phones,
49          email: $email,
50          address: $address,
51          remark: $remark
52        })
53      }
54      .width('100%')
55      .height('100%')
56      .flexGrow(1)
57      .flexShrink(1)
58      .layoutWeight(1)
59    }
60    .width('100%')
61    .height('100%')
62    .padding({ left: $r('app.float.page_padding'), right: $r('app.float.page_padding') })
63    .backgroundColor($r('app.color.page_background'))
64  }
65
66  aboutToAppear() {
67    if (router.getParams()) {
68      if (router.getParams()['operate']) {
69        let operate = router.getParams()['operate']
70        if (operate === OPERATE_STATUS.UPDATE) {
71          this.title = $r('app.string.edit_contact')
72          this.contact = <Contact> router.getParams()['contact']
73          this.name = this.contact.name
74          this.phone = this.contact.phone
75          this.email = this.contact.email
76          this.address = this.contact.address
77          this.remark = this.contact.remark
78          this.phones = this.phone.split(',')
79        }
80      }
81    }
82  }
83
84  submit = async () => {
85    this.phone = arrayToString(this.phones)
86    if (this.name.length === 0 || this.phone.length === 0) {
87      prompt.showToast({ message: $r('app.string.empty_tips') })
88      return
89    }
90    if (this.phone.length < 8) {
91      prompt.showToast({ message: $r('app.string.phone_length_tips') })
92      return
93    }
94    const valueBucket = {
95      'name': this.name,
96      'phone': this.phone,
97      'email': this.email,
98      'address': this.address,
99      'remark': this.remark
100    }
101    if (this.contact === undefined) {
102      let insertId = await contactDataShareUtil.insert(getContext(this), valueBucket)
103      logger.info(TAG, `insert contact, insertId = ${insertId}`)
104      router.back()
105    } else {
106      await contactDataShareUtil.update(getContext(this), this.contact.id, valueBucket)
107      logger.info(TAG, `update contact finish`)
108      this.contact.name = this.name
109      this.contact.phone = this.phone
110      this.contact.email = this.email
111      this.contact.address = this.address
112      this.contact.remark = this.remark
113      router.back({
114        url: 'pages/ContactDetail',
115        params: {
116          contact: this.contact
117        }
118      })
119    }
120  }
121}