Skip to content

Commit

Permalink
Displayed no recent searches text if recent search is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
gokulvenkat243 committed Dec 24, 2024
1 parent 9a7b321 commit dc6927f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Combine
import CoreDataStack
import MastodonCore
import MastodonUI
import MastodonLocalization
import MastodonAsset

final class SearchHistoryViewController: UIViewController {

Expand All @@ -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 {
Expand All @@ -38,7 +48,8 @@ extension SearchHistoryViewController {
collectionView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(collectionView)
collectionView.pinToParent()

self.setupNoSearchResultLabel()
updateNoRecentSearchLabelUI()
collectionView.delegate = self
viewModel.setupDiffableDataSource(
collectionView: collectionView,
Expand All @@ -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
Expand Down Expand Up @@ -101,12 +128,14 @@ extension SearchHistoryViewController: SearchHistorySectionHeaderCollectionReusa
) {
FileManager.default.removeSearchHistory(for: authenticationBox)
viewModel.items = []
self.updateNoRecentSearchLabelUI()
}
}

//MARK: - SearchResultOverviewCoordinatorDelegate
extension SearchHistoryViewController: SearchResultOverviewCoordinatorDelegate {
func newSearchHistoryItemAdded(_ coordinator: SearchResultOverviewCoordinator) {
viewModel.items = (try? FileManager.default.searchItems(for: authenticationBox)) ?? []
self.updateNoRecentSearchLabelUI()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class SearchHistoryViewModel {
// output
var diffableDataSource: UICollectionViewDiffableDataSource<SearchHistorySection, SearchHistoryItem>?

public var isRecentSearchEmpty: Bool {
return items.isEmpty
}

init(authenticationBox: MastodonAuthenticationBox) {
self.authenticationBox = authenticationBox
self.items = (try? FileManager.default.searchItems(for: authenticationBox)) ?? []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dc6927f

Please sign in to comment.