Skip to content

Commit

Permalink
Enable local echoes for media uploads on development builds. (#3514)
Browse files Browse the repository at this point in the history
* Enable local echoes for media uploads on development builds.
* Use media sizes when loading timeline items so they can take advantage of the cached local echo data.
  • Loading branch information
stefanceriu authored Nov 14, 2024
1 parent 5aadf4e commit 7e192b7
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class MediaUploadPreviewScreenViewModel: MediaUploadPreviewScreenViewModelType,
switch viewAction {
case .send:
Task {
let progressSubject = CurrentValueSubject<Double, Never>(0.0)
let progressSubject = AppSettings.isDevelopmentBuild ? nil : CurrentValueSubject<Double, Never>(0.0)

startLoading(progressPublisher: progressSubject.asCurrentValuePublisher())
startLoading(progressPublisher: progressSubject?.asCurrentValuePublisher())

switch await mediaUploadingPreprocessor.processMedia(at: url) {
case .success(let mediaInfo):
Expand Down Expand Up @@ -75,7 +75,7 @@ class MediaUploadPreviewScreenViewModel: MediaUploadPreviewScreenViewModelType,

// MARK: - Private

private func sendAttachment(mediaInfo: MediaInfo, progressSubject: CurrentValueSubject<Double, Never>) async -> Result<Void, TimelineProxyError> {
private func sendAttachment(mediaInfo: MediaInfo, progressSubject: CurrentValueSubject<Double, Never>?) async -> Result<Void, TimelineProxyError> {
let requestHandle: ((SendAttachmentJoinHandleProtocol) -> Void) = { [weak self] handle in
self?.requestHandle = handle
}
Expand All @@ -94,10 +94,16 @@ class MediaUploadPreviewScreenViewModel: MediaUploadPreviewScreenViewModelType,

private static let loadingIndicatorIdentifier = "\(MediaUploadPreviewScreenViewModel.self)-Loading"

private func startLoading(progressPublisher: CurrentValuePublisher<Double, Never>) {
private func startLoading(progressPublisher: CurrentValuePublisher<Double, Never>?) {
let progress: UserIndicator.Progress = if let progressPublisher {
.published(progressPublisher)
} else {
.indeterminate
}

userIndicatorController.submitIndicator(
UserIndicator(id: Self.loadingIndicatorIdentifier,
type: .modal(progress: .published(progressPublisher), interactiveDismissDisabled: false, allowsInteraction: true),
type: .modal(progress: progress, interactiveDismissDisabled: false, allowsInteraction: true),
title: L10n.commonSending,
persistent: true)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@ struct ImageRoomTimelineView: View {
var body: some View {
TimelineStyler(timelineItem: timelineItem) {
VStack(alignment: .leading, spacing: 4) {
LoadableImage(mediaSource: source,
mediaType: .timelineItem,
blurhash: timelineItem.content.blurhash,
mediaProvider: context.mediaProvider) {
placeholder
}
.timelineMediaFrame(height: timelineItem.content.height,
aspectRatio: timelineItem.content.aspectRatio)
.accessibilityElement(children: .ignore)
.accessibilityLabel(L10n.commonImage)
// This clip shape is distinct from the one in the styler as that one
// operates on the entire message so wouldn't round the bottom corners.
.clipShape(RoundedRectangle(cornerRadius: hasMediaCaption ? 6 : 0))
loadableImage
.accessibilityElement(children: .ignore)
.accessibilityLabel(L10n.commonImage)
// This clip shape is distinct from the one in the styler as that one
// operates on the entire message so wouldn't round the bottom corners.
.clipShape(RoundedRectangle(cornerRadius: hasMediaCaption ? 6 : 0))

if let attributedCaption = timelineItem.content.formattedCaption {
FormattedBodyText(attributedString: attributedCaption,
Expand All @@ -44,15 +37,32 @@ struct ImageRoomTimelineView: View {
}
}

var source: MediaSourceProxy {
guard timelineItem.content.contentType != .gif, let thumbnailSource = timelineItem.content.thumbnailSource else {
return timelineItem.content.source
@ViewBuilder
private var loadableImage: some View {
if timelineItem.content.contentType == .gif {
LoadableImage(mediaSource: timelineItem.content.source,
mediaType: .timelineItem,
blurhash: timelineItem.content.blurhash,
size: timelineItem.content.size,
mediaProvider: context.mediaProvider) {
placeholder
}
.timelineMediaFrame(height: timelineItem.content.thumbnailSize?.height,
aspectRatio: timelineItem.content.aspectRatio)
} else {
LoadableImage(mediaSource: timelineItem.content.thumbnailSource ?? timelineItem.content.source,
mediaType: .timelineItem,
blurhash: timelineItem.content.blurhash,
size: timelineItem.content.thumbnailSize ?? timelineItem.content.size,
mediaProvider: context.mediaProvider) {
placeholder
}
.timelineMediaFrame(height: timelineItem.content.thumbnailSize?.height ?? timelineItem.content.size?.height,
aspectRatio: timelineItem.content.thumbnailAspectRatio ?? timelineItem.content.aspectRatio)
}

return thumbnailSource
}

var placeholder: some View {
private var placeholder: some View {
Rectangle()
.foregroundColor(timelineItem.isOutgoing ? .compound._bgBubbleOutgoing : .compound._bgBubbleIncoming)
.opacity(0.3)
Expand Down Expand Up @@ -100,8 +110,8 @@ struct ImageRoomTimelineView_Previews: PreviewProvider, TestablePreview {
sender: .init(id: "Bob"),
content: .init(filename: "Blurhashed.jpg",
source: MediaSourceProxy(url: .picturesDirectory, mimeType: "image/gif"),
thumbnailSource: nil,
aspectRatio: 0.7,
thumbnailSource: nil,
blurhash: "L%KUc%kqS$RP?Ks,WEf8OlrqaekW",
contentType: .gif)))

Expand All @@ -115,10 +125,9 @@ struct ImageRoomTimelineView_Previews: PreviewProvider, TestablePreview {
content: .init(filename: "Blurhashed.jpg",
caption: "This is a great image 😎",
source: MediaSourceProxy(url: .picturesDirectory, mimeType: "image/png"),
thumbnailSource: nil,
width: 50,
height: 50,
size: .init(width: 50, height: 50),
aspectRatio: 1,
thumbnailSource: nil,
blurhash: "L%KUc%kqS$RP?Ks,WEf8OlrqaekW",
contentType: .gif)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ struct StickerRoomTimelineView: View {
LoadableImage(url: timelineItem.imageURL,
mediaType: .timelineItem,
blurhash: timelineItem.blurhash,
size: timelineItem.size,
mediaProvider: context.mediaProvider) {
placeholder
}
.timelineMediaFrame(height: timelineItem.height,
.timelineMediaFrame(height: timelineItem.size?.height,
aspectRatio: timelineItem.aspectRatio)
.accessibilityElement(children: .ignore)
.accessibilityLabel("\(L10n.commonSticker), \(timelineItem.body)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct VideoRoomTimelineView: View {
TimelineStyler(timelineItem: timelineItem) {
VStack(alignment: .leading, spacing: 4) {
thumbnail
.timelineMediaFrame(height: timelineItem.content.height,
.timelineMediaFrame(height: timelineItem.content.thumbnailSize?.height,
aspectRatio: timelineItem.content.aspectRatio)
.accessibilityElement(children: .ignore)
.accessibilityLabel(L10n.commonVideo)
Expand All @@ -45,6 +45,7 @@ struct VideoRoomTimelineView: View {
LoadableImage(mediaSource: thumbnailSource,
mediaType: .timelineItem,
blurhash: timelineItem.content.blurhash,
size: timelineItem.content.thumbnailSize,
mediaProvider: context.mediaProvider) { imageView in
imageView
.overlay { playIcon }
Expand Down Expand Up @@ -114,8 +115,8 @@ struct VideoRoomTimelineView_Previews: PreviewProvider, TestablePreview {
content: .init(filename: "Blurhashed.mp4",
duration: 23,
source: nil,
thumbnailSource: nil,
aspectRatio: 0.7,
thumbnailSource: nil,
blurhash: "L%KUc%kqS$RP?Ks,WEf8OlrqaekW")))

VideoRoomTimelineView(timelineItem: VideoRoomTimelineItem(id: .randomEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,9 @@ enum RoomTimelineItemFixtures {
content: .init(filename: "video.mp4",
duration: 100,
source: .init(url: .picturesDirectory, mimeType: nil),
thumbnailSource: .init(url: .picturesDirectory, mimeType: nil),
width: 1920,
height: 1080,
size: .init(width: 1920, height: 1080),
aspectRatio: 1.78,
thumbnailSource: .init(url: .picturesDirectory, mimeType: nil),
blurhash: "KtI~70X5V?yss9oyrYs:t6")),
ImageRoomTimelineItem(id: .randomEvent,
timestamp: "10:47 am",
Expand All @@ -274,10 +273,9 @@ enum RoomTimelineItemFixtures {
sender: .init(id: ""),
content: .init(filename: "image.jpg",
source: .init(url: .picturesDirectory, mimeType: nil),
thumbnailSource: nil,
width: 5120,
height: 3412,
size: .init(width: 5120, height: 3412),
aspectRatio: 1.5,
thumbnailSource: nil,
blurhash: "KpE4oyayR5|GbHb];3j@of"))
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ struct ImageRoomTimelineItemContent: Hashable {
/// The original textual representation of the formatted caption directly from the event (usually HTML code)
var formattedCaptionHTMLString: String?
let source: MediaSourceProxy
let thumbnailSource: MediaSourceProxy?
var width: CGFloat?
var height: CGFloat?
var size: CGSize?
var aspectRatio: CGFloat?

let thumbnailSource: MediaSourceProxy?
var thumbnailSize: CGSize?
var thumbnailAspectRatio: CGFloat?

var blurhash: String?
var contentType: UTType?
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ struct VideoRoomTimelineItemContent: Hashable {
/// The original textual representation of the formatted caption directly from the event (usually HTML code)
var formattedCaptionHTMLString: String?
let duration: TimeInterval

let source: MediaSourceProxy?
let thumbnailSource: MediaSourceProxy?
var width: CGFloat?
var height: CGFloat?
var size: CGSize?
var aspectRatio: CGFloat?

let thumbnailSource: MediaSourceProxy?
var thumbnailSize: CGSize?
var thumbnailAspectRatio: CGFloat?

var blurhash: String?
var contentType: UTType?
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ struct StickerRoomTimelineItem: EventBasedTimelineItemProtocol, Equatable {

let imageURL: URL

var width: CGFloat?
var height: CGFloat?
var size: CGSize?
var aspectRatio: CGFloat?
var blurhash: String?

Expand Down
Loading

0 comments on commit 7e192b7

Please sign in to comment.