mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-25 09:41:00 +00:00
Scroll position maintenance
This commit is contained in:
parent
a124c44eb1
commit
c589c74052
4 changed files with 27 additions and 0 deletions
|
@ -210,6 +210,12 @@ private extension TableViewController {
|
|||
self.sizeTableHeaderFooterViews()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
tableView.publisher(for: \.contentOffset)
|
||||
.compactMap { [weak self] _ in self?.tableView.indexPathsForVisibleRows?.first }
|
||||
.removeDuplicates()
|
||||
.sink { [weak self] in self?.viewModel.viewedAtTop(indexPath: $0) }
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
func update(items: [[CollectionItemIdentifier]]) {
|
||||
|
|
|
@ -15,6 +15,7 @@ final public class CollectionItemsViewModel: ObservableObject {
|
|||
private var viewModelCache = [CollectionItem: (CollectionItemViewModel, AnyCancellable)]()
|
||||
private let navigationEventsSubject = PassthroughSubject<NavigationEvent, Never>()
|
||||
private let loadingSubject = PassthroughSubject<Bool, Never>()
|
||||
private var topVisibleIndexPath = IndexPath(item: 0, section: 0)
|
||||
private var lastSelectedLoadMore: LoadMore?
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
|
@ -80,6 +81,10 @@ extension CollectionItemsViewModel: CollectionViewModel {
|
|||
}
|
||||
}
|
||||
|
||||
public func viewedAtTop(indexPath: IndexPath) {
|
||||
topVisibleIndexPath = indexPath
|
||||
}
|
||||
|
||||
public func canSelect(indexPath: IndexPath) -> Bool {
|
||||
switch items.value[indexPath.section][indexPath.item] {
|
||||
case let .status(_, configuration):
|
||||
|
@ -180,6 +185,17 @@ private extension CollectionItemsViewModel {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if items.value.count > topVisibleIndexPath.section,
|
||||
items.value[topVisibleIndexPath.section].count > topVisibleIndexPath.item {
|
||||
let topVisibleItem = items.value[topVisibleIndexPath.section][topVisibleIndexPath.item]
|
||||
|
||||
if newItems.count > topVisibleIndexPath.section,
|
||||
let newIndex = newItems[topVisibleIndexPath.section].firstIndex(of: topVisibleItem),
|
||||
newIndex > topVisibleIndexPath.item {
|
||||
return .init(item: topVisibleItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -12,6 +12,7 @@ public protocol CollectionViewModel {
|
|||
var nextPageMaxId: String? { get }
|
||||
var maintainScrollPositionOfItem: CollectionItemIdentifier? { get }
|
||||
func request(maxId: String?, minId: String?)
|
||||
func viewedAtTop(indexPath: IndexPath)
|
||||
func select(indexPath: IndexPath)
|
||||
func canSelect(indexPath: IndexPath) -> Bool
|
||||
func viewModel(indexPath: IndexPath) -> CollectionItemViewModel
|
||||
|
|
|
@ -85,6 +85,10 @@ extension ProfileViewModel: CollectionViewModel {
|
|||
collectionViewModel.value.request(maxId: maxId, minId: minId)
|
||||
}
|
||||
|
||||
public func viewedAtTop(indexPath: IndexPath) {
|
||||
collectionViewModel.value.viewedAtTop(indexPath: indexPath)
|
||||
}
|
||||
|
||||
public func select(indexPath: IndexPath) {
|
||||
collectionViewModel.value.select(indexPath: indexPath)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue