From cb8af8deeabf54a60b90d61be520b34f14de059c Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Mon, 13 May 2024 11:23:43 +0200 Subject: [PATCH] Fix image and gif size --- .../Fragments/ChatBubbleView.swift | 106 +++++++++--------- .../Fragments/ConversationsListFragment.swift | 20 ++-- 2 files changed, 60 insertions(+), 66 deletions(-) diff --git a/Linphone/UI/Main/Conversations/Fragments/ChatBubbleView.swift b/Linphone/UI/Main/Conversations/Fragments/ChatBubbleView.swift index 71d7d59bf..72efc4905 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ChatBubbleView.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ChatBubbleView.swift @@ -37,61 +37,7 @@ struct ChatBubbleView: View { VStack(alignment: message.isOutgoing ? .trailing : .leading) { if !message.attachments.isEmpty { - if message.attachments.count == 1 { - let result = imageDimensions(url: message.attachments.first!.full.absoluteString) - if message.attachments.first!.type == .image || message.attachments.first!.type == .gif { - if message.attachments.first!.type != .gif { - AsyncImage(url: message.attachments.first!.full) { image in - image.resizable() - .interpolation(.low) - .scaledToFit() - .clipShape(RoundedRectangle(cornerRadius: 4)) - } placeholder: { - ProgressView() - } - .frame( - height: result.0 > result.1 - ? result.1 / (result.0 / (geometryProxy.size.width - 80)) - : UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 2.5 : UIScreen.main.bounds.width / 2.5 - ) - } else { - if result.0 < result.1 { - GifImageView(message.attachments.first!.full) - .clipShape(RoundedRectangle(cornerRadius: 4)) - .frame( - width: result.1 > (UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 2.5 : UIScreen.main.bounds.width / 2.5) - ? result.0 / (result.1 / (UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 2.5 : UIScreen.main.bounds.width / 2.5)) - : result.0, - height: result.1 > (UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 2.5 : UIScreen.main.bounds.width / 2.5) - ? UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 2.5 : UIScreen.main.bounds.width / 2.5 - : result.1 - ) - } else { - GifImageView(message.attachments.first!.full) - .clipShape(RoundedRectangle(cornerRadius: 4)) - .frame( - height: result.1 / (result.0 / (geometryProxy.size.width - 80)) - ) - } - } - } else { - let result = imageDimensions(url: message.attachments.first!.full.absoluteString) - AsyncImage(url: message.attachments.first!.full) { image in - image.resizable() - .interpolation(.low) - .scaledToFit() - .clipShape(RoundedRectangle(cornerRadius: 4)) - } placeholder: { - ProgressView() - } - .frame( - height: result.0 > result.1 - ? result.1 / (result.0 / (geometryProxy.size.width - 80)) - : UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 2.5 : UIScreen.main.bounds.width / 2.5 - ) - } - } else { - } + messageAttachments() } if !message.text.isEmpty { @@ -113,6 +59,56 @@ struct ChatBubbleView: View { } } + @ViewBuilder + func messageAttachments() -> some View { + if message.attachments.count == 1 { + if message.attachments.first!.type == .image || message.attachments.first!.type == .gif { + let result = imageDimensions(url: message.attachments.first!.full.absoluteString) + ZStack { + Rectangle() + .fill(Color(.white)) + .aspectRatio(result.0/result.1, contentMode: .fit) + .if(result.0 < geometryProxy.size.width - 110) { view in + view.frame(maxWidth: result.0) + } + .if(result.1 < geometryProxy.size.height/2) { view in + view.frame(maxHeight: result.1) + } + .if(result.0 >= result.1 && result.0 >= geometryProxy.size.width - 110 && result.1 >= geometryProxy.size.height/2.5) { view in + view.frame( + maxWidth: geometryProxy.size.width - 110, + maxHeight: result.1 * ((geometryProxy.size.width - 110) / result.0) + ) + } + .if(result.0 < result.1 && result.1 >= geometryProxy.size.height/2.5) { view in + view.frame( + maxWidth: result.0 * ((geometryProxy.size.height/2.5) / result.1), + maxHeight: geometryProxy.size.height/2.5 + ) + } + + if message.attachments.first!.type == .image { + AsyncImage(url: message.attachments.first!.full) { image in + image + .resizable() + .interpolation(.medium) + .aspectRatio(contentMode: .fill) + } placeholder: { + ProgressView() + } + .layoutPriority(-1) + } else { + GifImageView(message.attachments.first!.full) + .layoutPriority(-1) + .clipShape(RoundedRectangle(cornerRadius: 4)) + } + } + .clipShape(RoundedRectangle(cornerRadius: 4)) + .clipped() + } + } + } + func imageDimensions(url: String) -> (CGFloat, CGFloat) { if let imageSource = CGImageSourceCreateWithURL(URL(string: url)! as CFURL, nil) { if let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary? { diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift index a49b22f71..de7a9476b 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationsListFragment.swift @@ -134,18 +134,16 @@ struct ConversationsListFragment: View { .listRowSeparator(.hidden) .background(.white) .onTapGesture { - withAnimation { - if conversationViewModel.displayedConversation != nil { - conversationViewModel.displayedConversation = nil - conversationViewModel.resetMessage() - conversationViewModel.changeDisplayedChatRoom(conversationModel: conversationsListViewModel.conversationsList[index]) - conversationViewModel.getMessages() - } else { - conversationViewModel.changeDisplayedChatRoom(conversationModel: conversationsListViewModel.conversationsList[index]) - } - conversationsListViewModel.conversationsList[index].markAsRead() - conversationsListViewModel.updateUnreadMessagesCount() + if conversationViewModel.displayedConversation != nil { + conversationViewModel.displayedConversation = nil + conversationViewModel.resetMessage() + conversationViewModel.changeDisplayedChatRoom(conversationModel: conversationsListViewModel.conversationsList[index]) + conversationViewModel.getMessages() + } else { + conversationViewModel.changeDisplayedChatRoom(conversationModel: conversationsListViewModel.conversationsList[index]) } + conversationsListViewModel.conversationsList[index].markAsRead() + conversationsListViewModel.updateUnreadMessagesCount() } .onLongPressGesture(minimumDuration: 0.2) { conversationsListViewModel.selectedConversation = conversationsListViewModel.conversationsList[index]