JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-iphone
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#import "LinphoneManager.h"
#import "ChatConversationTableView.h"
#import "ChatConversationImdnView.h"
#import "UIChatBubbleTextCell.h"
#import "UIChatBubblePhotoCell.h"
#import "UIChatNotifiedEventCell.h"
#import "PhoneMainView.h"
 
@implementation ChatConversationTableView
 
#pragma mark - Lifecycle Functions
 
- (void)dealloc {
    [self clearEventList];
}
 
 
#pragma mark - ViewController Functions
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tableView.accessibilityIdentifier = @"ChatRoom list";
    _imagesInChatroom = [NSMutableDictionary dictionary];
    _currentIndex = 0;
    [self startEphemeralDisplayTimer];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(ephemeralDeleted:)
                                               name:kLinphoneEphemeralMessageDeletedInRoom
                                             object:nil];
}
 
-(void) viewWillDisappear:(BOOL)animated {
    [self stopEphemeralDisplayTimer];
    [NSNotificationCenter.defaultCenter removeObserver:self];
    [super viewWillDisappear:animated];
}
 
#pragma mark -
 
- (void)clearEventList {
    for (NSValue *value in totalEventList) {
        LinphoneEventLog *event = value.pointerValue;
        linphone_event_log_unref(event);
    }
    [eventList removeAllObjects];
    [totalEventList removeAllObjects];
}
 
-(bool) eventTypeIsOfInterestForOneToOneRoom:(LinphoneEventLogType)type {
    return
    type == LinphoneEventLogTypeConferenceChatMessage ||
    type == LinphoneEventLogTypeConferenceEphemeralMessageEnabled ||
    type == LinphoneEventLogTypeConferenceEphemeralMessageDisabled ||
    type == LinphoneEventLogTypeConferenceEphemeralMessageLifetimeChanged;
}
 
- (void)updateData {
    [self clearEventList];
    if (!_chatRoom)
        return;
    
    LinphoneChatRoomCapabilitiesMask capabilities = linphone_chat_room_get_capabilities(_chatRoom);
    bool oneToOne = capabilities & LinphoneChatRoomCapabilitiesOneToOne;
    bctbx_list_t *chatRoomEvents = linphone_chat_room_get_history_events(_chatRoom, 0);
    
    
    bctbx_list_t *head = chatRoomEvents;
    size_t listSize = bctbx_list_size(chatRoomEvents);
    totalEventList = [[NSMutableArray alloc] initWithCapacity:listSize];
    eventList = [[NSMutableArray alloc] initWithCapacity:MIN(listSize, BASIC_EVENT_LIST)];
    BOOL autoDownload = (linphone_core_get_max_size_for_auto_download_incoming_files(LC) > -1);
    while (chatRoomEvents) {
        LinphoneEventLog *event = (LinphoneEventLog *)chatRoomEvents->data;
        if (oneToOne && ![self eventTypeIsOfInterestForOneToOneRoom:linphone_event_log_get_type(event)]) {
            chatRoomEvents = chatRoomEvents->next;
        } else {
            LinphoneChatMessage *chat = linphone_event_log_get_chat_message(event);
            // if auto_download is available and file transfer in progress, not add event now
            if (!(autoDownload && chat && linphone_chat_message_is_file_transfer_in_progress(chat))) {
                [totalEventList addObject:[NSValue valueWithPointer:linphone_event_log_ref(event)]];
                if (listSize <= BASIC_EVENT_LIST) {
                    [eventList addObject:[NSValue valueWithPointer:linphone_event_log_ref(event)]];
                }
            }
            
            chatRoomEvents = chatRoomEvents->next;
            listSize -= 1;
        }
    }
    bctbx_list_free_with_data(head, (bctbx_list_free_func)linphone_event_log_unref);
}
 
- (void)refreshData {
    if (totalEventList.count <= eventList.count) {
        _currentIndex = 0;
        return;
    }
    
    NSUInteger num = MIN(totalEventList.count-eventList.count, BASIC_EVENT_LIST);
    _currentIndex = num - 1;
    while (num) {
        NSInteger index = totalEventList.count - eventList.count - 1;
        [eventList insertObject:[totalEventList objectAtIndex:index] atIndex:0];
        index -= 1;
        num -= 1;
    }
}
 
