@@ -76,6 +76,7 @@ final class ChatListViewController: KeyboardObservingViewController {
7676 setBadgeValue ( unreadController? . fetchedObjects? . count)
7777 }
7878 }
79+ private var chatDeselectedIndex : IndexPath ?
7980
8081 let defaultAvatar = UIImage . asset ( named: " avatar-chat-placeholder " ) ?? . init( )
8182
@@ -197,8 +198,11 @@ final class ChatListViewController: KeyboardObservingViewController {
197198
198199 override func viewWillAppear( _ animated: Bool ) {
199200 super. viewWillAppear ( animated)
200- if let indexPath = tableView. indexPathForSelectedRow {
201- tableView. deselectRow ( at: indexPath, animated: animated)
201+
202+ if UIDevice . current. userInterfaceIdiom == . phone {
203+ if let indexPath = tableView. indexPathForSelectedRow {
204+ tableView. deselectRow ( at: indexPath, animated: animated)
205+ }
202206 }
203207 }
204208 override func viewDidLayoutSubviews( ) {
@@ -342,7 +346,10 @@ final class ChatListViewController: KeyboardObservingViewController {
342346 chatPreservation. updateNotifier
343347 . receive ( on: DispatchQueue . main)
344348 . sink { [ weak self] in
345- self ? . tableView. reloadData ( )
349+ guard let deselectedIndex = self ? . chatDeselectedIndex,
350+ let cell = self ? . tableView. cellForRow ( at: deselectedIndex) as? ChatTableViewCell ,
351+ let chatroom = self ? . chatsController? . fetchedObjects ? [ safe: deselectedIndex. row] else { return }
352+ self ? . configureCell ( cell, for: chatroom)
346353 }
347354 . store ( in: & subscriptions)
348355 }
@@ -357,7 +364,7 @@ final class ChatListViewController: KeyboardObservingViewController {
357364
358365 private func updateUITitles( ) {
359366 updatingIndicatorView. updateTitle ( title: String . adamant. chatList. title)
360- tableView. reloadData ( )
367+ tableView. reloadDataPreservingSelection ( )
361368 searchController? . searchBar. placeholder = String . adamant. chatList. searchPlaceholder
362369 }
363370
@@ -408,7 +415,7 @@ final class ChatListViewController: KeyboardObservingViewController {
408415 areMessagesLoaded = true
409416 performOnMessagesLoadedActions ( )
410417 setIsBusy ( !synced)
411- tableView. reloadData ( )
418+ tableView. reloadDataPreservingSelection ( )
412419 }
413420
414421 // MARK: IB Actions
@@ -479,7 +486,7 @@ final class ChatListViewController: KeyboardObservingViewController {
479486
480487 switch result {
481488 case . success:
482- tableView. reloadData ( )
489+ tableView. reloadDataPreservingSelection ( )
483490 case . failure( let error) :
484491 dialogService. showRichError ( error: error)
485492 }
@@ -564,6 +571,10 @@ extension ChatListViewController: UITableViewDelegate, UITableViewDataSource {
564571 return UIView ( )
565572 }
566573
574+ func tableView( _ tableView: UITableView , didDeselectRowAt indexPath: IndexPath ) {
575+ chatDeselectedIndex = indexPath
576+ }
577+
567578 func tableView( _ tableView: UITableView , didSelectRowAt indexPath: IndexPath ) {
568579 if isBusy,
569580 indexPath. row == lastSystemChatPositionRow,
@@ -719,15 +730,15 @@ extension ChatListViewController {
719730
720731 private func insertReloadRow( ) {
721732 lastSystemChatPositionRow = getBottomSystemChatIndex ( )
722- tableView. reloadData ( )
733+ tableView. reloadDataPreservingSelection ( )
723734 }
724735
725736 @MainActor
726737 private func loadNewChats( offset: Int ) {
727738 loadNewChatTask = Task {
728739 await chatsProvider. getChatRooms ( offset: offset)
729740 isBusy = false
730- tableView. reloadData ( )
741+ tableView. reloadDataPreservingSelection ( )
731742 }
732743 }
733744}
@@ -1065,14 +1076,14 @@ extension ChatListViewController {
10651076 private func shortDescription( for address: String ) -> NSAttributedString ? {
10661077 var descriptionParts : [ NSAttributedString ] = [ ]
10671078
1068- if chatPreservation. getReplyMessage ( address: address, thenRemoveIt : false ) != nil {
1079+ if chatPreservation. getReplyMessage ( address: address) != nil {
10691080 let replyImageAttachment = NSTextAttachment ( )
10701081 replyImageAttachment. image = UIImage ( systemName: " arrowshape.turn.up.left " ) ? . withTintColor ( . adamant. primary)
10711082 replyImageAttachment. bounds = CGRect ( x: . zero, y: - 3 , width: 23 , height: 20 )
10721083
10731084 descriptionParts. append ( NSAttributedString ( attachment: replyImageAttachment) )
10741085 }
1075- if let files = chatPreservation. getPreservedFiles ( for: address, thenRemoveIt : false ) , !files. isEmpty {
1086+ if let files = chatPreservation. getPreservedFiles ( for: address) , !files. isEmpty {
10761087 let mediaCount = files. count ( where: { $0. type. isMedia } )
10771088 let otherCount = files. count ( where: { !$0. type. isMedia } )
10781089
@@ -1093,7 +1104,7 @@ extension ChatListViewController {
10931104 }
10941105 }
10951106
1096- if let preservedMessage = chatPreservation. getPreservedMessageFor ( address: address, thenRemoveIt : false ) {
1107+ if let preservedMessage = chatPreservation. getPreservedMessageFor ( address: address) {
10971108 let processedMessage = MessageProcessHelper . process ( preservedMessage)
10981109 descriptionParts. append ( NSAttributedString ( string: processedMessage) )
10991110 }
@@ -1595,5 +1606,13 @@ private extension UITableView {
15951606 reloadRows ( at: indexPaths, with: . none)
15961607 selectRow ( at: selectedRowIndexPath, animated: false , scrollPosition: . none)
15971608 }
1609+
1610+ func reloadDataPreservingSelection( ) {
1611+ let selectedRow = indexPathForSelectedRow
1612+ reloadData ( )
1613+ if let selectedRow = selectedRow {
1614+ selectRow ( at: selectedRow, animated: false , scrollPosition: . none)
1615+ }
1616+ }
15981617}
15991618
0 commit comments