diff --git a/Mastodon/Scene/Profile/Following/FollowingListViewController.swift b/Mastodon/Scene/Profile/Following/FollowingListViewController.swift index 8c6b78dac..ecd26fb34 100644 --- a/Mastodon/Scene/Profile/Following/FollowingListViewController.swift +++ b/Mastodon/Scene/Profile/Following/FollowingListViewController.swift @@ -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() + } + + } +} + diff --git a/Mastodon/Scene/Profile/Following/FollowingListViewModel+State.swift b/Mastodon/Scene/Profile/Following/FollowingListViewModel+State.swift index 1075cb491..ac5913ab1 100644 --- a/Mastodon/Scene/Profile/Following/FollowingListViewModel+State.swift +++ b/Mastodon/Scene/Profile/Following/FollowingListViewModel+State.swift @@ -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) diff --git a/Mastodon/Scene/Profile/Following/FollowingListViewModel.swift b/Mastodon/Scene/Profile/Following/FollowingListViewModel.swift index ef619f6d6..247b4fc64 100644 --- a/Mastodon/Scene/Profile/Following/FollowingListViewModel.swift +++ b/Mastodon/Scene/Profile/Following/FollowingListViewModel.swift @@ -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() + var tableView: UITableView? // output @@ -55,6 +55,5 @@ final class FollowingListViewModel { self.userID = userID self.accounts = [] self.relationships = [] - self.listBatchFetchViewModel = ListBatchFetchViewModel() } }