Skip to content
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

Remove layout styling abstraction #2982

Merged
merged 1 commit into from
Jun 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ struct RoomScreenViewState: BindableState {
var typingMembers: [String] = []
var showLoading = false
var showReadReceipts = false
let timelineStyle = TimelineStyle.bubbles
var isEncryptedOneToOneRoom = false
var timelineViewState: TimelineViewState // check the doc before changing this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ struct RoomScreen: View {
TimelineView()
.id(context.viewState.roomID)
.environmentObject(context)
.environment(\.timelineStyle, context.viewState.timelineStyle)
.environment(\.focussedEventID, context.viewState.timelineViewState.focussedEvent?.eventID)
.overlay(alignment: .bottomTrailing) {
scrollToBottomButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct TimelineItemBubbledStylerView<Content: View>: View {
.padding(.leading, bubbleAvatarPadding)
}
}
.padding(TimelineStyle.bubbles.rowInsets)
.padding(EdgeInsets(top: 1, leading: 8, bottom: 1, trailing: 8))
.highlightedTimelineItem(isFocussed)
}

Expand Down Expand Up @@ -532,7 +532,6 @@ struct TimelineItemBubbledStylerView_Previews: PreviewProvider, TestablePreview
}
}
}
.environment(\.timelineStyle, .bubbles)
.environmentObject(viewModel.context)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,6 @@
import Foundation
import SwiftUI

enum TimelineStyle: String, CaseIterable, Codable {
case bubbles

/// List row insets for a timeline
var rowInsets: EdgeInsets {
switch self {
case .bubbles:
return EdgeInsets(top: 1, leading: 8, bottom: 1, trailing: 8)
}
}

/// Short hand for `self == .bubbles`
var isBubbles: Bool {
switch self {
case .bubbles:
return true
}
}
}

