damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 866afe970b49ce1638dead4850cd2abf5821524f
parent 87efc915276a0e89b516d2a54f30e55f6b3d9225
Author: Swift Coder <scoder1747@gmail.com>
Date:   Tue, 26 Nov 2024 17:21:20 -0500

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 <scoder1747@gmail.com>

Diffstat:
Mdamus/Util/Constants.swift | 3+++
Mdamus/Views/PostView.swift | 4++--
Mdamus/Views/TextViewWrapper.swift | 31+++++++++++++++++++++++++------
3 files changed, 30 insertions(+), 8 deletions(-)

diff --git 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 @@ -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 @@ -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<UIImage?>, imageUploadConfirm: Binding<Bool>) { + init(imagePastedFromPasteboard: Binding<PreUploadedMedia?>, imageUploadConfirm: Binding<Bool>) { 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 } } }