Skip to content

Commit 839f376

Browse files
authored
Fix LivestreamChannelController not connecting chat after coming from background (#3778)
* Fix livestream controller not resuming chat after coming from background * Update CHANGELOG.md * Add comment explaining the reason
1 parent 5cfb6d1 commit 839f376

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1111
- Add a completion block to `LivestreamChannelController.resume()` to observe possible errors [#3774](https://github.com/GetStream/stream-chat-swift/pull/3774)
1212
### 🐞 Fixed
1313
- Fix pending message being added to `LivestreamChannelController.messages` when in paused state [#3774](https://github.com/GetStream/stream-chat-swift/pull/3774)
14+
- Fix `LivestreamChannelController` not connecting chat after coming from background [#3778](https://github.com/GetStream/stream-chat-swift/pull/3778)
1415
### 🔄 Changed
1516
- The `LivestreamChannelController.resume()` should be manually called, previously, it was automatically called on a new message [#3774](https://github.com/GetStream/stream-chat-swift/pull/3774)
1617

Sources/StreamChat/Controllers/ChannelController/LivestreamChannelController.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,15 @@ public class LivestreamChannelController: DataStoreProvider, EventsControllerDel
760760
loadFirstPage()
761761
}
762762

763+
public func applicationDidMoveToForeground() {
764+
// The livestream controller is not impacted by the syncing of missing events.
765+
// Since it won't get notified about messages updated from the DB.
766+
// So we need to manually reset the channel once the user is connected again.
767+
if client.connectionStatus != .connected {
768+
loadFirstPage()
769+
}
770+
}
771+
763772
// MARK: - Private Methods
764773

765774
private func updateChannelData(

TestTools/StreamChatTestTools/Mocks/StreamChat/ChatClient_Mock.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class ChatClient_Mock: ChatClient {
2727
mockedAppSettings
2828
}
2929

30-
var mockedEventNotificationCenter: EventNotificationCenter_Mock? = nil
30+
var mockedEventNotificationCenter: EventNotificationCenter_Mock?
3131

3232
override var eventNotificationCenter: EventNotificationCenter {
3333
mockedEventNotificationCenter ?? super.eventNotificationCenter
@@ -64,7 +64,7 @@ final class ChatClient_Mock: ChatClient {
6464
}
6565

6666
override var currentUserId: UserId? {
67-
return currentUserId_mock
67+
currentUserId_mock
6868
}
6969

7070
public var currentUserId_mock: UserId? {
@@ -76,6 +76,16 @@ final class ChatClient_Mock: ChatClient {
7676
}
7777
}
7878

79+
public var connectionStatus_mock: ConnectionStatus?
80+
override var connectionStatus: ConnectionStatus {
81+
get {
82+
connectionStatus_mock ?? super.connectionStatus
83+
}
84+
set {
85+
connectionStatus_mock = newValue
86+
}
87+
}
88+
7989
override func createBackgroundWorkers() {
8090
createBackgroundWorkers_called = true
8191

Tests/StreamChatTests/Controllers/ChannelController/LivestreamChannelController_Tests.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,35 @@ extension LivestreamChannelController_Tests {
906906
let expectedEndpoint = Endpoint<ChannelPayload>.updateChannel(query: expectedQuery)
907907
XCTAssertEqual(apiClient.request_endpoint, AnyEndpoint(expectedEndpoint))
908908
}
909-
909+
910+
func test_applicationDidMoveToForeground_whenNotConnected_callsLoadFirstPage() {
911+
// Given
912+
let apiClient = client.mockAPIClient
913+
client.connectionStatus_mock = .disconnected(error: ClientError())
914+
915+
// When
916+
controller.applicationDidMoveToForeground()
917+
918+
// Then
919+
let expectedPagination = MessagesPagination(pageSize: 25, parameter: nil)
920+
var expectedQuery = channelQuery!
921+
expectedQuery.pagination = expectedPagination
922+
let expectedEndpoint = Endpoint<ChannelPayload>.updateChannel(query: expectedQuery)
923+
XCTAssertEqual(apiClient.request_endpoint, AnyEndpoint(expectedEndpoint))
924+
}
925+
926+
func test_applicationDidMoveToForeground_whenConnected_doesNotCallLoadFirstPage() {
927+
// Given
928+
let apiClient = client.mockAPIClient
929+
client.connectionStatus_mock = .connected
930+
931+
// When
932+
controller.applicationDidMoveToForeground()
933+
934+
// Then
935+
XCTAssertNil(apiClient.request_endpoint)
936+
}
937+
910938
func test_didReceiveEvent_messageNewEvent_addsMessageToArray() {
911939
let newMessage = ChatMessage.mock(id: "new", cid: controller.cid!, text: "New message")
912940
let event = MessageNewEvent(

0 commit comments

Comments
 (0)