Allow-pasting-image-option

This commit is contained in:
Swift Coder
2024-10-08 13:52:30 -04:00
parent 42f5af0ffd
commit ba9780fb17
4 changed files with 60 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ enum PreUploadedMedia {
enum MediaUpload {
case image(URL)
case uiImage(UIImage)
case video(URL)
var genericFileName: String {
@@ -30,6 +31,8 @@ enum MediaUpload {
return url.pathExtension
case .video(let url):
return url.pathExtension
case .uiImage(_):
return "jpeg"
}
}
@@ -39,6 +42,8 @@ enum MediaUpload {
return url
case .video(let url):
return url
case .uiImage(_):
return URL(string: "about:blank")! // Safe placeholder URL
}
}
@@ -72,6 +77,8 @@ enum MediaUpload {
return "image/jpg"
case .video:
return "video/mp4"
case .uiImage(_):
return "image/jpg"
}
}
}

View File

@@ -61,6 +61,8 @@ func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploa
} catch {
return .failed(error)
}
case .uiImage(_):
return .failed(nil) // no need to handle for uiimage case
}
guard let mediaData else {

View File

@@ -54,6 +54,8 @@ 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 imageUploadConfirmPasteboard: Bool = false
@State var references: [RefId] = []
@State var filtered_pubkeys: Set<Pubkey> = []
@State var focusWordAttributes: (String?, NSRange?) = (nil, nil)
@@ -246,7 +248,9 @@ struct PostView: View {
TextViewWrapper(
attributedText: $post,
textHeight: $textHeight,
initialTextSuffix: initial_text_suffix,
initialTextSuffix: initial_text_suffix,
imagePastedFromPasteboard: $imagePastedFromPasteboard,
imageUploadConfirmPasteboard: $imageUploadConfirmPasteboard,
cursorIndex: newCursorIndex,
getFocusWordForMention: { word, range in
focusWordAttributes = (word, range)
@@ -463,6 +467,15 @@ struct PostView: View {
self.attach_media = true
}
}
.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)) {
self.handle_upload(media: mediaToUpload)
}
}
Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
}
.onAppear() {
let loaded_draft = load_draft()

View File

@@ -12,13 +12,16 @@ struct TextViewWrapper: UIViewRepresentable {
@EnvironmentObject var tagModel: TagModel
@Binding var textHeight: CGFloat?
let initialTextSuffix: String?
@Binding var imagePastedFromPasteboard: UIImage?
@Binding var imageUploadConfirmPasteboard: Bool
let cursorIndex: Int?
var getFocusWordForMention: ((String?, NSRange?) -> Void)? = nil
let updateCursorPosition: ((Int) -> Void)
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
let textView = CustomPostTextView(imagePastedFromPasteboard: $imagePastedFromPasteboard,
imageUploadConfirm: $imageUploadConfirmPasteboard)
textView.backgroundColor = UIColor(DamusColors.adaptableWhite)
textView.delegate = context.coordinator
@@ -240,3 +243,36 @@ struct TextViewWrapper: UIViewRepresentable {
}
}
class CustomPostTextView: UITextView {
@Binding var imagePastedFromPasteboard: UIImage?
@Binding var imageUploadConfirm: Bool
// Custom initializer
init(imagePastedFromPasteboard: Binding<UIImage?>, imageUploadConfirm: Binding<Bool>) {
self._imagePastedFromPasteboard = imagePastedFromPasteboard
self._imageUploadConfirm = imageUploadConfirm
super.init(frame: .zero, textContainer: nil)
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
// Override canPerformAction to enable image pasting
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)),
UIPasteboard.general.image != nil {
return true // Show `Paste` option while long-pressing if there is an image present in the clipboard
}
return super.canPerformAction(action, withSender: sender) // Default behavior for other actions
}
// Override paste to handle image pasting
override func paste(_ sender: Any?) {
if let image = UIPasteboard.general.image {
imagePastedFromPasteboard = image
// Show alert view in PostView for Confirming upload
imageUploadConfirm = true
} else {
super.paste(sender) // Fall back to default paste behavior if no image
}
}
}