From 107df9149d7e602eddcddcfa885fd30e89d39752 Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Thu, 20 Feb 2025 21:37:30 +0300 Subject: [PATCH] Removing old search screen --- AutoCat.xcodeproj/project.pbxproj | 4 - AutoCat/Base.lproj/Main.storyboard | 175 +---------- AutoCat/Controllers/SearchController.swift | 320 --------------------- 3 files changed, 2 insertions(+), 497 deletions(-) delete mode 100644 AutoCat/Controllers/SearchController.swift diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index b3c04d5..09b90c3 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -56,7 +56,6 @@ 7A3E12D72C7B42B700EE710D /* UserDefaults+Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A3E12D62C7B42B700EE710D /* UserDefaults+Settings.swift */; }; 7A3E30F32C18840600567704 /* ActivityItemSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A3E30F22C18840600567704 /* ActivityItemSource.swift */; }; 7A3F07AB24360DC800E59687 /* Dated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A3F07AA24360DC800E59687 /* Dated.swift */; }; - 7A3F07AD2436350B00E59687 /* SearchController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A3F07AC2436350B00E59687 /* SearchController.swift */; }; 7A4322912CB2CC8A00085CF6 /* FiltersScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A4322902CB2CC8A00085CF6 /* FiltersScreen.swift */; }; 7A4322932CB2CCAA00085CF6 /* FiltersViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A4322922CB2CCAA00085CF6 /* FiltersViewModel.swift */; }; 7A4322952CB2CD0F00085CF6 /* FiltersCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A4322942CB2CD0F00085CF6 /* FiltersCoordinator.swift */; }; @@ -330,7 +329,6 @@ 7A3E12D62C7B42B700EE710D /* UserDefaults+Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UserDefaults+Settings.swift"; sourceTree = ""; }; 7A3E30F22C18840600567704 /* ActivityItemSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityItemSource.swift; sourceTree = ""; }; 7A3F07AA24360DC800E59687 /* Dated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dated.swift; sourceTree = ""; }; - 7A3F07AC2436350B00E59687 /* SearchController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchController.swift; sourceTree = ""; }; 7A4322902CB2CC8A00085CF6 /* FiltersScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FiltersScreen.swift; sourceTree = ""; }; 7A4322922CB2CCAA00085CF6 /* FiltersViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FiltersViewModel.swift; sourceTree = ""; }; 7A4322942CB2CD0F00085CF6 /* FiltersCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FiltersCoordinator.swift; sourceTree = ""; }; @@ -645,7 +643,6 @@ 7A96AE2C246B2B7400297C33 /* GoogleSignInController.swift */, 7A11471523FDEB2A00B424AF /* MainSplitController.swift */, 7A27ADF2249F8B650035F39E /* RecordsController.swift */, - 7A3F07AC2436350B00E59687 /* SearchController.swift */, 7AC3554B29696A1C00889457 /* MainTabController.swift */, 7AC3554D29696C4500889457 /* DummyNewController.swift */, 7AC3554F29696D5A00889457 /* NewNumberController.swift */, @@ -1389,7 +1386,6 @@ 7A813DC32508EE4F00CC93B9 /* EventCell.swift in Sources */, 7A1441682C297EFD00E79018 /* NotesViewModel.swift in Sources */, 7AFBE8C02C3024E5003C491D /* ACHud.swift in Sources */, - 7A3F07AD2436350B00E59687 /* SearchController.swift in Sources */, 7AABDE26253350C30041AFC6 /* RxSectionedDataSource.swift in Sources */, 7AAAFADA2C4D1AFE0050410D /* Zoomable.swift in Sources */, 7A6DD90C24335A6D009DE740 /* FlagLayer.swift in Sources */, diff --git a/AutoCat/Base.lproj/Main.storyboard b/AutoCat/Base.lproj/Main.storyboard index 09f3db6..69d1d6b 100644 --- a/AutoCat/Base.lproj/Main.storyboard +++ b/AutoCat/Base.lproj/Main.storyboard @@ -8,146 +8,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -452,26 +312,7 @@ - - - - - - - - - - - - - - - - - - - - + @@ -508,19 +349,13 @@ - + - - - - - - @@ -530,14 +365,8 @@ - - - - - - diff --git a/AutoCat/Controllers/SearchController.swift b/AutoCat/Controllers/SearchController.swift deleted file mode 100644 index c38f45f..0000000 --- a/AutoCat/Controllers/SearchController.swift +++ /dev/null @@ -1,320 +0,0 @@ -import UIKit -import RealmSwift -import PKHUD -import ExceptionCatcher -import AutoCatCore - -class SearchController: UIViewController, UISearchResultsUpdating, UITableViewDelegate, UIScrollViewDelegate, UISearchBarDelegate { - - @IBOutlet weak var tableView: UITableView! - @IBOutlet weak var showMapButton: UIBarButtonItem? - - private var refreshButton: UIBarButtonItem! - private var refreshIndicator: UIBarButtonItem! - private var moreActionsButton: UIBarButtonItem? - - 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: SectionedDataSource! - private var isLoadingPage = false - private var pageLoadingIndicator = UIActivityIndicatorView(style: .medium) - - var filter = Filter() - - override func viewDidLoad() { - super.viewDidLoad() - - self.showMapButton?.isEnabled = false - - navigationItem.searchController = searchController - definesPresentationContext = true - - if #available(iOS 14.0, *) { - setupActionsMenu() - } - - self.refreshButton = UIBarButtonItem(image: UIImage(systemName: "arrow.triangle.2.circlepath"), - style: .plain, - target: self, - action: #selector(refresh)) - - self.refreshIndicator = UIBarButtonItem(customView: self.pageLoadingIndicator) - #if targetEnvironment(macCatalyst) - self.navigationItem.leftBarButtonItem = self.refreshButton - #endif - - //self.refreshControl.attributedTitle = NSAttributedString(string: "") - self.refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged) - self.tableView.addSubview(self.refreshControl) - - self.datasource = SectionedDataSource(table: self.tableView) - self.tableView.delegate = self - self.tableView.keyboardDismissMode = .onDrag - - updateSearchResults(with: filter) - } - - func updateSearchResults(with filter: Filter) { - Task { - showProgress() - - if filter.needReset { - self.datasource.reset() - } - - let vehicles = (try? await ApiService.shared.getVehicles(with: filter, pageToken: self.datasource.pageToken, pageSize: 50)) ?? PagedResponse() - - if let count = vehicles.count { - self.navigationItem.title = String.localizedStringWithFormat(NSLocalizedString("vehicles found", comment: ""), count) - self.showMapButton?.isEnabled = count > 0 - } - - refreshControl.endRefreshing() - isLoadingPage = false - pageLoadingIndicator.stopAnimating() - hideProgress() - datasource.update(with: vehicles) - } - } - - func showProgress() { - navigationItem.leftBarButtonItem = self.refreshIndicator - pageLoadingIndicator.startAnimating() - moreActionsButton?.isEnabled = false - } - - func hideProgress() { - #if targetEnvironment(macCatalyst) - navigationItem.leftBarButtonItem = self.refreshButton - #else - navigationItem.leftBarButtonItem = nil - #endif - - moreActionsButton?.isEnabled = true - } - - // FIXME: Code duplication - func updateDetailController(with vehicle: VehicleDto, indexPath: IndexPath) { - if let splitViewController = self.view.window?.rootViewController as? UISplitViewController - { - Task { - let coordinator = ReportCoordinator(controller: splitViewController, vehicle: vehicle, isPersistent: false) - if let updatedVehicle = try? await coordinator.start() { - datasource.set(item: updatedVehicle, at: indexPath) - tableView.reloadData() - } - } - } - } - - // MARK: - UISearchResultsUpdating - - func updateSearchResults(for searchController: UISearchController) { - let newQuery = searchController.searchBar.text?.uppercased() ?? "" - guard self.filter.searchString != newQuery else { return } - - self.filter.searchString = newQuery - self.filter.needReset = true - self.filter.scope = SearchScope(rawValue: searchController.searchBar.selectedScopeButtonIndex) ?? .plateNumber - - updateSearchResults(with: filter) - } - - func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { - guard let scope = SearchScope(rawValue: selectedScope) else { - return - } - - filter.scope = scope - updateSearchResults(with: filter) - } - - // MARK: NavigationBar actions - - @available(iOS 14.0, *) - func setupActionsMenu() { - - let menu = UIMenu(children: [ - UIAction(title: NSLocalizedString("Filter results", comment: ""), - image: UIImage(systemName: "line.horizontal.3.decrease"), - handler: { [weak self] _ in Task { try? await self?.showFilter() } }), - UIAction(title: NSLocalizedString("Show on map", comment: ""), - image: UIImage(systemName: "map"), - handler: { _ in self.showOnMap() }), - UIAction(title: NSLocalizedString("Export", comment: ""), - image: UIImage(systemName: "square.and.arrow.up"), - handler: { _ in self.exportSearchResults() }) - ]) - - let menuBarButton = UIBarButtonItem(title: nil, image: UIImage(systemName: "ellipsis"), primaryAction: nil, menu: menu) - self.navigationItem.rightBarButtonItems = [menuBarButton] - self.moreActionsButton = menuBarButton - } - - @IBAction func onFilterTapped(_ sender: UIBarButtonItem) { - Task { try? await showFilter() } - } - - @IBAction func onMapTapped(_ sender: UIBarButtonItem) { - showOnMap() - } - - @objc func refresh(_ sender: AnyObject) { - self.showMapButton?.isEnabled = false - self.filter.needReset = true - updateSearchResults(with: filter) - } - - func showFilter() async throws { -// let sb = UIStoryboard(name: "Main", bundle: nil) -// let controller = sb.instantiateViewController(identifier: "FiltersController") as FiltersController -// controller.filter = self.filter -// controller.onDone = { -// 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.updateSearchResults(with: self.filter) -// } -// self.navigationController?.pushViewController(controller, animated: true) - - if let navigationController = self.navigationController { - let coordinator = FiltersCoordinator(navController: navigationController, filter: filter) - if let newFilter = try await coordinator.start() { - filter = newFilter - datasource.setSortParameter(self.filter.sortBy) - filter.needReset = true - filter.scope = SearchScope(rawValue: self.searchController.searchBar.selectedScopeButtonIndex) ?? .plateNumber - updateSearchResults(with: self.filter) - } - } - } - - func showOnMap() { - let sb = UIStoryboard(name: "Main", bundle: nil) - let controller = sb.instantiateViewController(identifier: "GlobalEventsNavigation") as UINavigationController - if let eventsVC = controller.viewControllers.first as? GlobalEventsController { - eventsVC.filter = self.filter - } - - controller.modalPresentationStyle = .fullScreen - self.present(controller, animated: true) - } - - func exportSearchResults() { - Task { - do { - showProgress() - let resp = try await ApiService.shared.getVehicles(with: filter, pageSize: 0) - - let newLine = "\r\n" - var csvString = VehicleDto.csvHeader + newLine - - for vehicle in resp.items { - csvString.append(vehicle.csvLine) - csvString.append(newLine) - } - - let tmpUrl = FileManager.default.tmpUrl(name: "search", ext: "csv") - try csvString.write(to: tmpUrl, atomically: true, encoding: .utf8) -#if targetEnvironment(macCatalyst) - self.save(file: tmpUrl) -#else - self.share(file: tmpUrl) -#endif - - hideProgress() - } catch { - hideProgress() - HUD.show(error: error) - } - } - } - - func share(file url: URL) { - let activityController = UIActivityViewController(activityItems: [url], applicationActivities: nil) - self.present(activityController, animated: true) - } - - func save(file url: URL) { - if #available(iOS 14, *) { - let controller = UIDocumentPickerViewController(forExporting: [url]) - self.present(controller, animated: true) - } else { - let controller = UIDocumentPickerViewController(url: url, in: .exportToService) - present(controller, animated: true) - } - } - - // MARK: - UITableViewDelegate - - func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { - let vehicle = self.datasource.item(at: indexPath) - let updateAction = UIContextualAction(style: .normal, title: NSLocalizedString("Update", comment: "")) { action, view, completion in - self.update(vehicle: vehicle, at: indexPath) - completion(true) - } - updateAction.image = UIImage(systemName: "arrow.2.circlepath") - updateAction.backgroundColor = .systemBlue - - let configuration = UISwipeActionsConfiguration(actions: [updateAction]) - configuration.performsFirstActionWithFullSwipe = false - return configuration - } - - func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { - let vehicle = self.datasource.item(at: indexPath) - - return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in - let update = UIAction(title: NSLocalizedString("Update", comment: ""), image: UIImage(systemName: "arrow.2.circlepath")) { action in - self.update(vehicle: vehicle, at: indexPath) - } - - return UIMenu(title: NSLocalizedString("Actions", comment: ""), children: [update]) - } - } - - func update(vehicle: VehicleDto, at indexPath: IndexPath) { - - Task { - do { - HUD.show(.progress) - let newVehicle = try await ApiService.shared.checkVehicle(by: vehicle.getNumber(), notes: vehicle.notes, events: [], force: true) - HUD.hide() - let realm = try await Realm() - if realm.object(ofType: Vehicle.self, forPrimaryKey: vehicle.getNumber()) != nil { - try realm.write { - realm.add(Vehicle(dto: newVehicle), update: .all) - } - } - datasource.set(item: newVehicle, at: indexPath) - updateDetailController(with: newVehicle, indexPath: indexPath) - } catch { - HUD.hide() - show(error: error) - } - } - } - - func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - let vehicle = self.datasource.item(at: indexPath) - self.updateDetailController(with: vehicle, indexPath: indexPath) - } - - func scrollViewDidScroll(_ scrollView: UIScrollView) { - guard tableView.contentSize.height > 0 else { return } - - let toBottom = tableView.contentSize.height - (tableView.contentOffset.y + tableView.frame.size.height) - if toBottom < 100 && !self.isLoadingPage && self.datasource.needMoreData() { - self.isLoadingPage = true - self.filter.needReset = false - updateSearchResults(with: filter) - } - } -}