diff --git a/Mastodon/Scene/Profile/Follower/FollowerListViewController.swift b/Mastodon/Scene/Profile/Follower/FollowerListViewController.swift index 2591caf87..daa2838a5 100644 --- a/Mastodon/Scene/Profile/Follower/FollowerListViewController.swift +++ b/Mastodon/Scene/Profile/Follower/FollowerListViewController.swift @@ -50,7 +50,7 @@ final class FollowerListViewController: UIViewController, NeedsDependency { view.addSubview(tableView) tableView.pinToParent() tableView.delegate = self - tableView.refreshControl?.addTarget(self, action: #selector(FollowingListViewController.refresh(_:)), for: .valueChanged) + tableView.refreshControl?.addTarget(self, action: #selector(FollowerListViewController.refresh(_:)), for: .valueChanged) viewModel.tableView = tableView @@ -65,23 +65,13 @@ extension FollowerListViewController { override func viewDidLoad() { super.viewDidLoad() - title = L10n.Scene.Follower.title - - view.backgroundColor = .secondarySystemBackground - - tableView.translatesAutoresizingMaskIntoConstraints = false - view.addSubview(tableView) - tableView.pinToParent() - - tableView.delegate = self viewModel.setupDiffableDataSource( tableView: tableView, userTableViewCellDelegate: self ) // 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 } @@ -100,6 +90,8 @@ extension FollowerListViewController { self.viewModel.stateMachine.enter(FollowerListViewModel.State.Reloading.self) } .store(in: &disposeBag) + + tableView.refreshControl = UIRefreshControl() } override func viewWillAppear(_ animated: Bool) { @@ -166,3 +158,24 @@ extension FollowerListViewController: DataSourceProvider { return tableView.indexPath(for: cell) } } + +//MARK: - UIScrollViewDelegate + +extension FollowerListViewController: 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/Follower/FollowerListViewModel+State.swift b/Mastodon/Scene/Profile/Follower/FollowerListViewModel+State.swift index eeb285917..e25ff867f 100644 --- a/Mastodon/Scene/Profile/Follower/FollowerListViewModel+State.swift +++ b/Mastodon/Scene/Profile/Follower/FollowerListViewModel+State.swift @@ -144,6 +144,14 @@ extension FollowerListViewModel.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/Follower/FollowerListViewModel.swift b/Mastodon/Scene/Profile/Follower/FollowerListViewModel.swift index df0751a3c..b0d31417a 100644 --- a/Mastodon/Scene/Profile/Follower/FollowerListViewModel.swift +++ b/Mastodon/Scene/Profile/Follower/FollowerListViewModel.swift @@ -22,11 +22,11 @@ final class FollowerListViewModel { @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 @@ -56,6 +56,5 @@ final class FollowerListViewModel { self.userID = userID self.accounts = [] self.relationships = [] - self.listBatchFetchViewModel = ListBatchFetchViewModel() } } diff --git a/Mastodon/Scene/Profile/Following/FollowingListViewController.swift b/Mastodon/Scene/Profile/Following/FollowingListViewController.swift index ecd26fb34..28fdac266 100644 --- a/Mastodon/Scene/Profile/Following/FollowingListViewController.swift +++ b/Mastodon/Scene/Profile/Following/FollowingListViewController.swift @@ -173,7 +173,6 @@ extension FollowingListViewController: UIScrollViewDelegate { if visibleBottomY > fetchThrottleOffsetY { viewModel.shouldFetch.send() } - } }