damus

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

commit c83b0fba21951654455579fb3cf55b4ddbd08a90
parent b7053e8680e5bd69e9e9d6ba0b73bb79df842f03
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 13 Oct 2024 15:40:42 -0700

Merge 'paste images' support

Swift Coder (3):
      Allow-pasting-image-option
      Documenting
      Code optimization

Diffstat:
Mdamus/Views/PostView.swift | 16+++++++++++++++-
Mdamus/Views/TextViewWrapper.swift | 38+++++++++++++++++++++++++++++++++++++-
2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift @@ -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,16 @@ struct PostView: View { self.attach_media = true } } + // This alert seeks confirmation about Image-upload when user taps Paste option + .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() diff --git a/damus/Views/TextViewWrapper.swift b/damus/Views/TextViewWrapper.swift @@ -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 + } + } +}