From 69fdd322b001f6e8e3d0ec01fdb88a83d3983146 Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Sun, 4 Jun 2023 21:02:21 +0300 Subject: [PATCH] Additional search scopes - vin and notes --- AutoCat.xcodeproj/project.pbxproj | 4 +-- .../Extensions/SearchControllerExt.swift | 17 ++++++++++ AutoCat/Controllers/NewNumberController.swift | 31 ++++++++++++++++++- AutoCat/Controllers/SearchController.swift | 16 +++++++++- AutoCatCore/Models/Filter.swift | 27 ++++++++++++++++ 5 files changed, 91 insertions(+), 4 deletions(-) diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index 3aeeb32..e0cec14 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -1150,7 +1150,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 114; + CURRENT_PROJECT_VERSION = 116; DEVELOPMENT_TEAM = 46DTTB8X4S; INFOPLIST_FILE = AutoCat/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = AutoCat; @@ -1178,7 +1178,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 114; + CURRENT_PROJECT_VERSION = 116; DEVELOPMENT_TEAM = 46DTTB8X4S; INFOPLIST_FILE = AutoCat/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = AutoCat; diff --git a/AutoCat/ACUIKit/Extensions/SearchControllerExt.swift b/AutoCat/ACUIKit/Extensions/SearchControllerExt.swift index abfaf3e..215446d 100644 --- a/AutoCat/ACUIKit/Extensions/SearchControllerExt.swift +++ b/AutoCat/ACUIKit/Extensions/SearchControllerExt.swift @@ -15,6 +15,13 @@ extension UISearchController { searchController.obscuresBackgroundDuringPresentation = false searchController.hidesNavigationBarDuringPresentation = false searchController.searchBar.keyboardType = .webSearch + + if #available(iOS 16, *) { + searchController.scopeBarActivation = .onTextEntry + } else { + searchController.automaticallyShowsScopeBar = true + } + return searchController } @@ -37,4 +44,14 @@ extension UISearchController { searchBar.smartInsertDeleteType = .no return self } + + func scopeButtons(_ buttons: [String]) -> UISearchController { + searchBar.scopeButtonTitles = buttons + return self + } + + func searchBarDelegate(_ delegate: UISearchBarDelegate) -> UISearchController { + searchBar.delegate = delegate + return self + } } diff --git a/AutoCat/Controllers/NewNumberController.swift b/AutoCat/Controllers/NewNumberController.swift index 36271d1..c004b97 100644 --- a/AutoCat/Controllers/NewNumberController.swift +++ b/AutoCat/Controllers/NewNumberController.swift @@ -47,12 +47,41 @@ class NewNumberController: UIViewController { }() private lazy var mainStackView: UIStackView = { - let stack = UIStackView(arrangedSubviews: [titleLabel, stackView, keyboardView]) + let stack = UIStackView(arrangedSubviews: [titleLabel, stackView, settingsStackView, keyboardView]) stack.axis = .vertical stack.spacing = 16 stack.translatesAutoresizingMaskIntoConstraints = false return stack }() + + private lazy var locationSwitcherView: UISegmentedControl = { + let view = UISegmentedControl() + view.translatesAutoresizingMaskIntoConstraints = false + view.insertSegment(with: UIImage(systemName: "location.fill"), at: 0, animated: false) + view.insertSegment(with: UIImage(systemName: "location"), at: 1, animated: false) + view.insertSegment(with: UIImage(systemName: "location.slash"), at: 2, animated: false) + return view + }() + + private lazy var numTypeSwitcherView: UISegmentedControl = { + let view = UISegmentedControl() + view.translatesAutoresizingMaskIntoConstraints = false + view.insertSegment(withTitle: "ГРЗ", at: 0, animated: false) + view.insertSegment(withTitle: "VIN", at: 1, animated: false) + view.selectedSegmentIndex = 0 + view.isEnabled = false + return view + }() + + private lazy var settingsStackView: UIStackView = { + let stack = UIStackView(arrangedSubviews: [numTypeSwitcherView, locationSwitcherView]) + stack.axis = .horizontal + stack.spacing = 16 + stack.distribution = .fillProportionally + stack.translatesAutoresizingMaskIntoConstraints = false + stack.isHidden = true + return stack + }() override func viewDidLoad() { super.viewDidLoad() diff --git a/AutoCat/Controllers/SearchController.swift b/AutoCat/Controllers/SearchController.swift index 05487dc..ff7f80c 100644 --- a/AutoCat/Controllers/SearchController.swift +++ b/AutoCat/Controllers/SearchController.swift @@ -6,7 +6,7 @@ import PKHUD import ExceptionCatcher import AutoCatCore -class SearchController: UIViewController, UISearchResultsUpdating, UITableViewDelegate, UIScrollViewDelegate { +class SearchController: UIViewController, UISearchResultsUpdating, UITableViewDelegate, UIScrollViewDelegate, UISearchBarDelegate { @IBOutlet weak var tableView: UITableView! @IBOutlet weak var showMapButton: UIBarButtonItem? @@ -20,7 +20,9 @@ class SearchController: UIViewController, UISearchResultsUpdating, UITableViewDe private lazy var searchController: UISearchController = .default .placeholder(NSLocalizedString("Search plate numbers", comment: "")) .resultsUpdater(self) + .searchBarDelegate(self) .makeDumb() + .scopeButtons(SearchScope.allCases.map(\.title)) private var refreshControl = UIRefreshControl() private var datasource: RxSectionedDataSource! @@ -136,9 +138,20 @@ class SearchController: UIViewController, UISearchResultsUpdating, UITableViewDe self.filter.searchString = newQuery self.filter.needReset = true + self.filter.scope = SearchScope(rawValue: searchController.searchBar.selectedScopeButtonIndex) ?? .plateNumber + self.filterRelay.accept(self.filter) } + func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { + guard let scope = SearchScope(rawValue: selectedScope) else { + return + } + + filter.scope = scope + filterRelay.accept(filter) + } + // MARK: NavigationBar actions @available(iOS 14.0, *) @@ -183,6 +196,7 @@ class SearchController: UIViewController, UISearchResultsUpdating, UITableViewDe self.filter = controller.filter self.datasource.setSortParameter(self.filter.sortBy ?? .updatedDate) self.filter.needReset = true + self.filter.scope = SearchScope(rawValue: self.searchController.searchBar.selectedScopeButtonIndex) ?? .plateNumber self.filterRelay.accept(self.filter) } self.navigationController?.pushViewController(controller, animated: true) diff --git a/AutoCatCore/Models/Filter.swift b/AutoCatCore/Models/Filter.swift index 9a7438f..0c47b9d 100644 --- a/AutoCatCore/Models/Filter.swift +++ b/AutoCatCore/Models/Filter.swift @@ -38,6 +38,29 @@ public enum SortOrder: String, CustomStringConvertible, CaseIterable { } } +public enum SearchScope: Int, CaseIterable { + + case plateNumber = 0 + case vin = 1 + case notes = 2 + + public var title: String { + switch self { + case .plateNumber: return NSLocalizedString("Plate number", comment: "") + case .vin: return NSLocalizedString("VIN", comment: "") + case .notes: return NSLocalizedString("Notes", comment: "") + } + } + + public var stringValue: String { + switch self { + case .plateNumber: return "plateNumber" + case .vin: return "vin" + case .notes: return "notes" + } + } +} + public struct Filter { public var searchString = "" public var brand: String? @@ -55,6 +78,7 @@ public struct Filter { public var fromLocationDate: Date? public var toLocationDate: Date? public var needReset: Bool = false + public var scope: SearchScope = .plateNumber public init() { } @@ -74,6 +98,7 @@ public struct Filter { self.toDateUpdated = nil self.fromLocationDate = nil self.toLocationDate = nil + self.scope = .plateNumber } public func queryDictionary() -> [String: String] { @@ -122,6 +147,8 @@ public struct Filter { dict["toLocationDate"] = String(toLocationDate.timeIntervalSince1970) } + dict["scope"] = scope.stringValue + return dict } }