Replace listbatch-model for Following-list with UIScrollViewDelegate-implementation

No more recursion due to no timer and better state handling
This commit is contained in:
Nathan Mattes 2023-11-01 21:28:32 +01:00
parent 38a9268abb
commit 3fc27936d6
3 changed files with 35 additions and 5 deletions

View File

@ -68,11 +68,11 @@ final class FollowingListViewController: UIViewController, NeedsDependency {
)
// setup batch fetch
viewModel.listBatchFetchViewModel.setup(scrollView: tableView)
viewModel.listBatchFetchViewModel.shouldFetch
viewModel.shouldFetch
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
self.viewModel.stateMachine.enter(FollowingListViewModel.State.Loading.self)
}
.store(in: &disposeBag)
@ -154,3 +154,26 @@ extension FollowingListViewController: DataSourceProvider {
return tableView.indexPath(for: cell)
}
}
//MARK: - UIScrollViewDelegate
extension FollowingListViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.isDragging || scrollView.isTracking { return }
let frame = scrollView.frame
let contentOffset = scrollView.contentOffset
let contentSize = scrollView.contentSize
let visibleBottomY = contentOffset.y + frame.height
let offset = 2 * frame.height
let fetchThrottleOffsetY = contentSize.height - offset
if visibleBottomY > fetchThrottleOffsetY {
viewModel.shouldFetch.send()
}
}
}

View File

@ -139,6 +139,14 @@ extension FollowingListViewModel.State {
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
)
if accountResponse.value.isEmpty {
await enter(state: NoMore.self)
viewModel.accounts = []
viewModel.relationships = []
return
}
var hasNewAppend = false
let newRelationships = try await viewModel.context.apiService.relationship(forAccounts: accountResponse.value, authenticationBox: viewModel.authContext.mastodonAuthenticationBox)

View File

@ -21,11 +21,11 @@ final class FollowingListViewModel {
@Published var accounts: [Mastodon.Entity.Account]
@Published var relationships: [Mastodon.Entity.Relationship]
let listBatchFetchViewModel: ListBatchFetchViewModel
@Published var domain: String?
@Published var userID: String?
let shouldFetch = PassthroughSubject<Void, Never>()
var tableView: UITableView?
// output
@ -55,6 +55,5 @@ final class FollowingListViewModel {
self.userID = userID
self.accounts = []
self.relationships = []
self.listBatchFetchViewModel = ListBatchFetchViewModel()
}
}