@@ -43,6 +43,8 @@ final class ChatViewController: MessagesViewController {
4343 private var isScrollPositionNearlyTheBottom = true
4444 private var viewAppeared = false
4545 private var scrollToUnreadBottomConstraint : Constraint ?
46+ private var isScrollDownButtonHidden = true
47+ private var previousUnreadCount : Int = 0
4648
4749 private lazy var inputBar = ChatInputBar ( )
4850 private lazy var loadingView = LoadingView ( )
@@ -142,7 +144,6 @@ final class ChatViewController: MessagesViewController {
142144 override func viewWillLayoutSubviews( ) {
143145 super. viewWillLayoutSubviews ( )
144146 updateIsScrollPositionNearlyTheBottom ( )
145- updateScrollDownButtonVisibility ( )
146147 }
147148
148149 override func viewDidLayoutSubviews( ) {
@@ -157,8 +158,8 @@ final class ChatViewController: MessagesViewController {
157158 if navigationController? . delegate !== self {
158159 navigationController? . delegate = self
159160 }
160-
161161 viewModel. updatePartnerName ( )
162+ updateScrollDownButtonVisibility ( )
162163 }
163164
164165 override func viewDidAppear( _ animated: Bool ) {
@@ -546,12 +547,9 @@ private extension ChatViewController {
546547 }
547548
548549 func updateScrollToUnreadButtonPosition( ) {
549- UIView . animate ( withDuration: 0.3 , delay: 0 , options: . curveEaseInOut) {
550- if self . scrollDownButton. alpha == 0 {
551- self . scrollToUnreadBottomConstraint? . update ( offset: 0 )
552- } else {
553- self . scrollToUnreadBottomConstraint? . update ( offset: - ( scrollToUnreadInset + scrollButtonHeight) )
554- }
550+ let offset = ( scrollDownButton. alpha == 0 ) ? 0 : - ( scrollToUnreadInset + scrollButtonHeight)
551+ scrollToUnreadBottomConstraint? . update ( offset: offset)
552+ if messagesLoaded {
555553 self . view. layoutIfNeeded ( )
556554 }
557555 }
@@ -777,21 +775,37 @@ private extension ChatViewController {
777775
778776 func updateScrollDownButtonVisibility( ) {
779777 let topCount = viewModel. unreadMessagesIds? . count ?? 0
780-
781- UIView . animate ( withDuration: 0.3 , delay: 0 , options: . curveEaseInOut) {
782- self . scrollDownButton. alpha = self . isScrollPositionNearlyTheBottom ? 0 : 1
783- self . updateScrollToUnreadButtonPosition ( )
784- self . scrollDownButton. updateCounter ( topCount)
785- }
778+ self . scrollDownButton. updateCounter ( topCount)
779+ guard isScrollDownButtonHidden != isScrollPositionNearlyTheBottom else { return }
780+ isScrollDownButtonHidden = isScrollPositionNearlyTheBottom
781+ let buttonUpdate = {
782+ self . scrollDownButton. alpha = self . isScrollPositionNearlyTheBottom ? 0 : 1
783+ self . updateScrollToUnreadButtonPosition ( )
784+ }
785+ if messagesLoaded {
786+ UIView . animate ( withDuration: 0.3 , delay: 0 , options: . curveEaseInOut) {
787+ buttonUpdate ( )
788+ }
789+ } else {
790+ buttonUpdate ( )
791+ }
786792 }
787793
788794 func updateScrollToUnreadButtonVisibility( ) {
789795 let count = viewModel. messagesWithUnredReactionsIds? . count ?? 0
790-
791- UIView . animate ( withDuration: 0.3 , delay: 0 , options: . curveEaseInOut) {
792- self . scrollToUnreadReactButton. alpha = ( count == 0 ) ? 0 : 1
793- }
794796 scrollToUnreadReactButton. updateCounter ( count)
797+
798+ guard ( previousUnreadCount == 0 && count > 0 ) || ( previousUnreadCount > 0 && count == 0 ) else {
799+ previousUnreadCount = count
800+ return
801+ }
802+ previousUnreadCount = count
803+ let updateAlpha = { self . scrollToUnreadReactButton. alpha = ( count == 0 ) ? 0 : 1 }
804+ if messagesLoaded {
805+ UIView . animate ( withDuration: 0.3 , delay: 0 , options: . curveEaseInOut, animations: updateAlpha)
806+ } else {
807+ updateAlpha ( )
808+ }
795809 }
796810
797811 func updateDateHeaderIfNeeded( ) {
@@ -841,7 +855,7 @@ private extension ChatViewController {
841855
842856 viewModel. scroll ( to: unreadId)
843857 }
844-
858+ button . alpha = 0
845859 return button
846860 }
847861
0 commit comments