Add gifs support

This commit is contained in:
Benoit Martins 2024-03-12 17:24:49 +01:00
parent af3a0fbd31
commit a822a0895d
3 changed files with 96 additions and 100 deletions

View file

@ -18,76 +18,15 @@
*/
import SwiftUI
import WebKit
struct ChatBubbleView: View {
@ObservedObject var conversationViewModel: ConversationViewModel
//let index: IndexPath
let message: Message
var body: some View {
/*
if index < conversationViewModel.conversationMessagesList.count
&& conversationViewModel.conversationMessagesList[index].eventLog.chatMessage != nil {
VStack {
if index == 0 && conversationViewModel.displayedConversationHistorySize > conversationViewModel.conversationMessagesList.count {
//if index % 30 == 29 && conversationViewModel.displayedConversationHistorySize > conversationViewModel.conversationMessagesList.count {
ProgressView()
.frame(idealWidth: .infinity, maxWidth: .infinity, alignment: .center)
.id(UUID())
}
HStack {
if conversationViewModel.conversationMessagesList[index].eventLog.chatMessage!.isOutgoing {
Spacer()
}
VStack {
Text(conversationViewModel.conversationMessagesList[index].eventLog.chatMessage!.utf8Text ?? "")
.foregroundStyle(Color.grayMain2c700)
.default_text_style(styleSize: 16)
}
.padding(.all, 15)
.background(conversationViewModel.conversationMessagesList[index].eventLog.chatMessage!.isOutgoing ? Color.orangeMain100 : Color.grayMain2c100)
.clipShape(RoundedRectangle(cornerRadius: 16))
if !conversationViewModel.conversationMessagesList[index].eventLog.chatMessage!.isOutgoing {
Spacer()
}
}
.padding(.leading, conversationViewModel.conversationMessagesList[index].eventLog.chatMessage!.isOutgoing ? 40 : 0)
.padding(.trailing, !conversationViewModel.conversationMessagesList[index].eventLog.chatMessage!.isOutgoing ? 40 : 0)
}
}
if conversationViewModel.conversationMessagesSection.count > index.section && conversationViewModel.conversationMessagesSection[index.section].rows.count > index.row {
VStack {
HStack {
if message.isOutgoing {
Spacer()
}
VStack {
Text(message.text
)
.foregroundStyle(Color.grayMain2c700)
.default_text_style(styleSize: 16)
}
.padding(.all, 15)
.background(message.isOutgoing ? Color.orangeMain100 : Color.grayMain2c100)
.clipShape(RoundedRectangle(cornerRadius: 16))
if !message.isOutgoing {
Spacer()
}
}
.padding(.leading, message.isOutgoing ? 40 : 0)
.padding(.trailing, !message.isOutgoing ? 40 : 0)
}
}
*/
var body: some View {
VStack {
HStack {
if message.isOutgoing {
@ -96,16 +35,45 @@ struct ChatBubbleView: View {
VStack {
if !message.attachments.isEmpty {
AsyncImage(url: message.attachments.first!.full) { image in
image.resizable()
.scaledToFill()
//.aspectRatio(1.5, contentMode: .fill)
//.clipped()
} placeholder: {
ProgressView()
if message.attachments.count == 1 {
let result = imageDimensions(url: message.attachments.first!.full.absoluteString)
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 / (UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 3 : UIScreen.main.bounds.width / 3)))
: (UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 3 : UIScreen.main.bounds.width / 3)
)
} else {
if result.0 < result.1 {
GifImageView(message.attachments.first!.full)
.clipShape(RoundedRectangle(cornerRadius: 4))
.frame(
width: result.1 > UIScreen.main.bounds.height / 3
? (result.0 / (result.0 / (UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 3 : UIScreen.main.bounds.width / 3)))
: result.0,
height: result.1 > UIScreen.main.bounds.height / 3
? (result.1 / (result.0 / (UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 3 : UIScreen.main.bounds.width / 3)))
: result.1
)
} else {
GifImageView(message.attachments.first!.full)
.clipShape(RoundedRectangle(cornerRadius: 4))
.frame(maxWidth: .infinity)
.frame(
height: result.1 / (result.0 / (UIScreen.main.bounds.height > UIScreen.main.bounds.width ? UIScreen.main.bounds.height / 3 : UIScreen.main.bounds.width / 3))
)
}
}
} else {
}
.frame(maxHeight: 400)
//.frame(width: 50, height: 50)
}
if !message.text.isEmpty {
@ -125,11 +93,58 @@ struct ChatBubbleView: View {
.padding(.leading, message.isOutgoing ? 40 : 0)
.padding(.trailing, !message.isOutgoing ? 40 : 0)
}
}
}
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? {
let pixelWidth = imageProperties[kCGImagePropertyPixelWidth] as? CGFloat
let pixelHeight = imageProperties[kCGImagePropertyPixelHeight] as? CGFloat
return (pixelWidth ?? 0, pixelHeight ?? 0)
}
}
return (0, 0)
}
}
enum URLType {
case name(String) // local file name of gif
case url(URL) // remote url
var url: URL? {
switch self {
case .name(let name):
return Bundle.main.url(forResource: name, withExtension: "gif")
case .url(let remoteURL):
return remoteURL
}
}
}
struct GifImageView: UIViewRepresentable {
private let name: URL
init(_ name: URL) {
self.name = name
}
func makeUIView(context: Context) -> WKWebView {
let webview = WKWebView()
let url = name
let data = try? Data(contentsOf: url)
if data != nil {
webview.load(data!, mimeType: "image/gif", characterEncodingName: "UTF-8", baseURL: url.deletingLastPathComponent())
webview.scrollView.isScrollEnabled = false
}
return webview
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.reload()
}
}
/*
#Preview {
ChatBubbleView(conversationViewModel: ConversationViewModel(), index: 0)
}
*/
#Preview {
ChatBubbleView(conversationViewModel: ConversationViewModel(), index: 0)
}
*/