enum TimelineGroupStyle: Hashable {
case single
case first
Expand All @@ -55,20 +35,11 @@ enum TimelineGroupStyle: Hashable {

// MARK: - Environment

private struct TimelineStyleKey: EnvironmentKey {
static let defaultValue = TimelineStyle.bubbles
}

private struct TimelineGroupStyleKey: EnvironmentKey {
static let defaultValue = TimelineGroupStyle.single
}

extension EnvironmentValues {
var timelineStyle: TimelineStyle {
get { self[TimelineStyleKey.self] }
set { self[TimelineStyleKey.self] = newValue }
}

var timelineGroupStyle: TimelineGroupStyle {
get { self[TimelineGroupStyleKey.self] }
set { self[TimelineGroupStyleKey.self] = newValue }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import SwiftUI
// MARK: - TimelineStyler

struct TimelineStyler<Content: View>: View {
Velin92 marked this conversation as resolved.
Show resolved Hide resolved
@Environment(\.timelineStyle) private var style

let timelineItem: EventBasedTimelineItemProtocol
@ViewBuilder let content: () -> Content

Expand Down Expand Up @@ -59,10 +57,7 @@ struct TimelineStyler<Content: View>: View {

@ViewBuilder
var mainContent: some View {
switch style {
case .bubbles:
TimelineItemBubbledStylerView(timelineItem: timelineItem, adjustedDeliveryStatus: adjustedDeliveryStatus, content: content)
}
TimelineItemBubbledStylerView(timelineItem: timelineItem, adjustedDeliveryStatus: adjustedDeliveryStatus, content: content)
}
}

Expand Down Expand Up @@ -200,17 +195,14 @@ struct TimelineItemStyler_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
testView
.environmentObject(viewModel.context)
.environment(\.timelineStyle, .bubbles)
.previewDisplayName("Bubbles")

languagesTestView
.environmentObject(viewModel.context)
.environment(\.timelineStyle, .bubbles)
.previewDisplayName("Bubbles LTR with different layout languages")

languagesTestView
.environmentObject(viewModel.context)
.environment(\.timelineStyle, .bubbles)
.environment(\.layoutDirection, .rightToLeft)
.previewDisplayName("Bubbles RTL with different layout languages")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import SwiftUI
struct TimelineItemStatusView: View {
let timelineItem: EventBasedTimelineItemProtocol
let adjustedDeliveryStatus: TimelineItemDeliveryStatus?
@Environment(\.timelineStyle) private var style
@EnvironmentObject private var context: RoomScreenViewModel.Context

private var isLastOutgoingMessage: Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import Foundation
import SwiftUI

struct EmoteRoomTimelineView: View, TextBasedRoomTimelineViewProtocol {
@Environment(\.timelineStyle) var timelineStyle
let timelineItem: EmoteRoomTimelineItem

var body: some View {
TimelineStyler(timelineItem: timelineItem) {
if let attributedString = timelineItem.content.formattedBody {
FormattedBodyText(attributedString: attributedString, additionalWhitespacesCount: timelineItem.additionalWhitespaces(timelineStyle: timelineStyle))
FormattedBodyText(attributedString: attributedString, additionalWhitespacesCount: timelineItem.additionalWhitespaces())
} else {
FormattedBodyText(text: timelineItem.content.body, additionalWhitespacesCount: timelineItem.additionalWhitespaces(timelineStyle: timelineStyle))
FormattedBodyText(text: timelineItem.content.body, additionalWhitespacesCount: timelineItem.additionalWhitespaces())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,14 @@ struct EncryptedRoomTimelineView: View {
}

struct RoomTimelineViewLabelStyle: LabelStyle {
@Environment(\.timelineStyle) private var timelineStyle

func makeBody(configuration: Configuration) -> some View {
HStack(alignment: .center, spacing: 8) {
configuration.icon
.foregroundColor(.compound.iconSecondary)
configuration.title
.foregroundColor(.compound.textPrimary)
}
.padding(.horizontal, timelineStyle == .bubbles ? 4 : 0)
.padding(.horizontal, 4)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import SwiftUI

struct FormattedBodyText: View {
@Environment(\.timelineStyle) private var timelineStyle
@Environment(\.layoutDirection) private var layoutDirection

private let attributedString: AttributedString
Expand Down Expand Up @@ -81,17 +80,12 @@ struct FormattedBodyText: View {

@ViewBuilder
var mainContent: some View {
if timelineStyle == .bubbles {
bubbleLayout
.tint(.compound.textLinkExternal)
} else {
plainLayout
.tint(.compound.textLinkExternal)
}
layout
.tint(.compound.textLinkExternal)
}

/// The attributed components laid out for the bubbles timeline style.
var bubbleLayout: some View {
var layout: some View {
TimelineBubbleLayout(spacing: 8) {
ForEach(attributedComponents) { component in
// Ignore if the string contains only the layout correction
Expand All @@ -115,7 +109,7 @@ struct FormattedBodyText: View {
.layoutPriority(TimelineBubbleLayout.Priority.visibleQuote)
} else {
MessageText(attributedString: component.attributedString)
.padding(.horizontal, timelineStyle == .bubbles ? 4 : 0)
.padding(.horizontal, 4)
.fixedSize(horizontal: false, vertical: true)
.layoutPriority(TimelineBubbleLayout.Priority.regularText)
}
Expand All @@ -135,27 +129,6 @@ struct FormattedBodyText: View {
}
}

/// The attributed components laid out for the plain timeline style.
var plainLayout: some View {
VStack(alignment: .leading, spacing: 8.0) {
ForEach(attributedComponents) { component in
if component.isBlockquote {
HStack(spacing: 4.0) {
Rectangle()
.foregroundColor(Color.red)
.frame(width: 4.0)
MessageText(attributedString: component.attributedString)
}
.fixedSize(horizontal: false, vertical: true)
} else {
MessageText(attributedString: component.attributedString)
.padding(.horizontal, timelineStyle == .bubbles ? 4 : 0)
.fixedSize(horizontal: false, vertical: true)
}
}
}
}

private var blockquoteAttributes: AttributeContainer {
// The paragraph style removes the block style paragraph that the parser adds by default
// Set directly in the constructor to avoid `Conformance to 'Sendable'` warnings
Expand Down Expand Up @@ -246,13 +219,11 @@ struct FormattedBodyText_Previews: PreviewProvider, TestablePreview {
}

private struct PreviewBubbleModifier: ViewModifier {
@Environment(\.timelineStyle) private var timelineStyle

func body(content: Content) -> some View {
content
.padding(timelineStyle == .bubbles ? 8 : 0)
.background(timelineStyle == .bubbles ? Color.compound._bgBubbleOutgoing : nil)
.cornerRadius(timelineStyle == .bubbles ? 12 : 0)
.padding(8)
.background(Color.compound._bgBubbleOutgoing)
.cornerRadius(12)
.environmentObject(RoomScreenViewModel.mock.context)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import SwiftUI

struct LocationRoomTimelineView: View {
let timelineItem: LocationRoomTimelineItem
@Environment(\.timelineStyle) var timelineStyle

var body: some View {
TimelineStyler(timelineItem: timelineItem) {
Expand All @@ -43,7 +42,7 @@ struct LocationRoomTimelineView: View {
.clipped()
}
} else {
FormattedBodyText(text: timelineItem.body, additionalWhitespacesCount: timelineItem.additionalWhitespaces(timelineStyle: timelineStyle))
FormattedBodyText(text: timelineItem.body, additionalWhitespacesCount: timelineItem.additionalWhitespaces())
}
}

Expand All @@ -61,8 +60,7 @@ struct LocationRoomTimelineView: View {
private var descriptionView: some View {
if let description = timelineItem.content.description, !description.isEmpty {
FormattedBodyText(text: description)
.padding(.vertical, 8)
.padding(.horizontal, timelineStyle.isBubbles ? 8 : 0)
.padding(8)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import SwiftUI

struct NoticeRoomTimelineView: View, TextBasedRoomTimelineViewProtocol {
let timelineItem: NoticeRoomTimelineItem
@Environment(\.timelineStyle) var timelineStyle

var body: some View {
TimelineStyler(timelineItem: timelineItem) {
Expand All @@ -30,9 +29,9 @@ struct NoticeRoomTimelineView: View, TextBasedRoomTimelineViewProtocol {

Label {
if let attributedString = timelineItem.content.formattedBody {
FormattedBodyText(attributedString: attributedString, additionalWhitespacesCount: timelineItem.additionalWhitespaces(timelineStyle: timelineStyle))
FormattedBodyText(attributedString: attributedString, additionalWhitespacesCount: timelineItem.additionalWhitespaces())
} else {
FormattedBodyText(text: timelineItem.content.body, additionalWhitespacesCount: timelineItem.additionalWhitespaces(timelineStyle: timelineStyle))
FormattedBodyText(text: timelineItem.content.body, additionalWhitespacesCount: timelineItem.additionalWhitespaces())
}
} icon: {
CompoundIcon(\.info, size: .small, relativeTo: .compound.bodyLG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,26 @@ struct PollRoomTimelineView_Previews: PreviewProvider, TestablePreview {

static var previews: some View {
PollRoomTimelineView(timelineItem: .mock(poll: .disclosed(), isOutgoing: false))
.environment(\.timelineStyle, .bubbles)
.environmentObject(viewModel.context)
.previewDisplayName("Disclosed, Bubble")

PollRoomTimelineView(timelineItem: .mock(poll: .undisclosed(), isOutgoing: false))
.environment(\.timelineStyle, .bubbles)
.environmentObject(viewModel.context)
.previewDisplayName("Undisclosed, Bubble")

PollRoomTimelineView(timelineItem: .mock(poll: .endedDisclosed))
.environment(\.timelineStyle, .bubbles)
.environmentObject(viewModel.context)
.previewDisplayName("Ended, Disclosed, Bubble")

PollRoomTimelineView(timelineItem: .mock(poll: .endedUndisclosed))
.environment(\.timelineStyle, .bubbles)
.environmentObject(viewModel.context)
.previewDisplayName("Ended, Undisclosed, Bubble")

PollRoomTimelineView(timelineItem: .mock(poll: .disclosed(createdByAccountOwner: true)))
.environment(\.timelineStyle, .bubbles)
.environmentObject(viewModel.context)
.previewDisplayName("Creator, disclosed, Bubble")

PollRoomTimelineView(timelineItem: .mock(poll: .emptyDisclosed, isEditable: true))
.environment(\.timelineStyle, .bubbles)
.environmentObject(viewModel.context)
.previewDisplayName("Creator, no votes, Bubble")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ protocol TextBasedRoomTimelineViewProtocol {
associatedtype TimelineItemType: TextBasedRoomTimelineItem

var timelineItem: TimelineItemType { get }
var timelineStyle: TimelineStyle { get }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ import SwiftUI

struct TextRoomTimelineView: View, TextBasedRoomTimelineViewProtocol {
let timelineItem: TextRoomTimelineItem
@Environment(\.timelineStyle) var timelineStyle

var body: some View {
TimelineStyler(timelineItem: timelineItem) {
if let attributedString = timelineItem.content.formattedBody {
FormattedBodyText(attributedString: attributedString,
additionalWhitespacesCount: timelineItem.additionalWhitespaces(timelineStyle: timelineStyle),
additionalWhitespacesCount: timelineItem.additionalWhitespaces(),
boostEmojiSize: true)
} else {
FormattedBodyText(text: timelineItem.body,
additionalWhitespacesCount: timelineItem.additionalWhitespaces(timelineStyle: timelineStyle),
additionalWhitespacesCount: timelineItem.additionalWhitespaces(),
boostEmojiSize: true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class TimelineTableViewController: UIViewController {
private let coordinator: TimelineView.Coordinator
private let tableView = UITableView(frame: .zero, style: .plain)

var timelineStyle: TimelineStyle
var timelineItemsDictionary = OrderedDictionary<String, RoomTimelineItemViewState>() {
didSet {
guard canApplySnapshot else {
Expand Down Expand Up @@ -166,11 +165,9 @@ class TimelineTableViewController: UIViewController {
private var hasAppearedOnce = false

init(coordinator: TimelineView.Coordinator,
timelineStyle: TimelineStyle,
isScrolledToBottom: Binding<Bool>,
scrollToBottomPublisher: PassthroughSubject<Void, Never>) {
self.coordinator = coordinator
self.timelineStyle = timelineStyle
_isScrolledToBottom = isScrolledToBottom

super.init(nibName: nil, bundle: nil)
Expand Down
Loading
Loading