From c3bd5528fa4d7ff1cefe60593e45c3c48dcec05f Mon Sep 17 00:00:00 2001 From: CMK Date: Fri, 15 Apr 2022 17:34:58 +0800 Subject: [PATCH] feat: add reload to Hashtags tab for Discovery scene --- .../DiscoveryHashtagsViewController.swift | 20 +++++++++++++++++++ .../Hashtags/DiscoveryHashtagsViewModel.swift | 13 ++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Mastodon/Scene/Discovery/Hashtags/DiscoveryHashtagsViewController.swift b/Mastodon/Scene/Discovery/Hashtags/DiscoveryHashtagsViewController.swift index 1dca1232..91ab5036 100644 --- a/Mastodon/Scene/Discovery/Hashtags/DiscoveryHashtagsViewController.swift +++ b/Mastodon/Scene/Discovery/Hashtags/DiscoveryHashtagsViewController.swift @@ -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 { diff --git a/Mastodon/Scene/Discovery/Hashtags/DiscoveryHashtagsViewModel.swift b/Mastodon/Scene/Discovery/Hashtags/DiscoveryHashtagsViewModel.swift index 5f51d645..bb30ec9e 100644 --- a/Mastodon/Scene/Discovery/Hashtags/DiscoveryHashtagsViewModel.swift +++ b/Mastodon/Scene/Discovery/Hashtags/DiscoveryHashtagsViewModel.swift @@ -15,6 +15,8 @@ import MastodonSDK final class DiscoveryHashtagsViewModel { + let logger = Logger(subsystem: "DiscoveryHashtagsViewModel", category: "ViewModel") + var disposeBag = Set() // 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)") + } +}