feat: add reload to Hashtags tab for Discovery scene

This commit is contained in:
CMK 2022-04-15 17:34:58 +08:00
parent 9477071556
commit c3bd5528fa
2 changed files with 33 additions and 0 deletions

View File

@ -31,6 +31,8 @@ final class DiscoveryHashtagsViewController: UIViewController, NeedsDependency,
return tableView
}()
let refreshControl = UIRefreshControl()
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
@ -59,6 +61,9 @@ extension DiscoveryHashtagsViewController {
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
tableView.refreshControl = refreshControl
refreshControl.addTarget(self, action: #selector(DiscoveryHashtagsViewController.refreshControlValueChanged(_:)), for: .valueChanged)
tableView.delegate = self
viewModel.setupDiffableDataSource(
@ -80,6 +85,21 @@ extension DiscoveryHashtagsViewController {
}
extension DiscoveryHashtagsViewController {
@objc private func refreshControlValueChanged(_ sender: UIRefreshControl) {
Task { @MainActor in
do {
try await viewModel.fetch()
} catch {
// do nothing
}
sender.endRefreshing()
} // end Task
}
}
// MARK: - UITableViewDelegate
extension DiscoveryHashtagsViewController: UITableViewDelegate {

View File

@ -15,6 +15,8 @@ import MastodonSDK
final class DiscoveryHashtagsViewModel {
let logger = Logger(subsystem: "DiscoveryHashtagsViewModel", category: "ViewModel")
var disposeBag = Set<AnyCancellable>()
// input
@ -61,3 +63,14 @@ final class DiscoveryHashtagsViewModel {
}
}
extension DiscoveryHashtagsViewModel {
@MainActor
func fetch() async throws {
guard let authenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
let response = try await context.apiService.trendHashtags(domain: authenticationBox.domain, query: nil)
hashtags = response.value.filter { !$0.name.isEmpty }
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch tags: \(response.value.count)")
}
}