- (void)reloadData {
    [self updateData];
    [self.tableView reloadData];
    [self scrollToLastUnread:false];
}
 
- (void)addEventEntry:(LinphoneEventLog *)event {
    [eventList addObject:[NSValue valueWithPointer:linphone_event_log_ref(event)]];
    [totalEventList addObject:[NSValue valueWithPointer:linphone_event_log_ref(event)]];
    int pos = (int)eventList.count - 1;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:pos inSection:0];
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView reloadData];
    [self.tableView endUpdates];
}
 
- (void)updateEventEntry:(LinphoneEventLog *)event {
    NSInteger index = [eventList indexOfObject:[NSValue valueWithPointer:event]];
    if (index < 0) {
        LOGW(@"event entry doesn't exist");
        return;
    }
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]]
                          withRowAnimation:FALSE]; // just reload
    return;
}
 
- (void)scrollToBottom:(BOOL)animated {
    //[self.tableView reloadData];
    size_t count = eventList.count;
    if (!count)
        return;
 
    //[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:(count - 1) inSection:0]];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(count - 1) inSection:0]
                          atScrollPosition:UITableViewScrollPositionBottom
                                  animated:YES];
}
 
- (void)scrollToLastUnread:(BOOL)animated {
    if (eventList.count == 0 || _chatRoom == nil)
        return;
 
    int index = -1;
    size_t count = eventList.count;
    // Find first unread & set all entry read
    for (int i = (int)count - 1; i > 0; --i) {
        LinphoneEventLog *event = [[eventList objectAtIndex:i] pointerValue];
        if (!(linphone_event_log_get_type(event) == LinphoneEventLogTypeConferenceChatMessage))
            break;
 
        LinphoneChatMessage *chat = linphone_event_log_get_chat_message(event);
        int read = linphone_chat_message_is_read(chat);
        LinphoneChatMessageState state = linphone_chat_message_get_state(chat);
        if (read == 0 &&
            !(state == LinphoneChatMessageStateFileTransferError || state == LinphoneChatMessageStateNotDelivered)) {
            if (index == -1) {
                index = i;
                break;
            }
        }
    }
    if (index == -1 && count > 0)
        index = (int)count - 1;
 
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
        [ChatConversationView markAsRead:_chatRoom];
 
    // Scroll to unread
    if (index < 0)
        return;
 
    [self.tableView.layer removeAllAnimations];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]
                          atScrollPosition:UITableViewScrollPositionTop
                                  animated:animated];
}
 
#pragma mark - Property Functions
 
- (void)setChatRoom:(LinphoneChatRoom *)room {
    _chatRoom = room;
    [self reloadData];
    [self updateEphemeralTimes];
}
 
static const int MAX_AGGLOMERATED_TIME=300;
static const int BASIC_EVENT_LIST=15;
 
- (BOOL)isFirstIndexInTableView:(NSIndexPath *)indexPath chat:(LinphoneChatMessage *)chat {
    LinphoneEventLog *previousEvent = nil;
    NSInteger indexOfPreviousEvent = indexPath.row - 1;
    if (indexOfPreviousEvent > -1) {
        previousEvent = [[eventList objectAtIndex:indexOfPreviousEvent] pointerValue];
        if (linphone_event_log_get_type(previousEvent) != LinphoneEventLogTypeConferenceChatMessage) {
            return TRUE;
        }
    }
    if (!previousEvent)
        return TRUE;
 
    LinphoneChatMessage *previousChat = linphone_event_log_get_chat_message(previousEvent);
    if (!linphone_address_equal(linphone_chat_message_get_from_address(previousChat), linphone_chat_message_get_from_address(chat))) {
        return TRUE;
    }
    // the maximum interval between 2 agglomerated chats at 5mn
    if ((linphone_chat_message_get_time(chat)-linphone_chat_message_get_time(previousChat)) > MAX_AGGLOMERATED_TIME) {
        return TRUE;
    }
        
    return FALSE;
}
 
