From 57930a46ce82ed94dcb1cd3ad71e354f90bcc227 Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Sun, 20 Mar 2022 09:58:24 +0300 Subject: [PATCH] Fix error when event was lost after add(event:) error --- .gitignore | 2 + AutoCat.xcodeproj/project.pbxproj | 4 +- .../xcschemes/xcschememanagement.plist | 98 +++++++++---------- AutoCat/Controllers/CheckController.swift | 26 +++-- .../Location/EventsController.swift | 21 +++- 5 files changed, 87 insertions(+), 64 deletions(-) diff --git a/.gitignore b/.gitignore index 4a4e88f..0018d07 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ design/generated/ .DS_Store AutoCat.xcodeproj/project.xcworkspace/xcuserdata/ AutoCat.xcodeproj/xcuserdata/ +xcuserdata/ .build/ +.idea/ diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index 12aafb5..17d8af4 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -1080,7 +1080,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 89; + CURRENT_PROJECT_VERSION = 90; DEVELOPMENT_TEAM = 46DTTB8X4S; INFOPLIST_FILE = AutoCat/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 13.0; @@ -1105,7 +1105,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 89; + CURRENT_PROJECT_VERSION = 90; DEVELOPMENT_TEAM = 46DTTB8X4S; INFOPLIST_FILE = AutoCat/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 13.0; diff --git a/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist b/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist index 5652784..3188224 100644 --- a/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist @@ -17,210 +17,210 @@ DifferenceKit (Playground) 1.xcscheme isShown - + orderHint 9 DifferenceKit (Playground) 2.xcscheme isShown - + orderHint 10 DifferenceKit (Playground).xcscheme isShown - + orderHint 8 Eureka (Playground) 1.xcscheme isShown - + orderHint 3 Eureka (Playground) 2.xcscheme isShown - + orderHint 4 Eureka (Playground) 3.xcscheme isShown - + orderHint - 17 + 18 Eureka (Playground) 4.xcscheme isShown - + orderHint - 18 + 19 Eureka (Playground) 5.xcscheme isShown - + orderHint - 19 + 21 Eureka (Playground) 6.xcscheme isShown - + orderHint - 20 + 23 Eureka (Playground) 7.xcscheme isShown - + orderHint - 21 + 25 Eureka (Playground) 8.xcscheme isShown - + orderHint - 22 + 27 Eureka (Playground).xcscheme isShown - + orderHint - 1 + 2 GettingStarted (Playground) 1.xcscheme isShown - + orderHint - 15 + 16 GettingStarted (Playground) 2.xcscheme isShown - + orderHint - 16 + 17 GettingStarted (Playground) 3.xcscheme isShown - + orderHint 14 GettingStarted (Playground) 4.xcscheme isShown - + orderHint - 18 + 20 GettingStarted (Playground) 5.xcscheme isShown - + orderHint - 21 + 26 GettingStarted (Playground).xcscheme isShown - + orderHint 12 Rx (Playground) 1.xcscheme isShown - + orderHint 6 Rx (Playground) 2.xcscheme isShown - + orderHint 7 Rx (Playground) 3.xcscheme isShown - + orderHint - 19 + 22 Rx (Playground) 4.xcscheme isShown - + orderHint - 20 + 24 Rx (Playground) 5.xcscheme isShown - + orderHint - 22 + 28 Rx (Playground).xcscheme isShown - + orderHint 5 SwiftDate (Playground) 1.xcscheme isShown - + orderHint 13 SwiftDate (Playground) 2.xcscheme isShown - + orderHint - 14 + 15 SwiftDate (Playground) 3.xcscheme isShown - + orderHint - 24 + 29 SwiftDate (Playground) 4.xcscheme isShown - + orderHint - 26 + 30 SwiftDate (Playground) 5.xcscheme isShown - + orderHint - 28 + 31 SwiftDate (Playground).xcscheme isShown - + orderHint 11 @@ -230,7 +230,7 @@ 7A1146FC23FDE7E500B424AF primary - + diff --git a/AutoCat/Controllers/CheckController.swift b/AutoCat/Controllers/CheckController.swift index 1f8b9ea..2083859 100644 --- a/AutoCat/Controllers/CheckController.swift +++ b/AutoCat/Controllers/CheckController.swift @@ -216,7 +216,7 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat var events: [VehicleEvent] = [] do { let realm = try Realm() - if let dbVehicle = realm.object(ofType: Vehicle.self, forPrimaryKey: numberNormalized), dbVehicle.unrecognized { + if let dbVehicle = realm.object(ofType: Vehicle.self, forPrimaryKey: numberNormalized) { events.append(contentsOf: dbVehicle.events.map { $0.clone() }) } } catch { @@ -398,7 +398,7 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat } return Single.zip(eventSingle, checkSingle).flatMap { eventResult, vehicleResult in - let errors = [eventResult.error, vehicleResult.error].map { error -> Error? in + var errors = [eventResult.error, vehicleResult.error].map { error -> Error? in if let clerror = error as? CLError { if clerror.code != .denied { return CocoaError.error(NSLocalizedString("Location error", comment: ""), reason: clerror.code.description) @@ -413,15 +413,17 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat RxLocationManager.resetLastEvent() - if vehicleResult.error != nil { - let realm = try Realm() - if let event = eventResult.event { - try realm.write { - vehicleResult.vehicle.events.append(event) - vehicleResult.vehicle.updatedDate = Date().timeIntervalSince1970 - vehicleResult.vehicle.synchronized = false - } + let realm = try Realm() + let dbVehicle = realm.object(ofType: Vehicle.self, forPrimaryKey: vehicleResult.vehicle.getNumber()) + if let event = eventResult.event, let vehicle = dbVehicle { + try realm.write { + vehicle.events.append(event) + vehicle.updatedDate = Date().timeIntervalSince1970 + vehicle.synchronized = false } + } + + if vehicleResult.error != nil { return .just((vehicle: vehicleResult.vehicle, errors: errors)) } else { if let event = eventResult.event { @@ -431,6 +433,10 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat try self.save(vehicle: $0) return (vehicle: $0, errors: errors) } + .catchError { error in + errors.append(error) + return .just((vehicle: vehicleResult.vehicle, errors: errors)) + } } else { return .just((vehicle: vehicleResult.vehicle, errors: errors)) } diff --git a/AutoCat/Controllers/Location/EventsController.swift b/AutoCat/Controllers/Location/EventsController.swift index 8e73475..cf056a0 100644 --- a/AutoCat/Controllers/Location/EventsController.swift +++ b/AutoCat/Controllers/Location/EventsController.swift @@ -12,11 +12,13 @@ class EventPin: NSObject, MKAnnotation { var coordinate: CLLocationCoordinate2D var title: String? var subtitle: String? + var id: String - init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String) { + init(id: String, coordinate: CLLocationCoordinate2D, title: String?, subtitle: String) { self.coordinate = coordinate self.title = title self.subtitle = subtitle + self.id = id } convenience init(event: VehicleEvent) { @@ -29,9 +31,9 @@ class EventPin: NSObject, MKAnnotation { let dateStr = formatter.string(from: date) if let number = event.number { - self.init(coordinate: coordinate, title: number, subtitle: dateStr) + self.init(id: event.id, coordinate: coordinate, title: number, subtitle: dateStr) } else { - self.init(coordinate: coordinate, title: dateStr, subtitle: address) + self.init(id: event.id, coordinate: coordinate, title: dateStr, subtitle: address) } } } @@ -189,6 +191,19 @@ class EventsController: UIViewController, UITableViewDataSource, UITableViewDele return configuration } + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + guard let event = self.vehicle?.events[indexPath.row], + let pin = pins.first(where: { $0.id == event.id }) else { + return + } + + tableView.deselectRow(at: indexPath, animated: true) + switchMode(modeButton) + + map.setCenter(pin.coordinate, animated: true) + map.selectAnnotation(pin, animated: true) + } + // MARK: - Event actions func deleteEvent(index: Int, completion: ((Bool) -> Void)? = nil) {