Skip to content

[trello.com/c/jKqpxiCa] update for paragraph 8 and 13 in the card #865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: release/3.11.0
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 70 additions & 13 deletions Adamant/Modules/Chat/View/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -836,12 +836,16 @@ extension ChatViewController {
fileprivate func updateScrollDownButtonVisibility() {
let topCount = viewModel.unreadMessagesIds?.count ?? 0
self.scrollDownButton.updateCounter(topCount)
guard state.isScrollDownButtonHidden != state.isScrollPositionNearlyTheBottom else { return }
state.isScrollDownButtonHidden = state.isScrollPositionNearlyTheBottom

let shouldShowButton = (topCount > 0) || !state.isScrollPositionNearlyTheBottom
guard state.isScrollDownButtonHidden != !shouldShowButton else { return }

state.isScrollDownButtonHidden = !shouldShowButton
let buttonUpdate = {
self.scrollDownButton.alpha = self.state.isScrollPositionNearlyTheBottom ? 0 : 1
self.scrollDownButton.alpha = shouldShowButton ? 1 : 0
self.updateScrollToUnreadButtonPosition()
}

if state.isAnimationAllowed {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut) {
buttonUpdate()
Expand Down Expand Up @@ -913,15 +917,7 @@ extension ChatViewController {
let button = ChatScrollButton(position: .down)
button.action = { [weak self] in
guard let self else { return }
if viewModel.shouldScrollToBottom {
state.isScrollingToBottom = true
markMessagesFromCurrentToBottomAsRead()
self.messagesCollectionView.scrollToBottom(animated: true)
} else if let id = viewModel.unreadMessagesIds?.first {
viewModel.animationType = MessageAnimationType.none
self.scrollToPosition(.messageId(id), animated: true)
viewModel.shouldScrollToBottom = true
}
scrollButtonAction()
}
button.alpha = 0
return button
Expand Down Expand Up @@ -958,6 +954,60 @@ extension ChatViewController {
)
return collection
}

fileprivate func scrollButtonAction() {
if viewModel.shouldScrollToBottom {
scrollToBottom()
return
}

guard let firstUnreadId = viewModel.unreadMessagesIds?.first,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could combine this with previous checking

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the point of view of readability of the algorithm it is more convenient

let unreadIndex = viewModel.messages.firstIndex(where: { $0.messageId == firstUnreadId }) else {
scrollToBottom()
return
}

let visibleBounds = messagesCollectionView.bounds
let restrictedVisibleRect = visibleBounds.insetBy(dx: 0, dy: 100)

let visibleSections: Set<Int> = Set(
messagesCollectionView.indexPathsForVisibleItems.compactMap { indexPath in
guard let attributes = messagesCollectionView.layoutAttributesForItem(at: indexPath) else {
return nil
}

let frame = attributes.frame
if restrictedVisibleRect.intersects(frame) {
return indexPath.section
} else {
return nil
}
}
)

if visibleSections.contains(unreadIndex) {
scrollToBottom()
return
}

if let separatorIndex = viewModel.separatorState.separatorIndex,
unreadIndex == separatorIndex + 1 {
viewModel.animationType = .none
scrollToPosition(.messageId(firstUnreadId), animated: false, setExtraOffset: true, scrollAt: .top)
viewModel.shouldScrollToBottom = true
return
}

viewModel.animationType = .none
scrollToPosition(.messageId(firstUnreadId), animated: true, scrollAt: .top)
viewModel.shouldScrollToBottom = true
}

fileprivate func scrollToBottom() {
state.isScrollingToBottom = true
markMessagesFromCurrentToBottomAsRead()
messagesCollectionView.scrollToBottom(animated: true)
}
}

// MARK: Other
Expand Down Expand Up @@ -1395,4 +1445,11 @@ private let messagePadding: CGFloat = 12
private let filesToolbarViewHeight: CGFloat = 140
private let targetYOffset: CGFloat = 20
private let scrollButtonHeight: CGFloat = 30
private let hiddenScrollViewPartHeight: CGFloat = 94
//not a real height just for reading messages
private var hiddenScrollViewPartHeight: CGFloat {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you get this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the height of the elements above and below is 100, to read messages not at the very edge of the cell, but a little higher, I selected the most suitable values

if isMacOS {
return 93
} else {
return 85
}
}