diff --git a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewController.swift b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewController.swift index 76e7b5942..0407cfc8c 100644 --- a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewController.swift +++ b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewController.swift @@ -10,6 +10,8 @@ import Combine import CoreDataStack import MastodonCore import MastodonUI +import MastodonLocalization +import MastodonAsset final class SearchHistoryViewController: UIViewController { @@ -27,6 +29,14 @@ final class SearchHistoryViewController: UIViewController { collectionView.keyboardDismissMode = .onDrag return collectionView }() + + private let noSearchResultLabel: UILabel = { + let label: UILabel = UILabel() + label.text = L10n.Scene.Search.Searching.noSearchResults + label.textColor = .secondaryLabel + label.isHidden = true // Initially Hiden + return label + }() } extension SearchHistoryViewController { @@ -38,7 +48,8 @@ extension SearchHistoryViewController { collectionView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(collectionView) collectionView.pinToParent() - + self.setupNoSearchResultLabel() + updateNoRecentSearchLabelUI() collectionView.delegate = self viewModel.setupDiffableDataSource( collectionView: collectionView, @@ -49,6 +60,22 @@ extension SearchHistoryViewController { override func viewWillAppear(_ animated: Bool) { viewModel.items = (try? FileManager.default.searchItems(for: authenticationBox)) ?? [] } + + private func setupNoSearchResultLabel() { + noSearchResultLabel.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(noSearchResultLabel) + NSLayoutConstraint.activate([ + noSearchResultLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor), + noSearchResultLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor) + ]) + } + + private func updateNoRecentSearchLabelUI() { + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + self.noSearchResultLabel.isHidden = !self.viewModel.isRecentSearchEmpty + } + } } // MARK: - UICollectionViewDelegate @@ -101,6 +128,7 @@ extension SearchHistoryViewController: SearchHistorySectionHeaderCollectionReusa ) { FileManager.default.removeSearchHistory(for: authenticationBox) viewModel.items = [] + self.updateNoRecentSearchLabelUI() } } @@ -108,5 +136,6 @@ extension SearchHistoryViewController: SearchHistorySectionHeaderCollectionReusa extension SearchHistoryViewController: SearchResultOverviewCoordinatorDelegate { func newSearchHistoryItemAdded(_ coordinator: SearchResultOverviewCoordinator) { viewModel.items = (try? FileManager.default.searchItems(for: authenticationBox)) ?? [] + self.updateNoRecentSearchLabelUI() } } diff --git a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift index 59bb75e03..bbecfa15b 100644 --- a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift +++ b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewModel.swift @@ -20,6 +20,10 @@ final class SearchHistoryViewModel { // output var diffableDataSource: UICollectionViewDiffableDataSource? + public var isRecentSearchEmpty: Bool { + return items.isEmpty + } + init(authenticationBox: MastodonAuthenticationBox) { self.authenticationBox = authenticationBox self.items = (try? FileManager.default.searchItems(for: authenticationBox)) ?? [] diff --git a/MastodonSDK/Sources/MastodonLocalization/Generated/Strings.swift b/MastodonSDK/Sources/MastodonLocalization/Generated/Strings.swift index 88ad53d94..d63cb7396 100644 --- a/MastodonSDK/Sources/MastodonLocalization/Generated/Strings.swift +++ b/MastodonSDK/Sources/MastodonLocalization/Generated/Strings.swift @@ -1464,6 +1464,8 @@ public enum L10n { } /// Recent searches public static let recentSearch = L10n.tr("Localizable", "Scene.Search.Searching.RecentSearch", fallback: "Recent searches") + /// No search results + public static let noSearchResults = L10n.tr("Localizable", "Scene.Search.Searching.NoSearchResults", fallback: "No recent searches.") /// Open URL in Mastodon public static let url = L10n.tr("Localizable", "Scene.Search.Searching.Url", fallback: "Open URL in Mastodon") public enum EmptyState {