Removing old search screen

This commit is contained in:
Selim Mustafaev 2025-02-20 21:37:30 +03:00
parent c587783e86
commit 107df9149d
3 changed files with 2 additions and 497 deletions

View File

@ -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 = "<group>"; };
7A3E30F22C18840600567704 /* ActivityItemSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityItemSource.swift; sourceTree = "<group>"; };
7A3F07AA24360DC800E59687 /* Dated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dated.swift; sourceTree = "<group>"; };
7A3F07AC2436350B00E59687 /* SearchController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchController.swift; sourceTree = "<group>"; };
7A4322902CB2CC8A00085CF6 /* FiltersScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FiltersScreen.swift; sourceTree = "<group>"; };
7A4322922CB2CCAA00085CF6 /* FiltersViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FiltersViewModel.swift; sourceTree = "<group>"; };
7A4322942CB2CD0F00085CF6 /* FiltersCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FiltersCoordinator.swift; sourceTree = "<group>"; };
@ -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 */,

View File

@ -8,146 +8,6 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Search Controller-->
<scene sceneID="3Md-yW-a0R">
<objects>
<viewController id="UPf-uT-oOr" customClass="SearchController" customModule="AutoCat" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="IJP-gV-Ojc">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="dB3-iP-QRo">
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="VehicleCell" id="VEP-QD-i6y" customClass="VehicleCell" customModule="AutoCat" customModuleProvider="target">
<rect key="frame" x="0.0" y="50" width="375" height="84.5"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="VEP-QD-i6y" id="8hH-8I-XLB">
<rect key="frame" x="0.0" y="0.0" width="375" height="84.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="E0H-HY-s2L">
<rect key="frame" x="8" y="8" width="359" height="68.5"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" spacing="4" translatesAutoresizingMaskIntoConstraints="NO" id="KFu-ew-wX3">
<rect key="frame" x="0.0" y="0.0" width="359" height="20"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" verticalHuggingPriority="251" horizontalCompressionResistancePriority="749" text="Kia Optima" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="wCR-6h-HgF">
<rect key="frame" x="0.0" y="0.0" width="290.5" height="20"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="exclamationmark.arrow.triangle.2.circlepath" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="Qo7-51-ou9">
<rect key="frame" x="294" y="0.5" width="21" height="19"/>
<color key="tintColor" systemColor="systemOrangeColor"/>
<constraints>
<constraint firstAttribute="width" constant="20" id="Pct-H5-e3e"/>
<constraint firstAttribute="height" constant="20" id="UBl-5J-zAY"/>
</constraints>
</imageView>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="751" image="text.bubble" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="k5v-Ic-Stn">
<rect key="frame" x="318.5" y="0.5" width="20" height="19.5"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="M1i-uQ-eYH"/>
<constraint firstAttribute="width" constant="20" id="wyl-SJ-L78"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="12" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="QnE-Cx-aUe">
<rect key="frame" x="342.5" y="0.0" width="16.5" height="20"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleCallout"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</stackView>
<stackView opaque="NO" tag="8" contentMode="scaleToFill" alignment="bottom" translatesAutoresizingMaskIntoConstraints="NO" id="vMC-Bk-8rZ">
<rect key="frame" x="0.0" y="28" width="359" height="40.5"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="cvf-vM-QnT" customClass="PlateView" customModule="AutoCat" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.5" width="303" height="40"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="Xoz-Iw-PCU"/>
</constraints>
</view>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="bottom" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="quG-e3-h5v">
<rect key="frame" x="303" y="0.5" width="56" height="40"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="751" text="1.01.2021" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="RPA-NR-0C6">
<rect key="frame" x="0.0" y="0.0" width="56" height="16"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleFootnote"/>
<color key="textColor" systemColor="secondaryLabelColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="aOM-gr-Yem">
<rect key="frame" x="23" y="24" width="33" height="16"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleFootnote"/>
<color key="textColor" systemColor="tertiaryLabelColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</stackView>
</subviews>
</stackView>
</subviews>
</stackView>
</subviews>
<constraints>
<constraint firstAttribute="bottom" secondItem="E0H-HY-s2L" secondAttribute="bottom" constant="8" id="Ijr-HL-UZx"/>
<constraint firstItem="E0H-HY-s2L" firstAttribute="top" secondItem="8hH-8I-XLB" secondAttribute="top" constant="8" id="SbP-00-gE3"/>
<constraint firstItem="E0H-HY-s2L" firstAttribute="leading" secondItem="8hH-8I-XLB" secondAttribute="leading" constant="8" id="V57-T6-4bW"/>
<constraint firstAttribute="trailing" secondItem="E0H-HY-s2L" secondAttribute="trailing" constant="8" id="hxm-tH-1Ja"/>
</constraints>
</tableViewCellContentView>
<inset key="separatorInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
<connections>
<outlet property="addedDate" destination="aOM-gr-Yem" id="aQL-6B-r1G"/>
<outlet property="bubbleImage" destination="k5v-Ic-Stn" id="bZR-Qo-0FU"/>
<outlet property="name" destination="wCR-6h-HgF" id="rXr-iq-H2Y"/>
<outlet property="notesCount" destination="QnE-Cx-aUe" id="KlD-As-G9y"/>
<outlet property="plate" destination="cvf-vM-QnT" id="8Bm-gA-bbm"/>
<outlet property="syncImage" destination="Qo7-51-ou9" id="4fV-fJ-Q4y"/>
<outlet property="updatedDate" destination="RPA-NR-0C6" id="c0e-Ew-Bc3"/>
</connections>
</tableViewCell>
</prototypes>
</tableView>
</subviews>
<viewLayoutGuide key="safeArea" id="6Uh-9i-MKR"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="6Uh-9i-MKR" firstAttribute="bottom" secondItem="dB3-iP-QRo" secondAttribute="bottom" id="Qq7-Rm-AvA"/>
<constraint firstItem="dB3-iP-QRo" firstAttribute="top" secondItem="6Uh-9i-MKR" secondAttribute="top" id="SUK-IH-xTR"/>
<constraint firstItem="dB3-iP-QRo" firstAttribute="leading" secondItem="6Uh-9i-MKR" secondAttribute="leading" id="XTb-EI-bvm"/>
<constraint firstItem="dB3-iP-QRo" firstAttribute="trailing" secondItem="6Uh-9i-MKR" secondAttribute="trailing" id="w3e-Ir-qlo"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="kEp-Rw-7hu">
<barButtonItem key="backBarButtonItem" title="Back" id="7QQ-Qi-qEw"/>
<rightBarButtonItems>
<barButtonItem image="line.horizontal.3.decrease" catalog="system" id="mvq-Q5-tVc">
<connections>
<action selector="onFilterTapped:" destination="UPf-uT-oOr" id="eCM-JW-z1h"/>
</connections>
</barButtonItem>
<barButtonItem image="map" catalog="system" id="iVh-uQ-fX5">
<connections>
<action selector="onMapTapped:" destination="UPf-uT-oOr" id="ekk-vL-cGI"/>
</connections>
</barButtonItem>
</rightBarButtonItems>
</navigationItem>
<connections>
<outlet property="showMapButton" destination="iVh-uQ-fX5" id="19X-3N-bDb"/>
<outlet property="tableView" destination="dB3-iP-QRo" id="b3n-R9-6lI"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="xsk-7S-rvc" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="3553" y="143"/>
</scene>
<!--Voice records-->
<scene sceneID="9pI-G0-wG0">
<objects>
@ -452,26 +312,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="XB6-0a-b8N" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="7518" y="144"/>
</scene>
<!--Search-->
<scene sceneID="kiS-EQ-VFl">
<objects>
<placeholder placeholderIdentifier="IBFirstResponder" id="XQB-kc-hUv" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="GCa-Re-j14" sceneMemberID="viewController">
<tabBarItem key="tabBarItem" title="Search" image="search" landscapeImage="search-compact" id="gDG-z8-R0t"/>
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="vdY-9n-hjX">
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="UPf-uT-oOr" kind="relationship" relationship="rootViewController" id="aun-Tj-SJT"/>
</connections>
</navigationController>
</objects>
<point key="canvasLocation" x="2614" y="143"/>
<point key="canvasLocation" x="3554" y="143"/>
</scene>
<!--Records-->
<scene sceneID="oyu-oz-pC4">
@ -508,19 +349,13 @@
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Zbo-fQ-UCM" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="6577" y="143"/>
<point key="canvasLocation" x="2614" y="142"/>
</scene>
</scenes>
<resources>
<image name="exclamationmark.arrow.triangle.2.circlepath" catalog="system" width="128" height="117"/>
<image name="line.horizontal.3.decrease" catalog="system" width="128" height="73"/>
<image name="map" catalog="system" width="128" height="112"/>
<image name="play.fill" catalog="system" width="120" height="128"/>
<image name="record" width="31" height="31"/>
<image name="record-compact" width="23" height="23"/>
<image name="search" width="23" height="23"/>
<image name="search-compact" width="17" height="17"/>
<image name="text.bubble" catalog="system" width="128" height="110"/>
<systemColor name="secondaryLabelColor">
<color red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.59999999999999998" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
@ -530,14 +365,8 @@
<systemColor name="systemBlueColor">
<color red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="systemOrangeColor">
<color red="1" green="0.58431372550000005" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="systemTealColor">
<color red="0.18823529410000001" green="0.69019607839999997" blue="0.78039215689999997" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="tertiaryLabelColor">
<color red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.29803921570000003" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>

View File

@ -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<VehicleDto,VehicleCell>!
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<VehicleDto>()
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)
}
}
}