From cc9a645fdcd6268c597aff3481f5a019546f3d65 Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Sun, 25 Oct 2020 21:14:16 +0300 Subject: [PATCH] Adjusting code for new report type --- AutoCat.xcodeproj/project.pbxproj | 4 +- .../xcschemes/xcschememanagement.plist | 24 ++++++------ AutoCat/AppDelegate.swift | 12 +++++- AutoCat/Controllers/CheckController.swift | 2 +- AutoCat/Controllers/OwnersController.swift | 31 ++++++++++++++- AutoCat/Controllers/ReportController.swift | 2 +- AutoCat/Extensions/VehicleReportImage.swift | 2 +- AutoCat/Models/Vehicle.swift | 9 ++++- AutoCat/Utils/RxRealmDataSource.swift | 38 ++++++++++++------- 9 files changed, 90 insertions(+), 34 deletions(-) diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index 4752346..18ec872 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -728,7 +728,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 49; + CURRENT_PROJECT_VERSION = 51; DEVELOPMENT_TEAM = 46DTTB8X4S; INFOPLIST_FILE = AutoCat/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 13.0; @@ -750,7 +750,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 49; + CURRENT_PROJECT_VERSION = 51; 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 ce4c584..9964a67 100644 --- a/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcschemes/xcschememanagement.plist @@ -14,35 +14,35 @@ isShown orderHint - 5 + 3 DifferenceKit (Playground) 2.xcscheme isShown orderHint - 6 + 4 DifferenceKit (Playground).xcscheme isShown orderHint - 4 + 2 Eureka (Playground) 1.xcscheme isShown orderHint - 8 + 6 Eureka (Playground) 2.xcscheme isShown orderHint - 9 + 7 Eureka (Playground) 3.xcscheme @@ -70,7 +70,7 @@ isShown orderHint - 7 + 5 GettingStarted (Playground) 1.xcscheme @@ -112,28 +112,28 @@ isShown orderHint - 12 + 13 Rx (Playground) 1.xcscheme isShown orderHint - 1 + 9 Rx (Playground) 2.xcscheme isShown orderHint - 3 + 10 Rx (Playground).xcscheme isShown orderHint - 2 + 8 SwiftDate (Playground) 1.xcscheme @@ -147,7 +147,7 @@ isShown orderHint - 13 + 12 SwiftDate (Playground) 3.xcscheme @@ -175,7 +175,7 @@ isShown orderHint - 10 + 1 SuppressBuildableAutocreation diff --git a/AutoCat/AppDelegate.swift b/AutoCat/AppDelegate.swift index cda7a91..d5cd7b7 100644 --- a/AutoCat/AppDelegate.swift +++ b/AutoCat/AppDelegate.swift @@ -25,7 +25,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let config = Realm.Configuration( - schemaVersion: 22, + schemaVersion: 23, migrationBlock: { migration, oldSchemaVersion in if oldSchemaVersion <= 3 { var numbers: [String] = [] @@ -90,6 +90,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } } + + if oldSchemaVersion <= 22 { + migration.enumerateObjects(ofType: "Vehicle") { old, new in + if let oldEnginePower = (old?["engine"] as? MigrationObject)?["powerKw"] as? Int { + (new?["engine"] as? MigrationObject)?["powerKw"] = RealmOptional(oldEnginePower) + } else { + (new?["engine"] as? MigrationObject)?["powerKw"] = RealmOptional(0) + } + } + } }) Realm.Configuration.defaultConfiguration = config diff --git a/AutoCat/Controllers/CheckController.swift b/AutoCat/Controllers/CheckController.swift index 7c94f26..691035f 100644 --- a/AutoCat/Controllers/CheckController.swift +++ b/AutoCat/Controllers/CheckController.swift @@ -241,7 +241,7 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat return Single.just(event) } - if let event = LocationManager.lastEvent, (Date().timeIntervalSince1970 - event.date) < 10 { + if let event = LocationManager.lastEvent, (Date().timeIntervalSince1970 - event.date) < 60 { return Single.just(event) } else { return LocationManager.requestCurrentLocation() diff --git a/AutoCat/Controllers/OwnersController.swift b/AutoCat/Controllers/OwnersController.swift index 0f98e84..ff4b628 100644 --- a/AutoCat/Controllers/OwnersController.swift +++ b/AutoCat/Controllers/OwnersController.swift @@ -16,7 +16,8 @@ class OwnersController: FormViewController { self.title = "\(self.owners.count) owner(s)" for (index, owner) in self.owners.enumerated() { - form +++ Section(header: "", footer: owner.lastOperation) + let section = Section(header: "", footer: owner.lastOperation) + form +++ section <<< LabelRow("Owner\(index)") { row in row.title = "Owner type" row.value = owner.ownerType @@ -35,6 +36,34 @@ class OwnersController: FormViewController { row.value = self.formatter.string(from: date) } } + + if let vehicleRegistrationRegion = owner.region { + section <<< LabelRow("VehicleRegion\(index)") { row in + row.cell.detailTextLabel?.numberOfLines = 0 + row.title = "Vehicle region" + row.value = vehicleRegistrationRegion + } + } + + + if let driverLocality = owner.locality { + var dRegion = driverLocality + if let driverRegion = owner.registrationRegion { + dRegion += " (\(driverRegion))" + } + section <<< LabelRow("DriverRegion\(index)") { row in + row.cell.detailTextLabel?.numberOfLines = 0 + row.title = "Driver region" + row.value = dRegion + } + } + + if let code = owner.code { + section <<< LabelRow("Code\(index)") { row in + row.title = "ZIP (or OKTMO) code" + row.value = code + } + } } } diff --git a/AutoCat/Controllers/ReportController.swift b/AutoCat/Controllers/ReportController.swift index 4db96dc..86e5198 100644 --- a/AutoCat/Controllers/ReportController.swift +++ b/AutoCat/Controllers/ReportController.swift @@ -239,7 +239,7 @@ class ReportController: UIViewController, UICollectionViewDataSource, UICollecti cell?.configure(param: engineSection.description, value: String(engine.powerHp)) break case .powerKw: - cell?.configure(param: engineSection.description, value: String(engine.powerKw)) + cell?.configure(param: engineSection.description, value: String(engine.powerKw.value ?? 0)) break } } diff --git a/AutoCat/Extensions/VehicleReportImage.swift b/AutoCat/Extensions/VehicleReportImage.swift index d4598e8..b590d59 100644 --- a/AutoCat/Extensions/VehicleReportImage.swift +++ b/AutoCat/Extensions/VehicleReportImage.swift @@ -114,7 +114,7 @@ extension Vehicle { y += cellHeight self.drawCell(y: y, width: w, height: cellHeight, title: "Power (HP)", value: String(self.engine?.powerHp ?? 0), context: ctx) y += cellHeight - self.drawCell(y: y, width: w, height: cellHeight, title: "Power (kw)", value: String(self.engine?.powerKw ?? 0), context: ctx) + self.drawCell(y: y, width: w, height: cellHeight, title: "Power (kw)", value: String(self.engine?.powerKw.value ?? 0), context: ctx) y += cellHeight + 32 "OWNERSHIP PERIODS (\(self.ownershipPeriods.count))".draw(with: CGRect(x: 15, y: y, width: w - 15, height: 24), options: .usesLineFragmentOrigin, attributes: headerAttributes, context: nil) diff --git a/AutoCat/Models/Vehicle.swift b/AutoCat/Models/Vehicle.swift index 23e716f..5e2abba 100644 --- a/AutoCat/Models/Vehicle.swift +++ b/AutoCat/Models/Vehicle.swift @@ -20,7 +20,7 @@ class VehicleEngine: Object, Decodable { @objc dynamic var number: String? var volume: RealmOptional = RealmOptional(0) @objc dynamic var powerHp: Float = 0 - @objc dynamic var powerKw: Float = 0 + var powerKw: RealmOptional = RealmOptional(0) @objc dynamic var fuelType: String? } @@ -64,6 +64,13 @@ class VehicleOwnershipPeriod: Object, Decodable { @objc dynamic var ownerType: String @objc dynamic var from: Int64 @objc dynamic var to: Int64 + @objc dynamic var region: String? + @objc dynamic var registrationRegion: String? + @objc dynamic var locality: String? + @objc dynamic var code: String? + @objc dynamic var street: String? + @objc dynamic var building: String? + @objc dynamic var inn: String? } class Vehicle: Object, Decodable, Identifiable, Differentiable { diff --git a/AutoCat/Utils/RxRealmDataSource.swift b/AutoCat/Utils/RxRealmDataSource.swift index bb05eb3..fed9a76 100644 --- a/AutoCat/Utils/RxRealmDataSource.swift +++ b/AutoCat/Utils/RxRealmDataSource.swift @@ -1,6 +1,7 @@ import UIKit import RealmSwift import DifferenceKit +import ExceptionCatcher class RealmSectionedDataSource: NSObject, UITableViewDataSource where Item: Object & Identifiable & Dated & Differentiable, Cell: UITableViewCell & ConfigurableCell, Cell.Item == Item { @@ -23,24 +24,33 @@ class RealmSectionedDataSource: NSObject, UITableViewDataSource where self.sections = self.data.groupedByDate() self.tv.reloadData() case .update(_, let deletions, let insertions, let modifications): - print("Deletions: \(deletions.count), Insertions: \(insertions.count), Modifications: \(modifications.count)") + var actions = "Deletions: \(deletions), Insertions: \(insertions), Modifications: \(modifications)" let newSections = self.data.groupedByDate() let changeset = StagedChangeset(source: self.sections, target: newSections, section: 0) - self.tv.beginUpdates() - self.tv.deleteRows(at: deletions.map(self.indexPath), with: .automatic) - self.sections = self.data.groupedByDate() - self.tv.insertRows(at: insertions.map(self.indexPath), with: .automatic) - self.tv.reloadRows(at: modifications.map(self.indexPath), with: .automatic) - - print("Changesets count: \(changeset.count)") - if let firstChangeset = changeset.first { - print("Deletions: \(firstChangeset.elementDeleted.map { $0.element })") - print("Insertions: \(firstChangeset.elementInserted.map { $0.element })") - self.tv.deleteSections(IndexSet(firstChangeset.elementDeleted.map { $0.element }), with: .automatic) - self.tv.insertSections(IndexSet(firstChangeset.elementInserted.map { $0.element }), with: .automatic) + do { + try ExceptionCatcher.catch { + self.tv.beginUpdates() + self.tv.deleteRows(at: deletions.map(self.indexPath), with: .automatic) + self.sections = self.data.groupedByDate() + self.tv.insertRows(at: insertions.map(self.indexPath), with: .automatic) + self.tv.reloadRows(at: modifications.map(self.indexPath), with: .automatic) + + actions += "\n" + "Changesets count: \(changeset.count)" + if let firstChangeset = changeset.first { + actions += "\n" + "Deletions: \(firstChangeset.elementDeleted.map { $0.element })" + actions += "\n" + "Insertions: \(firstChangeset.elementInserted.map { $0.element })" + self.tv.deleteSections(IndexSet(firstChangeset.elementDeleted.map { $0.element }), with: .automatic) + self.tv.insertSections(IndexSet(firstChangeset.elementInserted.map { $0.element }), with: .automatic) + } + self.tv.endUpdates() + } + } catch { + let msg = error.localizedDescription + "\n\n" + actions + let alert = UIAlertController(title: "Error", message: msg, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) + self.tv.window?.rootViewController?.present(alert, animated: true) } - self.tv.endUpdates() case .error(let err): print("Realm observer error: \(err)") }