Don't run into an endless loop if no followers exist

See also 3fc2793
This commit is contained in:
Nathan Mattes 2023-11-09 12:11:14 +01:00
parent f3a7fe8320
commit 48d7592a9a
4 changed files with 36 additions and 17 deletions

View File

@ -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()
}
}
}

View File

@ -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)

View File

@ -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<Void, Never>()
var tableView: UITableView?
// output
@ -56,6 +56,5 @@ final class FollowerListViewModel {
self.userID = userID
self.accounts = []
self.relationships = []
self.listBatchFetchViewModel = ListBatchFetchViewModel()
}
}

View File

@ -173,7 +173,6 @@ extension FollowingListViewController: UIScrollViewDelegate {
if visibleBottomY > fetchThrottleOffsetY {
viewModel.shouldFetch.send()
}
}
}