1// @ts-nocheck 2/** 3 * Copyright (c) 2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import router from '@ohos.router'; 18 19import LooseObject from "../../data/LooseObject" 20import ContactService from '../../service/ContactsService'; 21import common from '../../data/commonData'; 22import commonService from '../../service/CommonService' 23import ConversationListService from "../../service/ConversationListService"; 24import AvatarColor from "../../model/common/AvatarColor"; 25import DateUtil from "../../utils/DateUtil"; 26import TransmitMsgDataSource from "../../model/TransmitMsgDataSource"; 27import HiLog from "../../utils/HiLog"; 28 29const TAG = "TransmitMsgController" 30 31export default class TransmitMsgController { 32 private static sInstance: TransmitMsgController; 33 private routerParams: LooseObject = router.getParams() 34 // Total 35 total: 0; 36 // Information List 37 contactsList: Array<any> = []; 38 // List of contents forwarded by multiple messages 39 transmitContentList: Array<any> = []; 40 // Dual SIM card 41 doubleCard: boolean = false; 42 // Contents 43 content: string = ''; 44 // MMS attachment address 45 msgUriPath: string = ''; 46 // Number of Contacts 47 contactsNum: number = 0; 48 // Contact Name 49 contactName: string = ''; 50 // Format the mobile number. 51 telephoneFormat: string = ''; 52 // Multi-crowd hair only shows one person 53 contactNameSplit: string = ''; 54 // Only one mobile phone number is displayed for multiple people. 55 telephoneFormatSplit: string = ''; 56 // Mobile phone number 57 telephone: string = ''; 58 // Whether selected 59 isChecked: boolean = true; 60 // Indicates whether the message is an MMS message. 61 isMms: boolean = false; 62 // Card 1 63 simOne: number = 0; 64 // Card 2 65 simTwo: number = 1; 66 // Sending SMS message ID 67 threadId: number = 0; 68 // Forwarded Content Title 69 transmitContent: string = ''; 70 // Formatted forwarding content 71 transmitContentFormat: ''; 72 // Forwarding Content Editing Status Content 73 transmitContentEdit: string = ''; 74 // Display Forwarding Dialog Box 75 contactsPage: boolean = false; 76 // The font size of the dialog title is displayed. When the number of characters is small, the large font is 77 // displayed. When the number of characters is large, the small font is displayed. 78 titleChecked: boolean = false; 79 // Contact length threshold in dialog title 80 contactNameLen: number = 20; 81 // List pagination, number of pages 82 page: number = 0; 83 // Number of pages 84 limit: number = 15; 85 // Indicates whether the slide page is an MMS message. 86 isSlideDetail: boolean = false; 87 // MMS list data 88 mmsSource: Array<any> = []; 89 transmitItemSources: Array<any> = []; 90 reg: RegExp = /^[\u4e00-\u9fa5_a-zA-Z]+$/; 91 dialogMsg: LooseObject = {}; 92 DialogShow: boolean = false; 93 transmitMsgDataSource: TransmitMsgDataSource = new TransmitMsgDataSource(); 94 95 static getInstance() { 96 if (TransmitMsgController.sInstance == null) { 97 TransmitMsgController.sInstance = new TransmitMsgController(); 98 } 99 return TransmitMsgController.sInstance; 100 } 101 102 onInit() { 103 // List of contents forwarded by multiple messages 104 this.transmitContentList = router.getParams().transmitContentList; 105 this.dialogMsg.transmitContentList = this.transmitContentList; 106 this.dialogMsg.transmitContent = router.getParams().transmitContent; 107 this.threadId = router.getParams().threadId; 108 } 109 110 onShow() { 111 this.page = 0; 112 this.requestItem(); 113 } 114 115 // Obtaining List Data by Page 116 requestItem() { 117 let count = this.page * this.limit; 118 if (this.page === 0) { 119 this.page++; 120 this.queryAllMessages(); 121 } else if (count < this.total && this.contactsList.length > (this.page - 1) * this.limit) { 122 // The restriction on contactsList is to prevent multiple update requests during initialization. 123 this.page++; 124 this.queryAllMessages(); 125 } 126 } 127 128 // Queries all list information. 129 queryAllMessages() { 130 let actionData: LooseObject = {}; 131 actionData.page = this.page; 132 actionData.limit = this.limit; 133 actionData.orderByTimeDesc = true; 134 ConversationListService.getInstance().querySessionList(actionData, result => { 135 if (result.code == common.int.SUCCESS) { 136 this.contactsList = this.buildSessionList(result); 137 this.transmitMsgDataSource.refresh(this.contactsList); 138 this.total = result.total; 139 } 140 }); 141 } 142 143 buildSessionList(result) { 144 let res = []; 145 result.response.forEach(item => { 146 // Inherit selected items 147 this.contactsList.some(oldItem => { 148 if (item.threadId === oldItem.threadId) { 149 item.isCbChecked = oldItem.isCbChecked; 150 return true; 151 } 152 }); 153 let obj: LooseObject = {}; 154 obj = item; 155 obj.isDelShow = false; 156 obj.itemLeft = 0; 157 obj.photoFirstName = common.string.EMPTY_STR; 158 if( obj.name !== common.string.EMPTY_STR && this.reg.test(obj.name.substring(0, 1))) { 159 obj.photoFirstName = obj.name.substring(0, 1).toUpperCase(); 160 } 161 obj.portraitColor = AvatarColor.background.Color[Math.abs(parseInt(obj.threadId, 10)) % 6]; 162 // Time conversion 163 DateUtil.convertDateFormatForItem(item, false); 164 // Processes the display of the MMS message content. 165 this.dealMmsListContent(item); 166 res.push(obj); 167 }); 168 return res; 169 } 170 171 dealMmsListContent(item) { 172 if (item.hasMms && item.hasAttachment) { 173 if (item.content == common.string.EMPTY_STR) { 174 item.content = $r("app.string.attachment_no_subject"); 175 } else { 176 item.content = $r("app.string.attachment", item.content); 177 } 178 } 179 if (item.hasMms && !item.hasAttachment && item.content == common.string.EMPTY_STR) { 180 item.content = $r("app.string.no_subject"); 181 } 182 } 183 // A dialog box is displayed when you touch a recent contact. 184 clickSendMessage(item) { 185 this.contactName = item.conversation.name; 186 let contactsParamMsg: LooseObject = {}; 187 contactsParamMsg.contactsNum = item.conversation.contactsNum; 188 contactsParamMsg.contactName = item.conversation.name; 189 contactsParamMsg.telephoneFormat = item.conversation.telephoneFormat; 190 contactsParamMsg.telephone = item.conversation.telephone; 191 contactsParamMsg.contactNameSplit = item.conversation.name.split(common.string.COMMA)[0]; 192 contactsParamMsg.telephoneFormatSplit = item.conversation.telephoneFormat.split(common.string.COMMA)[0]; 193 this.threadId = item.conversation.threadId; 194 this.dialogMsg.contactsParam = contactsParamMsg; 195 // Determine the font length of a contact. 196 this.checkContactNameLen(); 197 } 198 199 checkContactNameLen() { 200 if (this.contactName != null && this.contactName.length > this.contactNameLen) { 201 this.titleChecked = true; 202 } else { 203 this.titleChecked = false; 204 } 205 } 206 207 jumpToSelectContacts(callback?) { 208 // The page for selecting a contact is displayed. 209 let actionData: LooseObject = {}; 210 actionData.pageFlag = common.contactPage.PAGE_FLAG_SINGLE_CHOOSE; 211 this.jumpToContactForResult(actionData, callback); 212 } 213 214 // Go to Contacts app 215 async jumpToContactForResult(actionData, callback) { 216 let str = commonService.commonContactParam(actionData); 217 let data = await globalThis.mmsContext.startAbilityForResult(str); 218 this.dialogMsg.data = data; 219 if (data.resultCode == 0 && JSON.parse(data.want.parameters.contactObjects).length != 0) { 220 this.DialogShow = true; 221 let contactsParam = ContactService.getInstance().dealContactParams(data.want.parameters.contactObjects); 222 let contactsParamMsg: LooseObject = {}; 223 contactsParamMsg.contactsNum = contactsParam.contactsNum; 224 contactsParamMsg.contactName = contactsParam.strContactsName; 225 contactsParamMsg.telephoneFormat = contactsParam.strContactsNumberFormat; 226 contactsParamMsg.telephone = contactsParam.strContactsNumber; 227 contactsParamMsg.contactNameSplit = contactsParam.strContactsName.split(common.string.COMMA)[0]; 228 contactsParamMsg.telephoneFormatSplit = contactsParam.strContactsNumberFormat.split(common.string.COMMA)[0]; 229 this.dialogMsg.contactsParam = contactsParamMsg; 230 ConversationListService.getInstance().querySessionByTelephone(this.telephone, res => { 231 let response = res.response; 232 if (res.code === common.int.SUCCESS && response.id > 0) { 233 this.threadId = response.id; 234 } else { 235 this.threadId = 0; 236 } 237 this.checkContactNameLen(); 238 }, null); 239 if (callback) { 240 callback(); 241 } 242 } 243 } 244 245 // Include Forward Message Source Button Switch - SMS 246 changeValue(item, e) { 247 item.content = e.text; 248 } 249 250 transmit(): void { 251 let params: LooseObject = { 252 "threadId": this.threadId, 253 "strContactsName": this.dialogMsg.contactsParam.contactName, //Contact Name 254 "strContactsNumber": this.dialogMsg.contactsParam.telephone, //Mobile phone number 255 "strContactsNumberFormat": this.dialogMsg.contactsParam.telephoneFormat, //Format the mobile number. 256 "transmitFlag": true, //Send Flag 257 "contactsPage": false, //Display Forwarding Dialog Box 258 "mmsSource": this.mmsSource, //MMS list data 259 "isSlideDetail": this.isSlideDetail,//Indicates whether the slide page is an MMS message. 260 "transmitSource": this.transmitContentList,//List of contents forwarded by multiple messages 261 "isContainerOriginSource": this.isChecked //Whether selected 262 } 263 router.replaceUrl({ 264 url: 'pages/conversation/conversation', 265 params: params 266 }); 267 } 268} 269