From 27b698a97a3725974dcc5ad41c171da1b6cb1cdf Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Wed, 7 Apr 2021 21:42:43 +0800 Subject: [PATCH] chore: add backgroud.navigation.color. update colors in searching page --- Mastodon/Generated/Assets.swift | 1 + .../navigationBar.colorset/Contents.json | 38 +++++++++++++++++ .../Scene/Search/SearchViewController.swift | 18 +++++--- .../SearchViewModel+LoadOldestState.swift | 15 +++---- Mastodon/Scene/Search/SearchViewModel.swift | 42 ++++++++++--------- .../TableViewCell/SearchBottomLoader.swift | 2 +- .../SearchingTableViewCell.swift | 2 +- .../SearchRecommendCollectionHeader.swift | 2 +- .../MastodonSDK/API/Mastodon+API+Search.swift | 11 +++-- 9 files changed, 92 insertions(+), 39 deletions(-) create mode 100644 Mastodon/Resources/Assets.xcassets/Colors/Background/navigationBar.colorset/Contents.json diff --git a/Mastodon/Generated/Assets.swift b/Mastodon/Generated/Assets.swift index 71034c1d..c8fccbfe 100644 --- a/Mastodon/Generated/Assets.swift +++ b/Mastodon/Generated/Assets.swift @@ -42,6 +42,7 @@ internal enum Asset { internal static let dangerBorder = ColorAsset(name: "Colors/Background/danger.border") internal static let danger = ColorAsset(name: "Colors/Background/danger") internal static let mediaTypeIndicotor = ColorAsset(name: "Colors/Background/media.type.indicotor") + internal static let navigationBar = ColorAsset(name: "Colors/Background/navigationBar") internal static let onboardingBackground = ColorAsset(name: "Colors/Background/onboarding.background") internal static let secondaryGroupedSystemBackground = ColorAsset(name: "Colors/Background/secondary.grouped.system.background") internal static let secondarySystemBackground = ColorAsset(name: "Colors/Background/secondary.system.background") diff --git a/Mastodon/Resources/Assets.xcassets/Colors/Background/navigationBar.colorset/Contents.json b/Mastodon/Resources/Assets.xcassets/Colors/Background/navigationBar.colorset/Contents.json new file mode 100644 index 00000000..7f9578a7 --- /dev/null +++ b/Mastodon/Resources/Assets.xcassets/Colors/Background/navigationBar.colorset/Contents.json @@ -0,0 +1,38 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "0.940", + "blue" : "249", + "green" : "249", + "red" : "249" + } + }, + "idiom" : "universal" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "0.940", + "blue" : "29", + "green" : "29", + "red" : "29" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Mastodon/Scene/Search/SearchViewController.swift b/Mastodon/Scene/Search/SearchViewController.swift index 2e6fc651..470c88e0 100644 --- a/Mastodon/Scene/Search/SearchViewController.swift +++ b/Mastodon/Scene/Search/SearchViewController.swift @@ -27,6 +27,7 @@ final class SearchViewController: UIViewController, NeedsDependency { searchBar.showsBookmarkButton = true searchBar.showsScopeBar = false searchBar.scopeButtonTitles = [L10n.Scene.Search.Searching.Segment.all, L10n.Scene.Search.Searching.Segment.people, L10n.Scene.Search.Searching.Segment.hashtags] + searchBar.barTintColor = Asset.Colors.Background.navigationBar.color return searchBar }() @@ -76,9 +77,10 @@ final class SearchViewController: UIViewController, NeedsDependency { // searching let searchingTableView: UITableView = { let tableView = UITableView() - tableView.backgroundColor = Asset.Colors.Background.secondaryGroupedSystemBackground.color + tableView.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color tableView.rowHeight = UITableView.automaticDimension tableView.separatorStyle = .singleLine + tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) return tableView }() @@ -99,7 +101,7 @@ final class SearchViewController: UIViewController, NeedsDependency { let clearSearchHistoryButton: UIButton = { let button = UIButton(type: .custom) - button.setTitleColor(Asset.Colors.buttonDefault.color, for: .normal) + button.setTitleColor(Asset.Colors.brandBlue.color, for: .normal) button.setTitle(L10n.Scene.Search.Searching.clear, for: .normal) return button }() @@ -109,6 +111,12 @@ extension SearchViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color + let barAppearance = UINavigationBarAppearance() + barAppearance.configureWithTransparentBackground() + barAppearance.backgroundColor = Asset.Colors.Background.navigationBar.color + navigationItem.standardAppearance = barAppearance + navigationItem.compactAppearance = barAppearance + navigationItem.scrollEdgeAppearance = barAppearance searchBar.delegate = self navigationItem.titleView = searchBar navigationItem.hidesBackButton = true @@ -183,11 +191,11 @@ extension SearchViewController: UISearchBarDelegate { func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { switch selectedScope { case 0: - viewModel.searchScope.value = "" + viewModel.searchScope.value = Mastodon.API.Search.SearchType.default case 1: - viewModel.searchScope.value = Mastodon.API.Search.Scope.accounts.rawValue + viewModel.searchScope.value = Mastodon.API.Search.SearchType.accounts case 2: - viewModel.searchScope.value = Mastodon.API.Search.Scope.hashtags.rawValue + viewModel.searchScope.value = Mastodon.API.Search.SearchType.hashtags default: break } diff --git a/Mastodon/Scene/Search/SearchViewModel+LoadOldestState.swift b/Mastodon/Scene/Search/SearchViewModel+LoadOldestState.swift index 6306e2e6..7088f136 100644 --- a/Mastodon/Scene/Search/SearchViewModel+LoadOldestState.swift +++ b/Mastodon/Scene/Search/SearchViewModel+LoadOldestState.swift @@ -53,23 +53,24 @@ extension SearchViewModel.LoadOldestState { } var offset = 0 switch viewModel.searchScope.value { - case Mastodon.API.Search.Scope.accounts.rawValue: + case Mastodon.API.Search.SearchType.accounts: offset = oldSearchResult.accounts.count - case Mastodon.API.Search.Scope.hashtags.rawValue: + case Mastodon.API.Search.SearchType.hashtags: offset = oldSearchResult.hashtags.count default: return } - let query = Mastodon.API.Search.Query(accountID: nil, + let query = Mastodon.API.Search.Query(q: viewModel.searchText.value, + type: viewModel.searchScope.value, + accountID: nil, maxID: nil, minID: nil, - type: viewModel.searchScope.value, excludeUnreviewed: nil, - q: viewModel.searchText.value, resolve: nil, limit: nil, offset: offset, following: nil) + viewModel.context.apiService.search(domain: activeMastodonAuthenticationBox.domain, query: query, mastodonAuthenticationBox: activeMastodonAuthenticationBox) .sink { completion in switch completion { @@ -81,7 +82,7 @@ extension SearchViewModel.LoadOldestState { } } receiveValue: { result in switch viewModel.searchScope.value { - case Mastodon.API.Search.Scope.accounts.rawValue: + case Mastodon.API.Search.SearchType.accounts: if result.value.accounts.isEmpty { stateMachine.enter(NoMore.self) } else { @@ -91,7 +92,7 @@ extension SearchViewModel.LoadOldestState { viewModel.searchResult.value = Mastodon.Entity.SearchResult(accounts: newAccounts.removeDuplicate(), statuses: oldSearchResult.statuses, hashtags: oldSearchResult.hashtags) stateMachine.enter(Idle.self) } - case Mastodon.API.Search.Scope.hashtags.rawValue: + case Mastodon.API.Search.SearchType.hashtags: if result.value.hashtags.isEmpty { stateMachine.enter(NoMore.self) } else { diff --git a/Mastodon/Scene/Search/SearchViewModel.swift b/Mastodon/Scene/Search/SearchViewModel.swift index df13ac48..06b654b3 100644 --- a/Mastodon/Scene/Search/SearchViewModel.swift +++ b/Mastodon/Scene/Search/SearchViewModel.swift @@ -22,7 +22,7 @@ final class SearchViewModel: NSObject { // output let searchText = CurrentValueSubject("") - let searchScope = CurrentValueSubject("") + let searchScope = CurrentValueSubject(Mastodon.API.Search.SearchType.default) let isSearching = CurrentValueSubject(false) @@ -68,12 +68,12 @@ final class SearchViewModel: NSObject { } .flatMap { (text, scope) -> AnyPublisher, Error> in - let query = Mastodon.API.Search.Query(accountID: nil, + let query = Mastodon.API.Search.Query(q: text, + type: scope, + accountID: nil, maxID: nil, minID: nil, - type: scope, excludeUnreviewed: nil, - q: text, resolve: nil, limit: nil, offset: nil, @@ -101,25 +101,27 @@ final class SearchViewModel: NSObject { searchScope ) .filter { isSearching, text, _ in - isSearching && text.isEmpty + isSearching } - .sink { [weak self] _, _, scope in + .sink { [weak self] _, text, scope in guard let self = self else { return } guard let searchHistories = self.fetchSearchHistory() else { return } guard let dataSource = self.searchResultDiffableDataSource else { return } var snapshot = NSDiffableDataSourceSnapshot() - snapshot.appendSections([.mixed]) - - searchHistories.forEach { searchHistory in - let containsAccount = scope == Mastodon.API.Search.Scope.accounts.rawValue || scope == "" - let containsHashTag = scope == Mastodon.API.Search.Scope.hashtags.rawValue || scope == "" - if let mastodonUser = searchHistory.account, containsAccount { - let item = SearchResultItem.accountObjectID(accountObjectID: mastodonUser.objectID) - snapshot.appendItems([item], toSection: .mixed) - } - if let tag = searchHistory.hashtag, containsHashTag { - let item = SearchResultItem.hashtagObjectID(hashtagObjectID: tag.objectID) - snapshot.appendItems([item], toSection: .mixed) + if text.isEmpty { + snapshot.appendSections([.mixed]) + + searchHistories.forEach { searchHistory in + let containsAccount = scope == Mastodon.API.Search.SearchType.accounts || scope == Mastodon.API.Search.SearchType.default + let containsHashTag = scope == Mastodon.API.Search.SearchType.hashtags || scope == Mastodon.API.Search.SearchType.default + if let mastodonUser = searchHistory.account, containsAccount { + let item = SearchResultItem.accountObjectID(accountObjectID: mastodonUser.objectID) + snapshot.appendItems([item], toSection: .mixed) + } + if let tag = searchHistory.hashtag, containsHashTag { + let item = SearchResultItem.hashtagObjectID(hashtagObjectID: tag.objectID) + snapshot.appendItems([item], toSection: .mixed) + } } } dataSource.apply(snapshot, animatingDifferences: false, completion: nil) @@ -166,7 +168,7 @@ final class SearchViewModel: NSObject { snapshot.appendSections([.account]) let items = accounts.compactMap { SearchResultItem.account(account: $0) } snapshot.appendItems(items, toSection: .account) - if self.searchScope.value == Mastodon.API.Search.Scope.accounts.rawValue && !items.isEmpty { + if self.searchScope.value == Mastodon.API.Search.SearchType.accounts && !items.isEmpty { snapshot.appendItems([.bottomLoader], toSection: .account) } } @@ -174,7 +176,7 @@ final class SearchViewModel: NSObject { snapshot.appendSections([.hashtag]) let items = tags.compactMap { SearchResultItem.hashtag(tag: $0) } snapshot.appendItems(items, toSection: .hashtag) - if self.searchScope.value == Mastodon.API.Search.Scope.hashtags.rawValue && !items.isEmpty { + if self.searchScope.value == Mastodon.API.Search.SearchType.hashtags && !items.isEmpty { snapshot.appendItems([.bottomLoader], toSection: .hashtag) } } diff --git a/Mastodon/Scene/Search/TableViewCell/SearchBottomLoader.swift b/Mastodon/Scene/Search/TableViewCell/SearchBottomLoader.swift index dcd4c497..7ab18bb0 100644 --- a/Mastodon/Scene/Search/TableViewCell/SearchBottomLoader.swift +++ b/Mastodon/Scene/Search/TableViewCell/SearchBottomLoader.swift @@ -40,7 +40,7 @@ final class SearchBottomLoader: UITableViewCell { func _init() { selectionStyle = .none - backgroundColor = Asset.Colors.lightWhite.color + backgroundColor = Asset.Colors.Background.systemGroupedBackground.color contentView.addSubview(activityIndicatorView) activityIndicatorView.constrainToCenter() } diff --git a/Mastodon/Scene/Search/TableViewCell/SearchingTableViewCell.swift b/Mastodon/Scene/Search/TableViewCell/SearchingTableViewCell.swift index cdfcdce2..aab8a570 100644 --- a/Mastodon/Scene/Search/TableViewCell/SearchingTableViewCell.swift +++ b/Mastodon/Scene/Search/TableViewCell/SearchingTableViewCell.swift @@ -20,7 +20,7 @@ final class SearchingTableViewCell: UITableViewCell { let _titleLabel: UILabel = { let label = UILabel() - label.textColor = Asset.Colors.buttonDefault.color + label.textColor = Asset.Colors.brandBlue.color label.font = .systemFont(ofSize: 17, weight: .semibold) label.lineBreakMode = .byTruncatingTail return label diff --git a/Mastodon/Scene/Search/View/SearchRecommendCollectionHeader.swift b/Mastodon/Scene/Search/View/SearchRecommendCollectionHeader.swift index df876a63..216f18f9 100644 --- a/Mastodon/Scene/Search/View/SearchRecommendCollectionHeader.swift +++ b/Mastodon/Scene/Search/View/SearchRecommendCollectionHeader.swift @@ -28,7 +28,7 @@ class SearchRecommendCollectionHeader: UIView { let seeAllButton: UIButton = { let button = UIButton(type: .custom) button.setTitleColor(Asset.Colors.brandBlue.color, for: .normal) - button.setTitle(L10n.Scene.Search.Recommend.buttontext, for: .normal) + button.setTitle(L10n.Scene.Search.Recommend.buttonText, for: .normal) return button }() diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift index dc6ef71e..be8bb260 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift @@ -50,9 +50,6 @@ extension Mastodon.API.Search { } extension Mastodon.API.Search { - public enum SearchType: String, Codable { - case ccounts, hashtags, statuses - } public struct Query: Codable, GetQuery { public init(q: String, @@ -109,9 +106,11 @@ extension Mastodon.API.Search { } public extension Mastodon.API.Search { - enum Scope: String { + enum SearchType: String, Codable { case accounts case hashtags + case statuses + case `default` public var rawValue: String { switch self { @@ -119,6 +118,10 @@ public extension Mastodon.API.Search { return "accounts" case .hashtags: return "hashtags" + case .statuses: + return "statuses" + case .default: + return "" } } }