From 2f5169d0c366677e81df38273efb99d7fa5b1638 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Fri, 10 Nov 2023 13:28:37 +0100 Subject: [PATCH] Add pull to refresh (IOS-186) --- .../FollowedTagsViewController.swift | 21 +++++++++++++++---- .../FollowedTags/FollowedTagsViewModel.swift | 4 +++- .../Service/API/APIService+Tags.swift | 1 + 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewController.swift b/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewController.swift index 7e2bf5095..231e91fa3 100644 --- a/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewController.swift +++ b/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewController.swift @@ -18,11 +18,11 @@ final class FollowedTagsViewController: UIViewController, NeedsDependency { var coordinator: SceneCoordinator! let authContext: AuthContext - var disposeBag = Set() var viewModel: FollowedTagsViewModel let titleView = DoubleTitleLabelNavigationBarTitleView() let tableView: UITableView + let refreshControl: UIRefreshControl init(appContext: AppContext, sceneCoordinator: SceneCoordinator, authContext: AuthContext, viewModel: FollowedTagsViewModel) { self.context = appContext @@ -30,20 +30,20 @@ final class FollowedTagsViewController: UIViewController, NeedsDependency { self.authContext = authContext self.viewModel = viewModel + refreshControl = UIRefreshControl() + tableView = UITableView() tableView.register(FollowedTagsTableViewCell.self, forCellReuseIdentifier: FollowedTagsTableViewCell.reuseIdentifier) tableView.translatesAutoresizingMaskIntoConstraints = false tableView.rowHeight = UITableView.automaticDimension tableView.separatorStyle = .none tableView.backgroundColor = .clear + tableView.refreshControl = refreshControl super.init(nibName: nil, bundle: nil) let title = L10n.Scene.FollowedTags.title self.title = title - titleView.update(title: title, subtitle: nil) - - navigationItem.titleView = titleView view.backgroundColor = .secondarySystemBackground @@ -51,6 +51,8 @@ final class FollowedTagsViewController: UIViewController, NeedsDependency { tableView.pinToParent() tableView.delegate = self + + refreshControl.addTarget(self, action: #selector(FollowedTagsViewController.refresh(_:)), for: .valueChanged) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } @@ -59,6 +61,17 @@ final class FollowedTagsViewController: UIViewController, NeedsDependency { super.viewDidLoad() viewModel.setupTableView(tableView) } + + //MARK: - Actions + + @objc + func refresh(_ sender: UIRefreshControl) { + viewModel.fetchFollowedTags(completion: { + DispatchQueue.main.async { + self.refreshControl.endRefreshing() + } + }) + } } extension FollowedTagsViewController: UITableViewDelegate { diff --git a/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewModel.swift b/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewModel.swift index 677408ac1..00c6a5a9e 100644 --- a/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewModel.swift +++ b/Mastodon/Scene/Profile/FollowedTags/FollowedTagsViewModel.swift @@ -41,7 +41,7 @@ extension FollowedTagsViewModel { fetchFollowedTags() } - func fetchFollowedTags() { + func fetchFollowedTags(completion: (() -> Void)? = nil ) { Task { @MainActor in followedTags = try await context.apiService.getFollowedTags( domain: authContext.mastodonAuthenticationBox.domain, @@ -55,6 +55,8 @@ extension FollowedTagsViewModel { snapshot.appendItems(items, toSection: .main) await diffableDataSource?.apply(snapshot) + + completion?() } } diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Tags.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Tags.swift index 0a61fc687..008a7e44e 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Tags.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Tags.swift @@ -66,6 +66,7 @@ extension APIService { } fileprivate extension APIService { + @available(*, deprecated, message: "We don't persist tags anymore") func persistTag( from response: Mastodon.Response.Content, domain: String,