@@ -25,42 +25,40 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
2525
2626 /// Used when screen is shown from a deeplink.
2727 private var selectedChannelId : String ?
28-
29- /// Temporarly holding changes while message list is shown.
30- private var queuedChannelsChanges = LazyCachedMapCollection < ChatChannel > ( )
31-
28+
3229 private var timer : Timer ?
3330
3431 /// Controls loading the channels.
3532 public private( set) var loadingNextChannels : Bool = false
3633
37- /// Checks if the queued changes are completely applied.
38- private var markDirty = false
39-
34+ /// True, if channel updates were skipped and are applied when selectedChannel is set to nil
35+ private var skippedChannelUpdates = false
36+
37+ /// True, if channel updates can be skipped for optimizing view refreshes while showing message list.
38+ ///
39+ /// - Important: Only meant for stacked navigation view style.
40+ private var canSkipChannelUpdates : Bool {
41+ guard isIphone || !utils. messageListConfig. iPadSplitViewEnabled else { return false }
42+ guard selectedChannel != nil || !searchText. isEmpty else { return false }
43+ return true
44+ }
45+
4046 /// Index of the selected channel.
4147 private var selectedChannelIndex : Int ?
4248
4349 /// When set, scrolls to the specified channel id (if it exists).
4450 @Published public var scrolledChannelId : String ?
4551
4652 /// Published variables.
47- @Published public var channels = LazyCachedMapCollection < ChatChannel > ( ) {
48- didSet {
49- if !markDirty {
50- queuedChannelsChanges = [ ]
51- } else {
52- markDirty = false
53- }
54- }
55- }
53+ @Published public var channels = LazyCachedMapCollection < ChatChannel > ( )
5654
5755 @Published public var selectedChannel : ChannelSelectionInfo ? {
5856 willSet {
5957 hideTabBar = newValue != nil
6058 if selectedChannel != nil && newValue == nil {
6159 // pop happened, apply the queued changes.
62- if !queuedChannelsChanges . isEmpty {
63- channels = queuedChannelsChanges
60+ if skippedChannelUpdates {
61+ updateChannels ( )
6462 }
6563 }
6664 if newValue == nil {
@@ -317,8 +315,8 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
317315 // MARK: - private
318316
319317 private func handleChannelListChanges( _ controller: ChatChannelListController ) {
320- if selectedChannel != nil || !searchText . isEmpty {
321- queuedChannelsChanges = controller . channels
318+ if canSkipChannelUpdates {
319+ skippedChannelUpdates = true
322320 updateChannelsIfNeeded ( )
323321 } else {
324322 channels = controller. channels
@@ -537,15 +535,16 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
537535 }
538536
539537 private func updateChannels( ) {
538+ skippedChannelUpdates = false
540539 channels = controller? . channels ?? LazyCachedMapCollection < ChatChannel > ( )
541540 }
542541
543542 private func handleChannelAppearance( ) {
544- if !queuedChannelsChanges . isEmpty && selectedChannel == nil {
545- channels = queuedChannelsChanges
546- } else if !queuedChannelsChanges . isEmpty {
547- handleQueuedChanges ( )
548- } else if queuedChannelsChanges . isEmpty && selectedChannel != nil {
543+ if skippedChannelUpdates && selectedChannel == nil {
544+ updateChannels ( )
545+ } else if skippedChannelUpdates {
546+ updateSelectedChannelData ( )
547+ } else if !skippedChannelUpdates && selectedChannel != nil {
549548 if selectedChannel? . injectedChannelInfo == nil {
550549 selectedChannel? . injectedChannelInfo = InjectedChannelInfo ( unreadCount: 0 )
551550 }
@@ -562,10 +561,10 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
562561 }
563562 }
564563
565- private func handleQueuedChanges ( ) {
564+ private func updateSelectedChannelData ( ) {
566565 let selected = selectedChannel? . channel
567566 var index : Int ?
568- var temp = Array ( queuedChannelsChanges )
567+ var temp = Array ( controller ? . channels ?? [ ] )
569568 for i in 0 ..< temp. count {
570569 let current = temp [ i]
571570 if current. cid == selected? . cid {
@@ -583,7 +582,6 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
583582 if let index = index, let selected = selected {
584583 temp [ index] = selected
585584 }
586- markDirty = true
587585 channels = LazyCachedMapCollection ( source: temp, map: { $0 } )
588586 }
589587
0 commit comments