Skip to content
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

Fix for offset 1 not making new messages auto scroll #1371

Merged
merged 11 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ class TimelineTableViewController: UIViewController {
/// The table's diffable data source.
private var dataSource: UITableViewDiffableDataSource<TimelineSection, String>?
private var cancellables: Set<AnyCancellable> = []

/// The scroll view adapter used to detect whether scrolling is in progress.
private let scrollAdapter = ScrollViewAdapter()

/// A publisher used to throttle back pagination requests.
///
/// Our view actions get wrapped in a `Task` so it is possible that a second call in
Expand Down Expand Up @@ -142,8 +140,9 @@ class TimelineTableViewController: UIViewController {
scrollToBottom(animated: false)
hasAppearedOnce = true
paginateBackwardsPublisher.send()
// This allows the reversed table view never fully be considered at the bottom
tableView.contentOffset.y = 1

// We never want the table view to be fully at the bottom to allow the status bar tap to work properly
tableView.contentOffset.y = -1
}

override func viewWillLayoutSubviews() {
Expand Down Expand Up @@ -213,14 +212,21 @@ class TimelineTableViewController: UIViewController {
MXLog.verbose("DIFF: \(snapshot.itemIdentifiers.difference(from: currentSnapshot.itemIdentifiers))")

// We only animate when new items come at the end of the timeline
let animated = shouldAnimate && snapshot.itemIdentifiers.first != currentSnapshot.itemIdentifiers.first
let animated = shouldAnimate &&
hasAppearedOnce &&
snapshot.itemIdentifiers.first != currentSnapshot.itemIdentifiers.first
dataSource.apply(snapshot, animatingDifferences: animated)
}

/// Scrolls to the bottom of the timeline.
private func scrollToBottom(animated: Bool) {
tableView.scrollToRow(at: IndexPath(item: 0, section: 0), at: .top, animated: animated)
}

/// Scrolls to the top of the timeline.
private func scrollToTop(animated: Bool) {
tableView.scrollToRow(at: IndexPath(item: timelineItemsIDs.count - 1, section: 0), at: .bottom, animated: animated)
}

/// Checks whether or a backwards pagination is needed and requests one if so.
///
Expand All @@ -233,11 +239,6 @@ class TimelineTableViewController: UIViewController {

coordinator.send(viewAction: .paginateBackwards)
}

private func scrollToTop(animated: Bool) {
tableView.scrollToRow(at: IndexPath(item: timelineItemsIDs.count - 1, section: 0), at: .bottom, animated: animated)
scrollAdapter.scrollViewDidScrollToTop(tableView)
}
}

// MARK: - UITableViewDelegate
Expand All @@ -250,39 +251,20 @@ extension TimelineTableViewController: UITableViewDelegate {
DispatchQueue.main.async { [weak self] in
guard let self else { return }

let scrollToBottomButtonVisible = scrollView.contentOffset.y > 15
let scrollToBottomButtonVisible = scrollView.contentOffset.y > 0

// Only update the binding on changes to avoid needlessly recomputing the hierarchy when scrolling.
if self.scrollToBottomButtonVisible != scrollToBottomButtonVisible {
self.scrollToBottomButtonVisible = scrollToBottomButtonVisible
}
}

// We never want the table view to be fully at the bottom to allow the status bar tap to work properly
if scrollView.contentOffset.y == 0 {
scrollView.contentOffset.y = 1
scrollView.contentOffset.y = -1
}
}

// MARK: ScrollViewAdapter Methods

// Required delegate methods are forwarded to the adapter so others can be implemented.

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
scrollAdapter.scrollViewWillBeginDragging(scrollView)
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
scrollAdapter.scrollViewDidEndDragging(scrollView, willDecelerate: decelerate)
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
scrollAdapter.scrollViewDidEndScrollingAnimation(scrollView)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollAdapter.scrollViewDidEndDecelerating(scrollView)
}

func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
scrollToTop(animated: true)
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct RoomTimelineItemView: View {
var body: some View {
timelineView
.environment(\.timelineGroupStyle, viewModel.groupStyle)
.animation(.elementDefault, value: viewModel.type)
.animation(.elementDefault, value: viewModel.groupStyle)
}

@ViewBuilder private var timelineView: some View {
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1371.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Timeline animations.