track direction

This commit is contained in:
Diogo
2024-10-15 21:55:46 +01:00
parent 19a34b630c
commit ee1d9fff36
+13 -2
View File
@@ -64,7 +64,9 @@ struct ReverseList<Content: View>: UIViewControllerRepresentable {
private var itemCount: Int = 0 private var itemCount: Int = 0
private let updateFloatingButtons = PassthroughSubject<Void, Never>() private let updateFloatingButtons = PassthroughSubject<Void, Never>()
private var bag = Set<AnyCancellable>() private var bag = Set<AnyCancellable>()
private var lastContentOffset: CGFloat = 0
private var scrollDirection: ChatScrollDirection = .none
init(representer: ReverseList) { init(representer: ReverseList) {
self.representer = representer self.representer = representer
super.init(style: .plain) super.init(style: .plain)
@@ -94,7 +96,7 @@ struct ReverseList<Content: View>: UIViewControllerRepresentable {
) { (tableView, indexPath, item) -> UITableViewCell? in ) { (tableView, indexPath, item) -> UITableViewCell? in
if let section = self.dataSource.sectionIdentifier(for: indexPath.section) { if let section = self.dataSource.sectionIdentifier(for: indexPath.section) {
let itemCount = self.getTotalItemsInItemSection(indexPath: indexPath) let itemCount = self.getTotalItemsInItemSection(indexPath: indexPath)
if self.representer.activeSection == section, indexPath.item > itemCount - 8, itemCount > 8 { if self.representer.activeSection == section, self.scrollDirection == .toOldest, indexPath.item > itemCount - 8, itemCount > 8 {
let lastItem = self.getLastItemInItemSection(indexPath: indexPath) let lastItem = self.getLastItemInItemSection(indexPath: indexPath)
self.representer.loadPage(.toOldest, section, lastItem) self.representer.loadPage(.toOldest, section, lastItem)
} }
@@ -261,6 +263,15 @@ struct ReverseList<Content: View>: UIViewControllerRepresentable {
} }
override func scrollViewDidScroll(_ scrollView: UIScrollView) { override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let currentOffset = scrollView.contentOffset.y
if currentOffset > lastContentOffset {
scrollDirection = .toOldest
} else if currentOffset < lastContentOffset {
scrollDirection = .toLatest
} else {
scrollDirection = .none
}
lastContentOffset = currentOffset
updateFloatingButtons.send() updateFloatingButtons.send()
} }