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>
This commit is contained in:
committed by
Daniel D’Aquino
parent
87efc91527
commit
866afe970b
@@ -31,4 +31,7 @@ class Constants {
|
|||||||
static let DAMUS_WEBSITE_LOCAL_TEST_URL: URL = URL(string: "http://localhost:3000")!
|
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_STAGING_URL: URL = URL(string: "https://staging.damus.io")!
|
||||||
static let DAMUS_WEBSITE_PRODUCTION_URL: URL = URL(string: "https://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"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ struct PostView: View {
|
|||||||
@State var error: String? = nil
|
@State var error: String? = nil
|
||||||
@State var uploadedMedias: [UploadedMedia] = []
|
@State var uploadedMedias: [UploadedMedia] = []
|
||||||
@State var image_upload_confirm: Bool = false
|
@State var image_upload_confirm: Bool = false
|
||||||
@State var imagePastedFromPasteboard: UIImage? = nil
|
@State var imagePastedFromPasteboard: PreUploadedMedia? = nil
|
||||||
@State var imageUploadConfirmPasteboard: Bool = false
|
@State var imageUploadConfirmPasteboard: Bool = false
|
||||||
@State var references: [RefId] = []
|
@State var references: [RefId] = []
|
||||||
@State var imageUploadConfirmDamusShare: Bool = false
|
@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) {
|
.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) {
|
Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
|
||||||
if let image = imagePastedFromPasteboard,
|
if let image = imagePastedFromPasteboard,
|
||||||
let mediaToUpload = generateMediaUpload(PreUploadedMedia.uiimage(image)) {
|
let mediaToUpload = generateMediaUpload(image) {
|
||||||
Task {
|
Task {
|
||||||
await self.handle_upload(media: mediaToUpload)
|
await self.handle_upload(media: mediaToUpload)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ struct TextViewWrapper: UIViewRepresentable {
|
|||||||
@EnvironmentObject var tagModel: TagModel
|
@EnvironmentObject var tagModel: TagModel
|
||||||
@Binding var textHeight: CGFloat?
|
@Binding var textHeight: CGFloat?
|
||||||
let initialTextSuffix: String?
|
let initialTextSuffix: String?
|
||||||
@Binding var imagePastedFromPasteboard: UIImage?
|
@Binding var imagePastedFromPasteboard: PreUploadedMedia?
|
||||||
@Binding var imageUploadConfirmPasteboard: Bool
|
@Binding var imageUploadConfirmPasteboard: Bool
|
||||||
|
|
||||||
let cursorIndex: Int?
|
let cursorIndex: Int?
|
||||||
@@ -244,11 +244,11 @@ struct TextViewWrapper: UIViewRepresentable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class CustomPostTextView: UITextView {
|
class CustomPostTextView: UITextView {
|
||||||
@Binding var imagePastedFromPasteboard: UIImage?
|
@Binding var imagePastedFromPasteboard: PreUploadedMedia?
|
||||||
@Binding var imageUploadConfirm: Bool
|
@Binding var imageUploadConfirm: Bool
|
||||||
|
|
||||||
// Custom initializer
|
// Custom initializer
|
||||||
init(imagePastedFromPasteboard: Binding<UIImage?>, imageUploadConfirm: Binding<Bool>) {
|
init(imagePastedFromPasteboard: Binding<PreUploadedMedia?>, imageUploadConfirm: Binding<Bool>) {
|
||||||
self._imagePastedFromPasteboard = imagePastedFromPasteboard
|
self._imagePastedFromPasteboard = imagePastedFromPasteboard
|
||||||
self._imageUploadConfirm = imageUploadConfirm
|
self._imageUploadConfirm = imageUploadConfirm
|
||||||
super.init(frame: .zero, textContainer: nil)
|
super.init(frame: .zero, textContainer: nil)
|
||||||
@@ -267,12 +267,31 @@ class CustomPostTextView: UITextView {
|
|||||||
|
|
||||||
// Override paste to handle image pasting
|
// Override paste to handle image pasting
|
||||||
override func paste(_ sender: Any?) {
|
override func paste(_ sender: Any?) {
|
||||||
if let image = UIPasteboard.general.image {
|
let pasteboard = UIPasteboard.general
|
||||||
imagePastedFromPasteboard = image
|
|
||||||
|
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
|
// Show alert view in PostView for Confirming upload
|
||||||
imageUploadConfirm = true
|
imageUploadConfirm = true
|
||||||
} else {
|
} 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user