Add pull to refresh (IOS-186)

This commit is contained in:
Nathan Mattes 2023-11-10 13:28:37 +01:00
parent 9975fd56d9
commit 2f5169d0c3
3 changed files with 21 additions and 5 deletions

View File

@ -18,11 +18,11 @@ final class FollowedTagsViewController: UIViewController, NeedsDependency {
var coordinator: SceneCoordinator! var coordinator: SceneCoordinator!
let authContext: AuthContext let authContext: AuthContext
var disposeBag = Set<AnyCancellable>()
var viewModel: FollowedTagsViewModel var viewModel: FollowedTagsViewModel
let titleView = DoubleTitleLabelNavigationBarTitleView() let titleView = DoubleTitleLabelNavigationBarTitleView()
let tableView: UITableView let tableView: UITableView
let refreshControl: UIRefreshControl
init(appContext: AppContext, sceneCoordinator: SceneCoordinator, authContext: AuthContext, viewModel: FollowedTagsViewModel) { init(appContext: AppContext, sceneCoordinator: SceneCoordinator, authContext: AuthContext, viewModel: FollowedTagsViewModel) {
self.context = appContext self.context = appContext
@ -30,20 +30,20 @@ final class FollowedTagsViewController: UIViewController, NeedsDependency {
self.authContext = authContext self.authContext = authContext
self.viewModel = viewModel self.viewModel = viewModel
refreshControl = UIRefreshControl()
tableView = UITableView() tableView = UITableView()
tableView.register(FollowedTagsTableViewCell.self, forCellReuseIdentifier: FollowedTagsTableViewCell.reuseIdentifier) tableView.register(FollowedTagsTableViewCell.self, forCellReuseIdentifier: FollowedTagsTableViewCell.reuseIdentifier)
tableView.translatesAutoresizingMaskIntoConstraints = false tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.rowHeight = UITableView.automaticDimension tableView.rowHeight = UITableView.automaticDimension
tableView.separatorStyle = .none tableView.separatorStyle = .none
tableView.backgroundColor = .clear tableView.backgroundColor = .clear
tableView.refreshControl = refreshControl
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
let title = L10n.Scene.FollowedTags.title let title = L10n.Scene.FollowedTags.title
self.title = title self.title = title
titleView.update(title: title, subtitle: nil)
navigationItem.titleView = titleView
view.backgroundColor = .secondarySystemBackground view.backgroundColor = .secondarySystemBackground
@ -51,6 +51,8 @@ final class FollowedTagsViewController: UIViewController, NeedsDependency {
tableView.pinToParent() tableView.pinToParent()
tableView.delegate = self tableView.delegate = self
refreshControl.addTarget(self, action: #selector(FollowedTagsViewController.refresh(_:)), for: .valueChanged)
} }
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
@ -59,6 +61,17 @@ final class FollowedTagsViewController: UIViewController, NeedsDependency {
super.viewDidLoad() super.viewDidLoad()
viewModel.setupTableView(tableView) viewModel.setupTableView(tableView)
} }
//MARK: - Actions
@objc
func refresh(_ sender: UIRefreshControl) {
viewModel.fetchFollowedTags(completion: {
DispatchQueue.main.async {
self.refreshControl.endRefreshing()
}
})
}
} }
extension FollowedTagsViewController: UITableViewDelegate { extension FollowedTagsViewController: UITableViewDelegate {

View File

@ -41,7 +41,7 @@ extension FollowedTagsViewModel {
fetchFollowedTags() fetchFollowedTags()
} }
func fetchFollowedTags() { func fetchFollowedTags(completion: (() -> Void)? = nil ) {
Task { @MainActor in Task { @MainActor in
followedTags = try await context.apiService.getFollowedTags( followedTags = try await context.apiService.getFollowedTags(
domain: authContext.mastodonAuthenticationBox.domain, domain: authContext.mastodonAuthenticationBox.domain,
@ -55,6 +55,8 @@ extension FollowedTagsViewModel {
snapshot.appendItems(items, toSection: .main) snapshot.appendItems(items, toSection: .main)
await diffableDataSource?.apply(snapshot) await diffableDataSource?.apply(snapshot)
completion?()
} }
} }

View File

@ -66,6 +66,7 @@ extension APIService {
} }
fileprivate extension APIService { fileprivate extension APIService {
@available(*, deprecated, message: "We don't persist tags anymore")
func persistTag( func persistTag(
from response: Mastodon.Response.Content<Mastodon.Entity.Tag>, from response: Mastodon.Response.Content<Mastodon.Entity.Tag>,
domain: String, domain: String,