Skip to content

Commit 1b867f0

Browse files
Improved loading of initial channel list
1 parent 7eeb5d3 commit 1b867f0

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
3535
/// Controls loading the channels.
3636
private var loadingNextChannels: Bool = false
3737

38+
/// Checks if internet connection is available.
39+
private let networkReachability = NetworkReachability()
40+
3841
/// Published variables.
3942
@Published public var channels = LazyCachedMapCollection<ChatChannel>() {
4043
didSet {
@@ -253,7 +256,10 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
253256

254257
updateChannels()
255258

256-
loading = true
259+
if channels.isEmpty {
260+
loading = networkReachability.isNetworkAvailable()
261+
}
262+
257263
controller?.synchronize { [weak self] error in
258264
guard let self = self else { return }
259265
self.loading = false
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright © 2022 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import Foundation
6+
import Network
7+
8+
/// Class that checks if the network is reachable.
9+
class NetworkReachability {
10+
11+
let pathMonitor: NWPathMonitor
12+
var path: NWPath?
13+
14+
lazy var pathUpdateHandler: ((NWPath) -> Void) = { path in
15+
self.path = path
16+
}
17+
18+
let backgroudQueue = DispatchQueue.global(qos: .background)
19+
20+
init() {
21+
pathMonitor = NWPathMonitor()
22+
pathMonitor.pathUpdateHandler = pathUpdateHandler
23+
pathMonitor.start(queue: backgroudQueue)
24+
}
25+
26+
func isNetworkAvailable() -> Bool {
27+
if let path = self.path {
28+
if path.status == NWPath.Status.satisfied {
29+
return true
30+
}
31+
}
32+
return false
33+
}
34+
}

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
846608E7278C95E700D3D7B3 /* ChatChannelExtensions_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 846608E6278C95E700D3D7B3 /* ChatChannelExtensions_Tests.swift */; };
164164
846608E9278C98CB00D3D7B3 /* TypingIndicatorView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 846608E8278C98CB00D3D7B3 /* TypingIndicatorView_Tests.swift */; };
165165
846D6564279FF0800094B36E /* ReactionUserView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 846D6563279FF0800094B36E /* ReactionUserView.swift */; };
166+
84733EC627FDBF82006926E0 /* NetworkReachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84733EC527FDBF82006926E0 /* NetworkReachability.swift */; };
166167
847BA08127E0B76400ED20C7 /* Spy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847BA08027E0B76400ED20C7 /* Spy.swift */; };
167168
847BA08327E0B9C600ED20C7 /* EventBatcherMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847BA08227E0B9C600ED20C7 /* EventBatcherMock.swift */; };
168169
847BA08527E0BA2500ED20C7 /* EventNotificationCenterMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847BA08427E0BA2500ED20C7 /* EventNotificationCenterMock.swift */; };
@@ -494,6 +495,7 @@
494495
846608E6278C95E700D3D7B3 /* ChatChannelExtensions_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatChannelExtensions_Tests.swift; sourceTree = "<group>"; };
495496
846608E8278C98CB00D3D7B3 /* TypingIndicatorView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypingIndicatorView_Tests.swift; sourceTree = "<group>"; };
496497
846D6563279FF0800094B36E /* ReactionUserView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReactionUserView.swift; sourceTree = "<group>"; };
498+
84733EC527FDBF82006926E0 /* NetworkReachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkReachability.swift; sourceTree = "<group>"; };
497499
847BA08027E0B76400ED20C7 /* Spy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Spy.swift; sourceTree = "<group>"; };
498500
847BA08227E0B9C600ED20C7 /* EventBatcherMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventBatcherMock.swift; sourceTree = "<group>"; };
499501
847BA08427E0BA2500ED20C7 /* EventNotificationCenterMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventNotificationCenterMock.swift; sourceTree = "<group>"; };
@@ -968,6 +970,7 @@
968970
8465FD3B2746A95600AF091E /* StringExtensions.swift */,
969971
841B64C92775BBC10016FF3B /* Errors.swift */,
970972
847CEFED27C38ABE00606257 /* MessageCachingUtils.swift */,
973+
84733EC527FDBF82006926E0 /* NetworkReachability.swift */,
971974
8465FD382746A95600AF091E /* Common */,
972975
);
973976
path = Utils;
@@ -1577,6 +1580,7 @@
15771580
841B64CA2775BBC10016FF3B /* Errors.swift in Sources */,
15781581
8465FD7A2746A95700AF091E /* VideoPlayerView.swift in Sources */,
15791582
8465FD8B2746A95700AF091E /* FilePickerView.swift in Sources */,
1583+
84733EC627FDBF82006926E0 /* NetworkReachability.swift in Sources */,
15801584
84E6EC27279B0C930017207B /* ReactionsUsersView.swift in Sources */,
15811585
8465FDA92746A95700AF091E /* AutoLayoutHelpers.swift in Sources */,
15821586
8465FDC32746A95700AF091E /* ChatChannelListHeader.swift in Sources */,

0 commit comments

Comments
 (0)