- (BOOL)isLastIndexInTableView:(NSIndexPath *)indexPath chat:(LinphoneChatMessage *)chat {
    LinphoneEventLog *nextEvent = nil;
    NSInteger indexOfNextEvent = indexPath.row + 1;
    while (!nextEvent && indexOfNextEvent < [eventList count]) {
        LinphoneEventLog *tmp = [[eventList objectAtIndex:indexOfNextEvent] pointerValue];
        if (linphone_event_log_get_type(tmp) == LinphoneEventLogTypeConferenceChatMessage) {
            nextEvent = tmp;
        }
        ++indexOfNextEvent;
    }
    
    if (!nextEvent)
        return TRUE;
 
    LinphoneChatMessage *nextChat = linphone_event_log_get_chat_message(nextEvent);
    if (!linphone_address_equal(linphone_chat_message_get_from_address(nextChat), linphone_chat_message_get_from_address(chat))) {
        return TRUE;
    }
    
    return FALSE;
}
 
#pragma mark - UITableViewDataSource Functions
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return eventList.count;
}
 
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
    if (!_chatRoom && [[cell reuseIdentifier] isEqualToString:@"UIChatBubblePhotoCell"]) {
        [(UIChatBubbleTextCell *)cell clearEncryptedFiles];
    }
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *kCellId = nil;
    LinphoneEventLog *event = [[eventList objectAtIndex:indexPath.row] pointerValue];
    if (linphone_event_log_get_type(event) == LinphoneEventLogTypeConferenceChatMessage) {
        LinphoneChatMessage *chat = linphone_event_log_get_chat_message(event);
        if (linphone_chat_message_get_file_transfer_information(chat) || linphone_chat_message_get_external_body_url(chat))
            kCellId = NSStringFromClass(UIChatBubblePhotoCell.class);
        else
            kCellId = NSStringFromClass(UIChatBubbleTextCell.class);
 
        // To use less memory and to avoid overlapping. To be improved.
        UIChatBubbleTextCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
        cell = [[NSClassFromString(kCellId) alloc] initWithIdentifier:kCellId];
 
        [cell setEvent:event];
        if (chat) {
            cell.isFirst = [self isFirstIndexInTableView:indexPath chat:chat];
            cell.isLast = [self isLastIndexInTableView:indexPath chat:chat];
            [cell update];
        }
 
        [cell setChatRoomDelegate:_chatRoomDelegate];
        [super accessoryForCell:cell atPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        kCellId = NSStringFromClass(UIChatNotifiedEventCell.class);
        UIChatNotifiedEventCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
        if (!cell)
            cell = [[NSClassFromString(kCellId) alloc] initWithIdentifier:kCellId];
 
        [cell setEvent:event];
        [super accessoryForCell:cell atPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
 
#pragma mark - UITableViewDelegate Functions
 
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [_chatRoomDelegate tableViewIsScrolling];
}
 
static const CGFloat MESSAGE_SPACING_PERCENTAGE = 1.f;
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LinphoneEventLog *event = [[eventList objectAtIndex:indexPath.row] pointerValue];
    if (linphone_event_log_get_type(event) == LinphoneEventLogTypeConferenceChatMessage) {
        LinphoneChatMessage *chat = linphone_event_log_get_chat_message(event);
        
        //If the message is followed by another one that is not from the same address, we add a little space under it
        CGFloat height = 0;
        if ([self isLastIndexInTableView:indexPath chat:chat])
            height += tableView.frame.size.height * MESSAGE_SPACING_PERCENTAGE / 100;
        if (![self isFirstIndexInTableView:indexPath chat:chat])
            height -= 20;
 
        return [UIChatBubbleTextCell ViewHeightForMessage:chat withWidth:self.view.frame.size.width].height + height;
    }
    return [UIChatNotifiedEventCell height];
}
 
- (void) tableView:(UITableView *)tableView deleteRowAtIndex:(NSIndexPath *)indexPath {
    [tableView beginUpdates];
    LinphoneEventLog *event = [[eventList objectAtIndex:indexPath.row] pointerValue];
    linphone_event_log_delete_from_database(event);
    NSInteger index = indexPath.row + _currentIndex + (totalEventList.count - eventList.count);
    if (index < totalEventList.count)
        [totalEventList removeObjectAtIndex:index];
    [eventList removeObjectAtIndex:indexPath.row];
 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationBottom];
    [tableView endUpdates];
    [self loadData];
}
 
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView
                  editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    LinphoneEventLog *event = [[eventList objectAtIndex:indexPath.row] pointerValue];
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
                                                                            title:NSLocalizedString(@"Delete", nil)
                                                                          handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
                                                                              [self tableView:tableView deleteRowAtIndex:indexPath];
                                                                          }];
 
    UITableViewRowAction *imdnAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
                                                                            title:NSLocalizedString(@"Info", nil)
                                                                          handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
                                                                              LinphoneChatMessage *msg = linphone_event_log_get_chat_message(event);
                                                                              ChatConversationImdnView *view = VIEW(ChatConversationImdnView);
                                                                              view.msg = msg;
                                                                              [PhoneMainView.instance changeCurrentView:view.compositeViewDescription];
                                                                          }];
 
    if (linphone_event_log_get_type(event) == LinphoneEventLogTypeConferenceChatMessage &&
        !(linphone_chat_room_get_capabilities(_chatRoom) & LinphoneChatRoomCapabilitiesOneToOne))
        return @[deleteAction, imdnAction];
    else
        return @[deleteAction];
}
 