View file

@ -22,6 +22,7 @@ import Foundation
public enum AttachmentType: String, Codable {
case image
case video
case gif
public var title: String {
switch self {

View file

@ -99,16 +99,6 @@ class ConversationViewModel: ObservableObject {
if self.displayedConversation != nil {
let historyEvents = self.displayedConversation!.chatRoom.getHistoryRangeEvents(begin: self.conversationMessagesList.count, end: self.conversationMessagesList.count + 30)
//For List
/*
historyEvents.reversed().forEach { eventLog in
DispatchQueue.main.async {
self.conversationMessagesList.append(LinphoneCustomEventLog(eventLog: eventLog))
}
}
*/
//For ScrollView
var conversationMessage: [Message] = []
historyEvents.enumerated().forEach { index, eventLog in
DispatchQueue.main.async {
@ -126,7 +116,7 @@ class ConversationViewModel: ObservableObject {
if content.filePath == nil || content.filePath!.isEmpty {
self.downloadContent(chatMessage: eventLog.chatMessage!, content: content)
} else {
let attachment = Attachment(id: UUID().uuidString, url: URL(string: "file://" + content.filePath!)!, type: .image)
let attachment = Attachment(id: UUID().uuidString, url: URL(string: self.getNewFilePath(name: content.name ?? ""))!, type: (content.name?.lowercased().hasSuffix("gif"))! ? .gif : .image)
attachmentList.append(attachment)
}
}
@ -350,18 +340,8 @@ class ConversationViewModel: ObservableObject {
}
}
func generateThumbnail(path: URL) -> UIImage? {
do {
let asset = AVURLAsset(url: path, options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
return thumbnail
} catch let error {
print("*** Error generating thumbnail: \(error.localizedDescription)")
return nil
}
func getNewFilePath(name: String) -> String {
return "file://" + Factory.Instance.getDownloadDir(context: nil) + name
}
}
struct LinphoneCustomEventLog: Hashable {