From 9503782650ad088f32df181800451e247c3701b1 Mon Sep 17 00:00:00 2001 From: Danmei Chen Date: Tue, 9 Oct 2018 10:57:27 +0200 Subject: [PATCH] fix crash for Imdn View --- Classes/ChatConversationImdnView.h | 4 + Classes/ChatConversationImdnView.m | 22 +++-- Classes/LinphoneUI/UIChatBubbleTextCell.h | 1 + Classes/LinphoneUI/UIChatBubbleTextCell.m | 106 ++++++++++++---------- 4 files changed, 80 insertions(+), 53 deletions(-) diff --git a/Classes/ChatConversationImdnView.h b/Classes/ChatConversationImdnView.h index 9872e8ea5..ea0991871 100644 --- a/Classes/ChatConversationImdnView.h +++ b/Classes/ChatConversationImdnView.h @@ -14,6 +14,10 @@ #import "UIRoundBorderedButton.h" @interface ChatConversationImdnView : UIViewController +{ + @private + NSString *messageText; +} @property(nonatomic) LinphoneChatMessage *msg; @property(nonatomic) bctbx_list_t *displayedList; diff --git a/Classes/ChatConversationImdnView.m b/Classes/ChatConversationImdnView.m index 9ce5f031b..703a4f3fc 100644 --- a/Classes/ChatConversationImdnView.m +++ b/Classes/ChatConversationImdnView.m @@ -47,11 +47,7 @@ static UICompositeViewDescription *compositeDescription = nil; [LinphoneUtils timeToString:linphone_chat_message_get_time(_msg) withFormat:LinphoneDateChatBubble], [FastAddressBook displayNameForAddress:addr]]; _msgAvatarImage.image = outgoing ? [LinphoneUtils selfAvatar] : [FastAddressBook imageForAddress:addr]; - if (linphone_chat_message_has_text_content(_msg)) - _msgText.text = [NSString stringWithUTF8String:linphone_chat_message_get_text(_msg)]; - else - _msgText.text = [NSString stringWithUTF8String: linphone_content_get_name(linphone_chat_message_get_file_transfer_information(_msg))]; - + _msgText.text = messageText; _msgBackgroundColorImage.image = _msgBottomBar.image = [UIImage imageNamed:(outgoing ? @"color_A.png" : @"color_D.png")]; _msgDateLabel.textColor = [UIColor colorWithPatternImage:_msgBackgroundColorImage.image]; @@ -67,13 +63,15 @@ static UICompositeViewDescription *compositeDescription = nil; } - (void)fitContent { + [self setMessageText]; + BOOL outgoing = linphone_chat_message_is_outgoing(_msg); _msgBackgroundColorImage.image = _msgBottomBar.image = [UIImage imageNamed:(outgoing ? @"color_A.png" : @"color_D.png")]; _msgDateLabel.textColor = [UIColor colorWithPatternImage:_msgBackgroundColorImage.image]; [_msgView setFrame:CGRectMake(_msgView.frame.origin.x, _msgView.frame.origin.y, _msgView.frame.size.width, - [UIChatBubbleTextCell ViewHeightForMessage:_msg withWidth:self.view.frame.size.width].height)]; + [UIChatBubbleTextCell ViewHeightForMessageText:_msg withWidth:self.view.frame.size.width textForImdn:messageText].height)]; [_tableView setFrame:CGRectMake(_tableView.frame.origin.x, _msgView.frame.origin.y + _msgView.frame.size.height + 10, @@ -85,6 +83,18 @@ static UICompositeViewDescription *compositeDescription = nil; [self fitContent]; } +- (void)setMessageText { + const char *utf8Text= linphone_chat_message_get_text_content(_msg); + LinphoneContent *fileContent = linphone_chat_message_get_file_transfer_information(_msg); + messageText = nil; + if (utf8Text) { + messageText = [NSString stringWithUTF8String:utf8Text]; + if (fileContent) + messageText = [NSString stringWithFormat:@"%@\n%@", messageText, [NSString stringWithUTF8String: linphone_content_get_name(fileContent)]]; + } else { + messageText = [NSString stringWithUTF8String: linphone_content_get_name(fileContent)]; + } +} #pragma mark - TableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.h b/Classes/LinphoneUI/UIChatBubbleTextCell.h index 1d66bc5b8..f5a4a25e4 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.h +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.h @@ -43,6 +43,7 @@ @property (nonatomic, strong) UIDocumentPickerViewController *documentPicker; + (CGSize)ViewSizeForMessage:(LinphoneChatMessage *)chat withWidth:(int)width; ++ (CGSize)ViewHeightForMessageText:(LinphoneChatMessage *)chat withWidth:(int)width textForImdn:(NSString *)imdnText; + (CGSize)getMediaMessageSizefromOriginalSize:(CGSize)originalSize withWidth:(int)width; - (void)setEvent:(LinphoneEventLog *)event; diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.m b/Classes/LinphoneUI/UIChatBubbleTextCell.m index 8d5bc27f1..75937c0a4 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.m +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.m @@ -392,9 +392,13 @@ static const CGFloat CELL_MESSAGE_X_MARGIN = 78 + 10.0f; static const CGFloat CELL_MESSAGE_Y_MARGIN = 52; // 44; + (CGSize)ViewHeightForMessage:(LinphoneChatMessage *)chat withWidth:(int)width { - NSString *messageText = [UIChatBubbleTextCell TextMessageForChat:chat]; - static UIFont *messageFont = nil; - + return [self ViewHeightForMessageText:chat withWidth:width textForImdn:nil]; +} + ++ (CGSize)ViewHeightForMessageText:(LinphoneChatMessage *)chat withWidth:(int)width textForImdn:(NSString *)imdnText{ + NSString *messageText = [UIChatBubbleTextCell TextMessageForChat:chat]; + static UIFont *messageFont = nil; + if (!messageFont) { UIChatBubbleTextCell *cell = [[UIChatBubbleTextCell alloc] initWithIdentifier:NSStringFromClass(UIChatBubbleTextCell.class)]; @@ -403,56 +407,64 @@ static const CGFloat CELL_MESSAGE_Y_MARGIN = 52; // 44; width -= 40; /*checkbox */ CGSize size; const char *url = linphone_chat_message_get_external_body_url(chat); - if (url == nil && linphone_chat_message_get_file_transfer_information(chat) == NULL) { - size = [self computeBoundingBox:messageText - size:CGSizeMake(width - CELL_MESSAGE_X_MARGIN - 4, CGFLOAT_MAX) - font:messageFont]; - } else { - NSString *localImage = [LinphoneManager getMessageAppDataForKey:@"localimage" inMessage:chat]; - NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:chat]; - NSString *localVideo = [LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:chat]; - - if(localFile) { - CGSize fileSize = CGSizeMake(200, 80); - size = [self getMediaMessageSizefromOriginalSize:fileSize withWidth:width]; + if (imdnText) { + size = [self computeBoundingBox:imdnText + size:CGSizeMake(width - CELL_MESSAGE_X_MARGIN - 4, CGFLOAT_MAX) + font:messageFont]; + size.height += 20; + } else { + if (url == nil && linphone_chat_message_get_file_transfer_information(chat) == NULL) { + size = [self computeBoundingBox:messageText + size:CGSizeMake(width - CELL_MESSAGE_X_MARGIN - 4, CGFLOAT_MAX) + font:messageFont]; } else { - CGSize textSize = CGSizeMake(0, 0); - if (![messageText isEqualToString:@"🗻"]) { - textSize = [self computeBoundingBox:messageText - size:CGSizeMake(width - CELL_MESSAGE_X_MARGIN - 4, CGFLOAT_MAX) - font:messageFont]; + NSString *localImage = [LinphoneManager getMessageAppDataForKey:@"localimage" inMessage:chat]; + NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:chat]; + NSString *localVideo = [LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:chat]; + + if(localFile) { + CGSize fileSize = CGSizeMake(200, 80); + size = [self getMediaMessageSizefromOriginalSize:fileSize withWidth:width]; + } else { + CGSize textSize = CGSizeMake(0, 0); + if (![messageText isEqualToString:@"🗻"]) { + textSize = [self computeBoundingBox:messageText + size:CGSizeMake(width - CELL_MESSAGE_X_MARGIN - 4, CGFLOAT_MAX) + font:messageFont]; + size.height += textSize.height; + } + + if (!localImage && !localVideo) { + //We are loading the image + return CGSizeMake(CELL_MIN_WIDTH + CELL_MESSAGE_X_MARGIN, CELL_MIN_HEIGHT + CELL_MESSAGE_Y_MARGIN + textSize.height); + } + PHFetchResult *assets; + if(localImage) + assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localImage] options:nil]; + else + assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localVideo] options:nil]; + if (![assets firstObject]) { + return CGSizeMake(CELL_MIN_WIDTH, CELL_MIN_HEIGHT); + } + PHAsset *asset = [assets firstObject]; + CGSize originalImageSize = CGSizeMake([asset pixelWidth], [asset pixelHeight]); + size = [self getMediaMessageSizefromOriginalSize:originalImageSize withWidth:width]; + //This fixes the image being too small. I think the issue comes form the fact that the display is retina. This should probably be changed in the future. + size.height += 40; + size.width -= CELL_MESSAGE_X_MARGIN; + + // add size for message text size.height += textSize.height; + size.width = MAX(textSize.width, size.width); } - - if (!localImage && !localVideo) { - //We are loading the image - return CGSizeMake(CELL_MIN_WIDTH + CELL_MESSAGE_X_MARGIN, CELL_MIN_HEIGHT + CELL_MESSAGE_Y_MARGIN + textSize.height); - } - PHFetchResult *assets; - if(localImage) - assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localImage] options:nil]; - else - assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localVideo] options:nil]; - if (![assets firstObject]) { - return CGSizeMake(CELL_MIN_WIDTH, CELL_MIN_HEIGHT); - } - PHAsset *asset = [assets firstObject]; - CGSize originalImageSize = CGSizeMake([asset pixelWidth], [asset pixelHeight]); - size = [self getMediaMessageSizefromOriginalSize:originalImageSize withWidth:width]; - //This fixes the image being too small. I think the issue comes form the fact that the display is retina. This should probably be changed in the future. - size.height += 40; - size.width -= CELL_MESSAGE_X_MARGIN; - - // add size for message text - size.height += textSize.height; - size.width = MAX(textSize.width, size.width); } - } + } - size.width = MAX(size.width + CELL_MESSAGE_X_MARGIN, CELL_MIN_WIDTH); - size.height = MAX(size.height + CELL_MESSAGE_Y_MARGIN, CELL_MIN_HEIGHT); - return size; + size.width = MAX(size.width + CELL_MESSAGE_X_MARGIN, CELL_MIN_WIDTH); + size.height = MAX(size.height + CELL_MESSAGE_Y_MARGIN, CELL_MIN_HEIGHT); + return size; } + + (CGSize)ViewSizeForMessage:(LinphoneChatMessage *)chat withWidth:(int)width { static UIFont *dateFont = nil; static CGSize dateViewSize;