1/** 2 * Copyright (c) 2022 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 { ContactVo } from '../../model/bean/ContactVo'; 18import DeleteDialogEx from '../../pages/dialog/DeleteDialogEx'; 19import ShareDialogEx from '../../pages/dialog/ShareDialogEx'; 20import { StringUtil } from '../../../../../../common/src/main/ets/util/StringUtil'; 21import { HiLog } from '../../../../../../common/src/main/ets/util/HiLog'; 22import ContactListPresenter from '../../presenter/contact/ContactListPresenter'; 23import EnvironmentProp from '../../feature/EnvironmentProp'; 24 25const TAG = 'ContactListItemView '; 26 27/** 28 * The contact item component is used to display a single contact. 29 */ 30@Component 31export default struct ContactListItemView { 32 private presenter: ContactListPresenter = ContactListPresenter.getInstance(); 33 @State item: ContactVo = new ContactVo("", "", "", "", "", "", true, "", ""); 34 @State private index: number = 0; 35 @State showIndex: boolean = false; 36 @State showDivifer: boolean = false; 37 @Link contactListTouchable: boolean; 38 @LocalStorageProp('breakpoint') curBp: string = 'sm'; 39 shareDialogControler: CustomDialogController = new CustomDialogController({ 40 builder: ShareDialogEx({ 41 itemList: this.presenter.shareList, 42 cancel: () => { 43 this.presenter.onShareDialogCancel(); 44 }, 45 title: $r("app.string.dialog_share"), 46 cancelText: $r("app.string.dialog_cancel") 47 }), 48 autoCancel: false, 49 alignment: (EnvironmentProp.isTablet() ? DialogAlignment.Center : DialogAlignment.Bottom), 50 gridCount: 4 51 }); 52 deleteDialogControler: CustomDialogController = new CustomDialogController({ 53 builder: DeleteDialogEx({ 54 cancel: () => { 55 this.presenter.onDeleteDialogCancel(); 56 }, 57 confirm: () => { 58 this.presenter.onDeleteDialogConfirm(this.index, this.item); 59 }, 60 title: $r("app.string.delete_dialog_title"), 61 cancalText: $r("app.string.dialog_cancel"), 62 confrimText: $r("app.string.dialog_delete") 63 }), 64 autoCancel: false, 65 alignment: (EnvironmentProp.isTablet() ? DialogAlignment.Center : DialogAlignment.Bottom), 66 offset: { dx: 0, dy: -16 }, 67 gridCount: 4, 68 closeAnimation: { duration: 100 } 69 }); 70 71 aboutToDisappear() { 72 this.shareDialogControler = null; 73 this.deleteDialogControler = null; 74 } 75 76 onDeleteClick() { 77 HiLog.i(TAG, 'onDeleteClick'); 78 this.deleteDialogControler.open(); 79 } 80 81 onShareClick() { 82 HiLog.i(TAG, 'onShareClick'); 83 this.shareDialogControler.open(); 84 } 85 86 @Builder MenuBuilder() { 87 Column() { 88 if (this.item.title) { 89 Text(this.item.title) 90 .textAlign(TextAlign.Start) 91 .fontSize($r("sys.float.ohos_id_text_size_headline8")) 92 .fontColor($r("sys.color.ohos_id_color_text_primary")) 93 .fontWeight(FontWeight.Medium) 94 .textOverflow({ overflow: TextOverflow.Ellipsis }) 95 .maxLines(1) 96 .height($r("app.float.id_item_height_max")) 97 } 98 Button({ type: ButtonType.Normal, stateEffect: true }) { 99 Text($r("app.string.delete")) 100 .width('100%') 101 .height($r("app.float.id_item_height_mid")) 102 .textAlign(TextAlign.Start) 103 .fontSize($r("sys.float.ohos_id_text_size_body1")) 104 .fontColor($r("sys.color.ohos_id_color_text_primary")) 105 .fontWeight(FontWeight.Regular) 106 } 107 .backgroundColor(Color.White) 108 .onClick(() => { 109 this.presenter.setCurItem(this.item); 110 ContextMenu.close(); 111 this.deleteDialogControler.open(); 112 }) 113 } 114 .alignItems(HorizontalAlign.Start) 115 .borderRadius($r("sys.float.ohos_id_corner_radius_card")) 116 .width($r("app.float.contact_longpress_dialog_width")) 117 .padding({ 118 left: $r("app.float.id_card_margin_max"), 119 right: $r("app.float.id_card_margin_max"), 120 top: $r("app.float.id_card_margin_mid"), 121 bottom: $r("app.float.id_card_margin_mid") 122 }) 123 } 124 125 build() { 126 Row() { 127 Row() { 128 if (StringUtil.isEmpty(this.item.nameSuffix)) { 129 Image(StringUtil.isEmpty(this.item.portraitPath) ? $r("app.media.ic_user_portrait") : this.item.portraitPath) 130 .width($r("app.float.id_card_image_mid")) 131 .height($r("app.float.id_card_image_mid")) 132 .objectFit(ImageFit.Contain) 133 .borderRadius($r("app.float.id_card_image_mid")) 134 .backgroundColor(this.item.portraitColor) 135 } else { 136 Text(this.item.nameSuffix.toUpperCase()) 137 .fontSize(30) 138 .fontWeight(FontWeight.Bold) 139 .fontColor(Color.White) 140 .backgroundColor(this.item.portraitColor) 141 .height($r("app.float.id_card_image_mid")) 142 .width($r("app.float.id_card_image_mid")) 143 .textAlign(TextAlign.Center) 144 .borderRadius($r("app.float.id_card_image_mid")) 145 } 146 } 147 .height($r("app.float.id_card_image_mid")) 148 .width($r("app.float.id_card_image_mid")) 149 150 Column() { 151 Text(this.item.title) 152 .fontColor($r("sys.color.ohos_id_color_text_primary")) 153 .fontSize($r("sys.float.ohos_id_text_size_body1")) 154 .fontWeight(FontWeight.Medium) 155 .margin({ bottom: $r("app.float.id_card_margin_sm") }) 156 .textOverflow({ overflow: TextOverflow.Ellipsis }) 157 .maxLines(2) 158 159 if (this.item.subTitle) { 160 Text(this.item.subTitle) 161 .width('100%') 162 .fontColor($r("sys.color.ohos_id_color_text_tertiary")) 163 .fontSize($r("sys.float.ohos_id_text_size_body2")) 164 .fontWeight(FontWeight.Regular) 165 .textOverflow({ overflow: TextOverflow.Ellipsis }) 166 .maxLines(2) 167 } 168 } 169 .alignItems(HorizontalAlign.Start) 170 .padding({ top: $r("app.float.id_card_margin_mid"), bottom: $r("app.float.id_card_margin_mid"), right: $r("app.float.id_card_margin_xl") }) 171 .margin({ left: $r("app.float.id_card_margin_xl"), right: $r("app.float.id_card_margin_xl") }) 172 } 173 .constraintSize({ minHeight: $r("app.float.id_item_height_max") }) 174 .width('100%') 175 .padding({ top: this.showIndex ? 4 : 0, 176 bottom: this.showDivifer ? 4 : 0, 177 left: this.curBp === 'lg' ? $r("app.float.id_card_margin_max") : 0, 178 right: $r("app.float.id_card_margin_max") }) 179 .backgroundColor(Color.White) 180 .borderRadius({ 181 topLeft: this.showIndex ? 24 : 0, 182 topRight: this.showIndex ? 24 : 0, 183 bottomLeft: this.showDivifer ? 0 : 24, 184 bottomRight: this.showDivifer ? 0 : 24 185 }) 186 .onClick(() => { 187 router.push( 188 { 189 url: "pages/contacts/details/ContactDetail", 190 params: { 191 sourceHasId: true, 192 contactId: this.item.contactId 193 } 194 } 195 ); 196 }) 197 .bindContextMenu(this.MenuBuilder, ResponseType.LongPress, { 198 onAppear: () => { 199 this.contactListTouchable = false; 200 }, 201 onDisappear: () => { 202 this.contactListTouchable = true; 203 } 204 }) 205 } 206}