From 866afe970b49ce1638dead4850cd2abf5821524f Mon Sep 17 00:00:00 2001 From: Swift Coder Date: Tue, 26 Nov 2024 17:21:20 -0500 Subject: [PATCH] Paste Gif image similar to jpeg and png files This commit change will allow users to paste GIF file in the Post by copying from other apps (previously similar to pasting Jpeg and PNG image functionality) Changelog-Added: Paste Gif image similar to jpeg and png files Signed-off-by: Swift Coder --- damus/Util/Constants.swift | 3 +++ damus/Views/PostView.swift | 4 ++-- damus/Views/TextViewWrapper.swift | 31 +++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/damus/Util/Constants.swift b/damus/Util/Constants.swift index c5bd281b..3f6d7acd 100644 --- a/damus/Util/Constants.swift +++ b/damus/Util/Constants.swift @@ -31,4 +31,7 @@ class Constants { static let DAMUS_WEBSITE_LOCAL_TEST_URL: URL = URL(string: "http://localhost:3000")! static let DAMUS_WEBSITE_STAGING_URL: URL = URL(string: "https://staging.damus.io")! static let DAMUS_WEBSITE_PRODUCTION_URL: URL = URL(string: "https://damus.io")! + + // MARK: General constants + static let GIF_IMAGE_TYPE: String = "com.compuserve.gif" } diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index e2a7a30e..d950320a 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -57,7 +57,7 @@ struct PostView: View { @State var error: String? = nil @State var uploadedMedias: [UploadedMedia] = [] @State var image_upload_confirm: Bool = false - @State var imagePastedFromPasteboard: UIImage? = nil + @State var imagePastedFromPasteboard: PreUploadedMedia? = nil @State var imageUploadConfirmPasteboard: Bool = false @State var references: [RefId] = [] @State var imageUploadConfirmDamusShare: Bool = false @@ -503,7 +503,7 @@ struct PostView: View { .alert(NSLocalizedString("Are you sure you want to upload this media?", comment: "Alert message asking if the user wants to upload media."), isPresented: $imageUploadConfirmPasteboard) { Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) { if let image = imagePastedFromPasteboard, - let mediaToUpload = generateMediaUpload(PreUploadedMedia.uiimage(image)) { + let mediaToUpload = generateMediaUpload(image) { Task { await self.handle_upload(media: mediaToUpload) } diff --git a/damus/Views/TextViewWrapper.swift b/damus/Views/TextViewWrapper.swift index 71361661..756fef13 100644 --- a/damus/Views/TextViewWrapper.swift +++ b/damus/Views/TextViewWrapper.swift @@ -12,7 +12,7 @@ struct TextViewWrapper: UIViewRepresentable { @EnvironmentObject var tagModel: TagModel @Binding var textHeight: CGFloat? let initialTextSuffix: String? - @Binding var imagePastedFromPasteboard: UIImage? + @Binding var imagePastedFromPasteboard: PreUploadedMedia? @Binding var imageUploadConfirmPasteboard: Bool let cursorIndex: Int? @@ -244,11 +244,11 @@ struct TextViewWrapper: UIViewRepresentable { } class CustomPostTextView: UITextView { - @Binding var imagePastedFromPasteboard: UIImage? + @Binding var imagePastedFromPasteboard: PreUploadedMedia? @Binding var imageUploadConfirm: Bool // Custom initializer - init(imagePastedFromPasteboard: Binding, imageUploadConfirm: Binding) { + init(imagePastedFromPasteboard: Binding, imageUploadConfirm: Binding) { self._imagePastedFromPasteboard = imagePastedFromPasteboard self._imageUploadConfirm = imageUploadConfirm super.init(frame: .zero, textContainer: nil) @@ -267,12 +267,31 @@ class CustomPostTextView: UITextView { // Override paste to handle image pasting override func paste(_ sender: Any?) { - if let image = UIPasteboard.general.image { - imagePastedFromPasteboard = image + let pasteboard = UIPasteboard.general + + if let data = pasteboard.data(forPasteboardType: Constants.GIF_IMAGE_TYPE), + let url = saveGIFToTemporaryDirectory(data) { + imagePastedFromPasteboard = PreUploadedMedia.unprocessed_image(url) + imageUploadConfirm = true + } else if let image = pasteboard.image { + // handle .png, .jpeg files here + imagePastedFromPasteboard = PreUploadedMedia.uiimage(image) // Show alert view in PostView for Confirming upload imageUploadConfirm = true } else { - super.paste(sender) // Fall back to default paste behavior if no image + // fall back to default paste behavior if no image or gif file found + super.paste(sender) + } + } + + private func saveGIFToTemporaryDirectory(_ data: Data) -> URL? { + let tempDirectory = FileManager.default.temporaryDirectory + let gifURL = tempDirectory.appendingPathComponent("pasted_image.gif") + do { + try data.write(to: gifURL) + return gifURL + } catch { + return nil } } }