Support adding a caption to media uploads. (#3531)

* Add a composer to the MediaUploadPreviewScreen.

And send it's content to the media upload's caption.

* Use the new compound SendButton (updating relative padding in the toolbar).

* Update snapshots.

* Add unit tests for sending a caption.
This commit is contained in:
Doug
2024-11-19 16:35:01 +00:00
committed by GitHub
parent 352bb577ad
commit b75ad6a5aa
56 changed files with 530 additions and 284 deletions
@@ -15,6 +15,13 @@ struct MediaUploadPreviewScreenViewState: BindableState {
let url: URL
let title: String?
var shouldDisableInteraction = false
var bindings = MediaUploadPreviewScreenBindings()
}
struct MediaUploadPreviewScreenBindings: BindableState {
var caption = NSAttributedString()
var presendCallback: (() -> Void)?
}
enum MediaUploadPreviewScreenViewAction {
@@ -42,6 +42,9 @@ class MediaUploadPreviewScreenViewModel: MediaUploadPreviewScreenViewModelType,
}
override func process(viewAction: MediaUploadPreviewScreenViewAction) {
// Get the current caption before all the processing starts.
let caption = state.bindings.caption.nonBlankString
switch viewAction {
case .send:
Task {
@@ -51,7 +54,7 @@ class MediaUploadPreviewScreenViewModel: MediaUploadPreviewScreenViewModelType,
switch await mediaUploadingPreprocessor.processMedia(at: url) {
case .success(let mediaInfo):
switch await sendAttachment(mediaInfo: mediaInfo, progressSubject: progressSubject) {
switch await sendAttachment(mediaInfo: mediaInfo, caption: caption, progressSubject: progressSubject) {
case .success:
actionsSubject.send(.dismiss)
case .failure(let error):
@@ -75,20 +78,38 @@ class MediaUploadPreviewScreenViewModel: MediaUploadPreviewScreenViewModelType,
// MARK: - Private
private func sendAttachment(mediaInfo: MediaInfo, progressSubject: CurrentValueSubject<Double, Never>?) async -> Result<Void, TimelineProxyError> {
private func sendAttachment(mediaInfo: MediaInfo, caption: String?, progressSubject: CurrentValueSubject<Double, Never>?) async -> Result<Void, TimelineProxyError> {
let requestHandle: ((SendAttachmentJoinHandleProtocol) -> Void) = { [weak self] handle in
self?.requestHandle = handle
}
switch mediaInfo {
case let .image(imageURL, thumbnailURL, imageInfo):
return await roomProxy.timeline.sendImage(url: imageURL, thumbnailURL: thumbnailURL, imageInfo: imageInfo, progressSubject: progressSubject, requestHandle: requestHandle)
return await roomProxy.timeline.sendImage(url: imageURL,
thumbnailURL: thumbnailURL,
imageInfo: imageInfo,
caption: caption,
progressSubject: progressSubject,
requestHandle: requestHandle)
case let .video(videoURL, thumbnailURL, videoInfo):
return await roomProxy.timeline.sendVideo(url: videoURL, thumbnailURL: thumbnailURL, videoInfo: videoInfo, progressSubject: progressSubject, requestHandle: requestHandle)
return await roomProxy.timeline.sendVideo(url: videoURL,
thumbnailURL: thumbnailURL,
videoInfo: videoInfo,
caption: caption,
progressSubject: progressSubject,
requestHandle: requestHandle)
case let .audio(audioURL, audioInfo):
return await roomProxy.timeline.sendAudio(url: audioURL, audioInfo: audioInfo, progressSubject: progressSubject, requestHandle: requestHandle)
return await roomProxy.timeline.sendAudio(url: audioURL,
audioInfo: audioInfo,
caption: caption,
progressSubject: progressSubject,
requestHandle: requestHandle)
case let .file(fileURL, fileInfo):
return await roomProxy.timeline.sendFile(url: fileURL, fileInfo: fileInfo, progressSubject: progressSubject, requestHandle: requestHandle)
return await roomProxy.timeline.sendFile(url: fileURL,
fileInfo: fileInfo,
caption: caption,
progressSubject: progressSubject,
requestHandle: requestHandle)
}
}
@@ -118,3 +139,10 @@ class MediaUploadPreviewScreenViewModel: MediaUploadPreviewScreenViewModelType,
userIndicatorController.submitIndicator(UserIndicator(title: label))
}
}
extension NSAttributedString {
var nonBlankString: String? {
guard !string.isBlank else { return nil }
return string
}
}
@@ -5,6 +5,7 @@
// Please see LICENSE in the repository root for full details.
//
import Compound
import QuickLook
import SwiftUI
@@ -17,13 +18,20 @@ struct MediaUploadPreviewScreen: View {
var body: some View {
mainContent
.id(UUID())
.id(context.viewState.url)
.ignoresSafeArea(edges: [.horizontal])
.safeAreaInset(edge: .bottom, spacing: 0) {
composer
.padding(.horizontal, 12)
.padding(.vertical, 16)
.background() // Don't use compound so we match the QLPreviewController.
}
.navigationTitle(title)
.navigationBarTitleDisplayMode(.inline)
.disabled(context.viewState.shouldDisableInteraction)
.ignoresSafeArea(edges: [.horizontal, .bottom])
.toolbar { toolbar }
.disabled(context.viewState.shouldDisableInteraction)
.interactiveDismissDisabled()
.preferredColorScheme(.dark)
}
@ViewBuilder
@@ -38,18 +46,31 @@ struct MediaUploadPreviewScreen: View {
}
}
private var composer: some View {
HStack(spacing: 12) {
MessageComposerTextField(placeholder: L10n.richTextEditorComposerCaptionPlaceholder,
text: $context.caption,
presendCallback: $context.presendCallback,
maxHeight: ComposerConstant.maxHeight,
keyHandler: { _ in },
pasteHandler: { _ in })
.messageComposerStyle()
SendButton {
context.send(viewAction: .send)
}
}
}
@ToolbarContentBuilder
private var toolbar: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
Button { context.send(viewAction: .cancel) } label: {
Text(L10n.actionCancel)
}
}
ToolbarItem(placement: .confirmationAction) {
Button { context.send(viewAction: .send) } label: {
Text(L10n.actionSend)
}
.disabled(context.viewState.shouldDisableInteraction)
// Fix a bug with the preferredColorScheme on iOS 18 where the button doesn't
// follow the dark colour scheme on devices running with dark mode disabled.
.tint(.compound.textActionPrimary)
}
}
}
@@ -111,21 +132,6 @@ private class PreviewItem: NSObject, QLPreviewItem {
}
}
// MARK: - Previews
struct MediaUploadPreviewScreen_Previews: PreviewProvider, TestablePreview {
static let viewModel = MediaUploadPreviewScreenViewModel(userIndicatorController: UserIndicatorControllerMock.default,
roomProxy: JoinedRoomProxyMock(),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings),
title: "some random file name",
url: URL.picturesDirectory)
static var previews: some View {
NavigationStack {
MediaUploadPreviewScreen(context: viewModel.context)
}
}
}
private class PreviewViewController: QLPreviewController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
@@ -137,3 +143,21 @@ private class PreviewViewController: QLPreviewController {
toolbarItems?.first?.isHidden = true
}
}
// MARK: - Previews
struct MediaUploadPreviewScreen_Previews: PreviewProvider, TestablePreview {
static let snapshotURL = URL.picturesDirectory
static let testURL = Bundle.main.url(forResource: "AppIcon60x60@2x", withExtension: "png")
static let viewModel = MediaUploadPreviewScreenViewModel(userIndicatorController: UserIndicatorControllerMock.default,
roomProxy: JoinedRoomProxyMock(),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings),
title: "App Icon.png",
url: snapshotURL)
static var previews: some View {
NavigationStack {
MediaUploadPreviewScreen(context: viewModel.context)
}
}
}