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
184    // A dialog box is displayed when you touch a recent contact.
185    clickSendMessage(item) {
186        this.contactName = item.conversation.name;
187        let contactsParamMsg: LooseObject = {};
188        contactsParamMsg.contactsNum = item.conversation.contactsNum;
189        contactsParamMsg.contactName = item.conversation.name;
190        contactsParamMsg.telephoneFormat = item.conversation.telephoneFormat;
191        contactsParamMsg.telephone = item.conversation.telephone;
192        contactsParamMsg.contactNameSplit = item.conversation.name.split(common.string.COMMA)[0];
193        contactsParamMsg.telephoneFormatSplit = item.conversation.telephoneFormat.split(common.string.COMMA)[0];
194        this.threadId = item.conversation.threadId;
195        this.dialogMsg.contactsParam = contactsParamMsg;
196        // Determine the font length of a contact.
197        this.checkContactNameLen();
198    }
199
200    checkContactNameLen() {
201        if (this.contactName != null && this.contactName.length > this.contactNameLen) {
202            this.titleChecked = true;
203        } else {
204            this.titleChecked = false;
205        }
206    }
207
208    jumpToSelectContacts(callback?) {
209        // The page for selecting a contact is displayed.
210        let actionData: LooseObject = {};
211        actionData.pageFlag = common.contactPage.PAGE_FLAG_SINGLE_CHOOSE;
212        this.jumpToContactForResult(actionData, callback);
213    }
214
215    // Go to Contacts app
216    async jumpToContactForResult(actionData, callback) {
217        let str = commonService.commonContactParam(actionData);
218        let data = await globalThis.mmsContext.startAbilityForResult(str);
219        this.dialogMsg.data = data;
220        if (data.resultCode == 0 && JSON.parse(data.want.parameters.contactObjects).length != 0) {
221            this.DialogShow = true;
222            let contactsParam = ContactService.getInstance().dealContactParams(data.want.parameters.contactObjects);
223            let contactsParamMsg: LooseObject = {};
224            contactsParamMsg.contactsNum = contactsParam.contactsNum;
225            contactsParamMsg.contactName = contactsParam.strContactsName;
226            contactsParamMsg.telephoneFormat = contactsParam.strContactsNumberFormat;
227            contactsParamMsg.telephone = contactsParam.strContactsNumber;
228            contactsParamMsg.contactNameSplit = contactsParam.strContactsName.split(common.string.COMMA)[0];
229            contactsParamMsg.telephoneFormatSplit = contactsParam.strContactsNumberFormat.split(common.string.COMMA)[0];
230            this.dialogMsg.contactsParam = contactsParamMsg;
231            ConversationListService.getInstance().querySessionByTelephone(this.telephone, res => {
232                let response = res.response;
233                if (res.code === common.int.SUCCESS && response.id > 0) {
234                    this.threadId = response.id;
235                } else {
236                    this.threadId = 0;
237                }
238                this.checkContactNameLen();
239            }, null);
240            if (callback) {
241                callback();
242            }
243        }
244    }
245
246    // Include Forward Message Source Button Switch - SMS
247    changeValue(item, e) {
248        item.content = e.text;
249    }
250
251    transmit(): void {
252        let params: LooseObject = {
253            'threadId': this.threadId,
254            'strContactsName': this.dialogMsg.contactsParam.contactName, //Contact Name
255            'strContactsNumber': this.dialogMsg.contactsParam.telephone, //Mobile phone number
256            'strContactsNumberFormat': this.dialogMsg.contactsParam.telephoneFormat, //Format the mobile number.
257            'transmitFlag': true, //Send Flag
258            'contactsPage': false, //Display Forwarding Dialog Box
259            'mmsSource': this.mmsSource, //MMS list data
260            'isSlideDetail': this.isSlideDetail, //Indicates whether the slide page is an MMS message.
261            'transmitSource': this.transmitContentList, //List of contents forwarded by multiple messages
262            'isContainerOriginSource': this.isChecked //Whether selected
263        }
264        router.replaceUrl({
265            url: 'pages/conversation/conversation',
266            params: params
267        });
268    }
269}
270