8000 Allow pasting images to the composer by nuno-vieira · Pull Request #797 · GetStream/stream-chat-swiftui · GitHub
[go: up one dir, main page]

Skip to content

Allow pasting images to the composer #797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### 🔄 Changed
### ✅ Added
- Allow pasting images to the composer [#797](https://github.com/GetStream/stream-chat-swiftui/pull/797)

# [4.76.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.76.0)
_March 31, 2025_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import UIKit

/// SwiftUI wrapper for a text field with multiple rows.
struct ComposerTextInputView: UIViewRepresentable {

@EnvironmentObject var composerViewModel: MessageComposerViewModel
@Injected(\.utils) private var utils

@Binding var text: String
Expand All @@ -33,6 +33,9 @@ struct ComposerTextInputView: UIViewRepresentable {
inputTextView.placeholderLabel.text = placeholder
inputTextView.contentInsetAdjustmentBehavior = .never
inputTextView.setContentCompressionResistancePriority(.streamLow, for: .horizontal)
inputTextView.onImagePasted = {
composerViewModel.imagePasted($0)
}

if utils.messageListConfig.becomesFirstResponderOnOpen {
inputTextView.becomeFirstResponder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ public struct ComposerInputView<Factory: ViewFactory>: View, KeyboardReadable {
maxMessageLength: maxMessageLength,
currentHeight: textFieldHeight
)
.environmentObject(viewModel)
.accessibilityIdentifier("ComposerTextInputView")
.accessibilityElement(children: .contain)
.frame(height: textFieldHeight)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,20 @@ open class MessageComposerViewModel: ObservableObject {
addedAssets = images
}

public func imagePasted(_ image: UIImage) {
guard let imageURL = try? image.saveAsJpgToTemporaryUrl() else {
log.error("Failed to write image to local temporary file")
return
}
let addedImage = AddedAsset(
image: image,
id: UUID().uuidString,
url: imageURL,
type: .image
)
addedAssets.append(addedImage)
}

public func removeAttachment(with id: String) {
if id.isURL, let url = URL(string: id) {
var urls = [URL]()
Expand Down
15 changes: 15 additions & 0 deletions Sources/StreamChatSwiftUI/Utils/Common/InputTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class InputTextView: UITextView, AccessibilityView {
}
}
}

var onImagePasted: ((UIImage) -> Void)?

override open func didMoveToSuperview() {
super.didMoveToSuperview()
Expand Down Expand Up @@ -143,9 +145,22 @@ class InputTextView: UITextView, AccessibilityView {

override open func paste(_ sender: Any?) {
super.paste(sender)
if let pastedImage = UIPasteboard.general.image,
let onImagePasted {
onImagePasted(pastedImage)
return
}
handleTextChange()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
self?.scrollToBottom()
}
}

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(paste(_:)) && onImagePasted != nil && UIPasteboard.general.image != nil {
return true
} else {
return super.canPerformAction(action, withSender: sender)
}
}
}
Loading
0