Skip to content

Commit 13387bc

Browse files
Fix position of date overlay view (#1409)
1 parent 61ede5e commit 13387bc

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ public struct ChatChannelView<Factory: ViewFactory>: View, KeyboardReadable {
8686
}
8787
.overlay(
8888
viewModel.currentDateString != nil ?
89-
factory.makeDateIndicatorView(options: DateIndicatorViewOptions(dateString: viewModel.currentDateString!))
89+
VStack {
90+
factory.makeDateIndicatorView(options: DateIndicatorViewOptions(dateString: viewModel.currentDateString!))
91+
Spacer()
92+
}
9093
: nil
9194
)
9295
} else {

StreamChatSwiftUITests/Tests/ChatChannel/ChatChannelViewDateOverlay_Tests.swift

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ import XCTest
1313
@MainActor class ChatChannelViewDateOverlay_Tests: StreamChatTestCase {
1414
override func setUp() {
1515
super.setUp()
16-
let utils = Utils(
17-
dateFormatter: EmptyDateFormatter(),
18-
messageListConfig: MessageListConfig(dateIndicatorPlacement: .messageList)
19-
)
20-
streamChat = StreamChat(chatClient: chatClient, utils: utils)
2116
DelayedRenderingViewModifier.isEnabled = false
2217
}
23-
18+
2419
override func tearDown() {
2520
super.tearDown()
2621
DelayedRenderingViewModifier.isEnabled = true
2722
}
2823

29-
func test_chatChannelView_snapshot() {
24+
func test_chatChannelView_snapshot_messageListPlacement() {
3025
// Given
26+
let utils = Utils(
27+
dateFormatter: EmptyDateFormatter(),
28+
messageListConfig: MessageListConfig(dateIndicatorPlacement: .messageList)
29+
)
30+
streamChat = StreamChat(chatClient: chatClient, utils: utils)
3131
let controller = ChatChannelController_Mock.mock(
3232
channelQuery: .init(cid: .unique),
3333
channelListQuery: nil,
@@ -65,4 +65,57 @@ import XCTest
6565
// Then
6666
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
6767
}
68+
69+
func test_chatChannelView_snapshot_overlayPlacement_dateIndicatorAppearsAtTop() {
70+
// Given
71+
let utils = Utils(
72+
dateFormatter: EmptyDateFormatter(),
73+
messageListConfig: MessageListConfig(dateIndicatorPlacement: .overlay)
74+
)
75+
streamChat = StreamChat(chatClient: chatClient, utils: utils)
76+
let controller = ChatChannelController_Mock.mock(
77+
channelQuery: .init(cid: .unique),
78+
channelListQuery: nil,
79+
client: chatClient
80+
)
81+
let mockChannel = ChatChannel.mock(cid: .unique, name: "Test channel")
82+
var messages = [ChatMessage]()
83+
let baseIntervalDistance: TimeInterval = 10000
84+
for i in 0..<3 {
85+
messages.append(
86+
ChatMessage.mock(
87+
id: .unique,
88+
cid: mockChannel.cid,
89+
text: "Test \(i)",
90+
author: .mock(id: .unique, name: "Martin"),
91+
createdAt: Date(timeIntervalSince1970: -TimeInterval(i) * baseIntervalDistance)
92+
)
93+
)
94+
}
95+
controller.simulateInitial(channel: mockChannel, messages: messages, state: .remoteDataFetched)
96+
97+
// When – inject a viewModel with currentDateString set to simulate the overlay being visible.
98+
// showScrollToLatestButton must also be true: handleMessageAppear calls save(lastDate:) during
99+
// rendering which triggers handleDateChange(); if showScrollToLatestButton is false that
100+
// method immediately resets currentDateString back to nil.
101+
let viewModel = ChatChannelViewModel(channelController: controller)
102+
viewModel.showScrollToLatestButton = true
103+
viewModel.currentDateString = "Today"
104+
105+
let view = NavigationView {
106+
ScrollView {
107+
ChatChannelView(
108+
viewFactory: DefaultViewFactory.shared,
109+
viewModel: viewModel,
110+
channelController: controller
111+
)
112+
.frame(width: defaultScreenSize.width, height: defaultScreenSize.height - 64)
113+
}
114+
.navigationBarTitleDisplayMode(.inline)
115+
}
116+
.applyDefaultSize()
117+
118+
// Then – date indicator must appear at the top, not the center
119+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
120+
}
68121
}

StreamChatSwiftUITests/Tests/ChatChannel/__Snapshots__/ChatChannelViewDateOverlay_Tests/test_chatChannelView_snapshot.1.png renamed to StreamChatSwiftUITests/Tests/ChatChannel/__Snapshots__/ChatChannelViewDateOverlay_Tests/test_chatChannelView_snapshot_messageListPlacement.1.png

File renamed without changes.
Loading

0 commit comments

Comments
 (0)