From c97dcc18f2dfd5a2d58031c79b6a58af69196cfe Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Fri, 13 Dec 2024 15:38:23 +0200 Subject: [PATCH] Simplify how we build bubbles for previewing purposes, make use of the new bubbleBackground modifier. --- .../AudioFileEventsTimelineView.swift | 4 +-- .../FileMediaEventsTimelineView.swift | 4 +-- .../VoiceMessageMediaEventsTimelineView.swift | 4 +-- .../RoomScreen/View/SwipeRightAction.swift | 10 ++----- .../View/Style/LongPressWithFeedback.swift | 4 +-- .../Style/TimelineItemBubbleBackground.swift | 7 ++--- .../TimelineItemViews/FormattedBodyText.swift | 26 ++++--------------- 7 files changed, 15 insertions(+), 44 deletions(-) diff --git a/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/AudioFileEventsTimelineView.swift b/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/AudioFileEventsTimelineView.swift index ab65d2e46..7786bc799 100644 --- a/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/AudioFileEventsTimelineView.swift +++ b/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/AudioFileEventsTimelineView.swift @@ -21,9 +21,7 @@ struct AudioMediaEventsTimelineView: View { isAudioFile: true) .accessibilityLabel(L10n.commonAudio) .frame(maxWidth: .infinity, alignment: .leading) - .bubbleBackground(isOutgoing: timelineItem.isOutgoing, - insets: .init(top: 8, leading: 12, bottom: 8, trailing: 12), - color: .compound.bgSubtleSecondary) + .bubbleBackground(isOutgoing: timelineItem.isOutgoing) } } diff --git a/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/FileMediaEventsTimelineView.swift b/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/FileMediaEventsTimelineView.swift index 37506e4d7..30d9251fe 100644 --- a/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/FileMediaEventsTimelineView.swift +++ b/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/FileMediaEventsTimelineView.swift @@ -20,9 +20,7 @@ struct FileMediaEventsTimelineView: View { additionalWhitespaces: timelineItem.additionalWhitespaces()) .accessibilityLabel(L10n.commonFile) .frame(maxWidth: .infinity, alignment: .leading) - .bubbleBackground(isOutgoing: timelineItem.isOutgoing, - insets: .init(top: 8, leading: 12, bottom: 8, trailing: 12), - color: .compound.bgSubtleSecondary) + .bubbleBackground(isOutgoing: timelineItem.isOutgoing) } } diff --git a/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/VoiceMessageMediaEventsTimelineView.swift b/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/VoiceMessageMediaEventsTimelineView.swift index 49e7bede4..3443f9783 100644 --- a/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/VoiceMessageMediaEventsTimelineView.swift +++ b/ElementX/Sources/Screens/MediaEventsTimelineScreen/View/TimelineViews/VoiceMessageMediaEventsTimelineView.swift @@ -17,9 +17,7 @@ struct VoiceMessageMediaEventsTimelineView: View { playerState: playerState) .accessibilityLabel(L10n.commonVoiceMessage) .frame(maxWidth: .infinity, alignment: .leading) - .bubbleBackground(isOutgoing: timelineItem.isOutgoing, - insets: .init(top: 8, leading: 12, bottom: 8, trailing: 12), - color: .compound.bgSubtleSecondary) + .bubbleBackground(isOutgoing: timelineItem.isOutgoing) } } diff --git a/ElementX/Sources/Screens/RoomScreen/View/SwipeRightAction.swift b/ElementX/Sources/Screens/RoomScreen/View/SwipeRightAction.swift index df0a3f5df..ae30004ee 100644 --- a/ElementX/Sources/Screens/RoomScreen/View/SwipeRightAction.swift +++ b/ElementX/Sources/Screens/RoomScreen/View/SwipeRightAction.swift @@ -137,7 +137,8 @@ struct SwipeRightAction_Previews: PreviewProvider, TestablePreview { NavigationStack { ScrollView { VStack(alignment: .leading, spacing: 2) { - mockBubble("This is a message from somebody with a couple of lines of text.") + Text("This is a message from somebody with a couple of lines of text.") + .bubbleBackground() .swipeRightAction { Image(systemName: "flame") } shouldStartAction: { @@ -156,12 +157,5 @@ struct SwipeRightAction_Previews: PreviewProvider, TestablePreview { .presentationDetents([.medium]) } } - - func mockBubble(_ body: String) -> some View { - Text(body) - .padding(.horizontal, 12) - .padding(.vertical, 4) - .background(Color.compound._bgBubbleOutgoing, in: RoundedRectangle(cornerRadius: 12)) - } } } diff --git a/ElementX/Sources/Screens/Timeline/View/Style/LongPressWithFeedback.swift b/ElementX/Sources/Screens/Timeline/View/Style/LongPressWithFeedback.swift index 357960c31..1b2c20777 100644 --- a/ElementX/Sources/Screens/Timeline/View/Style/LongPressWithFeedback.swift +++ b/ElementX/Sources/Screens/Timeline/View/Style/LongPressWithFeedback.swift @@ -95,9 +95,7 @@ struct LongPressWithFeedback_Previews: PreviewProvider, TestablePreview { func mockBubble(_ body: String) -> some View { Text(body) - .padding(.horizontal, 12) - .padding(.vertical, 4) - .background(Color.compound._bgBubbleOutgoing, in: RoundedRectangle(cornerRadius: 12)) + .bubbleBackground() .contentShape(.contextMenuPreview, RoundedRectangle(cornerRadius: 12)) .onTapGesture { /* Fix long press gesture blocking the scroll view */ } } diff --git a/ElementX/Sources/Screens/Timeline/View/Style/TimelineItemBubbleBackground.swift b/ElementX/Sources/Screens/Timeline/View/Style/TimelineItemBubbleBackground.swift index 22704669e..a00a1c4d4 100644 --- a/ElementX/Sources/Screens/Timeline/View/Style/TimelineItemBubbleBackground.swift +++ b/ElementX/Sources/Screens/Timeline/View/Style/TimelineItemBubbleBackground.swift @@ -5,12 +5,13 @@ // Please see LICENSE in the repository root for full details. // +import Compound import SwiftUI extension View { - func bubbleBackground(isOutgoing: Bool, - insets: EdgeInsets, - color: Color? = nil) -> some View { + func bubbleBackground(isOutgoing: Bool = true, + insets: EdgeInsets = .init(top: 8, leading: 12, bottom: 8, trailing: 12), + color: Color? = .compound.bgSubtleSecondary) -> some View { modifier(TimelineItemBubbleBackgroundModifier(isOutgoing: isOutgoing, insets: insets, color: color)) diff --git a/ElementX/Sources/Screens/Timeline/View/TimelineItemViews/FormattedBodyText.swift b/ElementX/Sources/Screens/Timeline/View/TimelineItemViews/FormattedBodyText.swift index 4790ee5ef..565d74f69 100644 --- a/ElementX/Sources/Screens/Timeline/View/TimelineItemViews/FormattedBodyText.swift +++ b/ElementX/Sources/Screens/Timeline/View/TimelineItemViews/FormattedBodyText.swift @@ -189,18 +189,18 @@ struct FormattedBodyText_Previews: PreviewProvider, TestablePreview { ForEach(htmlStrings, id: \.self) { htmlString in if let attributedString = attributedStringBuilder.fromHTML(htmlString) { FormattedBodyText(attributedString: attributedString) - .previewBubble() + .bubbleBackground() } } FormattedBodyText(attributedString: AttributedString("Some plain text wrapped in an AttributedString.")) - .previewBubble() + .bubbleBackground() FormattedBodyText(text: "Some plain text that's not an attributed component.") - .previewBubble() + .bubbleBackground() FormattedBodyText(text: "Some plain text that's not an attributed component. This one is really long.") - .previewBubble() + .bubbleBackground() FormattedBodyText(text: "❤️", boostEmojiSize: true) - .previewBubble() + .bubbleBackground() } .padding() } @@ -208,19 +208,3 @@ struct FormattedBodyText_Previews: PreviewProvider, TestablePreview { .snapshotPreferences(delay: 0.25) } } - -private struct PreviewBubbleModifier: ViewModifier { - func body(content: Content) -> some View { - content - .padding(8) - .background(Color.compound._bgBubbleOutgoing) - .cornerRadius(12) - .environmentObject(TimelineViewModel.mock.context) - } -} - -private extension View { - func previewBubble() -> some View { - modifier(PreviewBubbleModifier()) - } -}