- (void)tableView:(UITableView *)tableView
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
     forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self tableView:tableView deleteRowAtIndex:indexPath];
    }
}
 
- (void)removeSelectionUsing:(void (^)(NSIndexPath *))remover {
    [super removeSelectionUsing:^(NSIndexPath *indexPath) {
        LinphoneEventLog *event = [[eventList objectAtIndex:indexPath.row] pointerValue];
        if (linphone_event_log_get_chat_message(event)) {
            linphone_chat_room_delete_message(_chatRoom, linphone_event_log_get_chat_message(event));
        }
        NSInteger index = indexPath.row + _currentIndex + (totalEventList.count - eventList.count);
        if (index < totalEventList.count)
            [totalEventList removeObjectAtIndex:index];
        [eventList removeObjectAtIndex:indexPath.row];
    }];
}
 
#pragma mark ephemeral messages
 
-(void) startEphemeralDisplayTimer {
    _ephemeralDisplayTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                              target:self
                                                            selector:@selector(updateEphemeralTimes)
                                                            userInfo:nil
                                                             repeats:YES];
}
 
-(void) updateEphemeralTimes {
    NSDateComponentsFormatter *f= [[NSDateComponentsFormatter alloc] init];
    f.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
    f.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
        
    for (NSValue *v in eventList) {
        LinphoneEventLog *event = [v pointerValue];
        if (linphone_event_log_get_type(event) == LinphoneEventLogTypeConferenceChatMessage) {
            LinphoneChatMessage *msg = linphone_event_log_get_chat_message(event);
            if (linphone_chat_message_is_ephemeral(msg)) {
                UIChatBubbleTextCell *cell = (UIChatBubbleTextCell  *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[eventList indexOfObject:v] inSection:0]];
                long duration = linphone_chat_message_get_ephemeral_expire_time(msg) == 0 ?
                    linphone_chat_room_get_ephemeral_lifetime(linphone_chat_message_get_chat_room(msg)) :
                    linphone_chat_message_get_ephemeral_expire_time(msg)-[NSDate date].timeIntervalSince1970;
                f.allowedUnits = (duration > 86400 ? kCFCalendarUnitDay : 0)|(duration > 3600 ? kCFCalendarUnitHour : 0)|kCFCalendarUnitMinute|kCFCalendarUnitSecond;
                cell.ephemeralTime.text =  [f stringFromTimeInterval:duration];
                cell.ephemeralTime.hidden = NO;
                cell.ephemeralIcon.hidden = NO;
            }
        }
    }
}
 
-(void) stopEphemeralDisplayTimer {
    [_ephemeralDisplayTimer invalidate];
}
 
- (void)ephemeralDeleted:(NSNotification *)notif {
    LinphoneChatRoom *r =[[notif.userInfo objectForKey:@"room"] pointerValue];
    if (r ==_chatRoom)
        [self reloadData];
}
 
@end