From d613203d13612275bb2a77c6915bd4794b50f7fb Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Mon, 11 Apr 2022 00:41:01 +0300 Subject: [PATCH] Fixes for realm datasource --- AutoCat/Controllers/CheckController.swift | 4 ++- AutoCat/Utils/RxRealmDataSource.swift | 33 ++++++++++++++++++++--- AutoCatCore/Models/DateSection.swift | 16 +++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/AutoCat/Controllers/CheckController.swift b/AutoCat/Controllers/CheckController.swift index 2083859..29c1f5e 100644 --- a/AutoCat/Controllers/CheckController.swift +++ b/AutoCat/Controllers/CheckController.swift @@ -52,7 +52,9 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat self.check.isEnabled = false DispatchQueue.main.async { - self.historyDataSource = RealmSectionedDataSource(table: self.history, data: realm.objects(Vehicle.self).sorted(byKeyPath: "updatedDate", ascending: false)) + self.historyDataSource = RealmSectionedDataSource(table: self.history, data: realm.objects(Vehicle.self).sorted(byKeyPath: "updatedDate", ascending: false)) { count in + self.navigationItem.title = String.localizedStringWithFormat(NSLocalizedString("vehicles found", comment: ""), count) + } } self.history.delegate = self diff --git a/AutoCat/Utils/RxRealmDataSource.swift b/AutoCat/Utils/RxRealmDataSource.swift index 5c90c42..4dca7bb 100644 --- a/AutoCat/Utils/RxRealmDataSource.swift +++ b/AutoCat/Utils/RxRealmDataSource.swift @@ -18,7 +18,7 @@ class RealmSectionedDataSource: NSObject, UITableViewDataSource private var cellIdentifier: String private var filterPredicate: FilterPredicate? - init(table: UITableView, data: Results, cellIdentifier: String = String(describing: Cell.self)) { + init(table: UITableView, data: Results, cellIdentifier: String = String(describing: Cell.self), onSizeChanged: ((Int) -> Void)? = nil) { self.tv = table self.data = data self.cellIdentifier = cellIdentifier @@ -27,11 +27,20 @@ class RealmSectionedDataSource: NSObject, UITableViewDataSource self.tv.reloadData() self.notificationToken = self.data.observe { changes in + onSizeChanged?(self.data.count) switch changes { case .initial: self.reload(animated: false) - case .update(_, _, _, _): - self.reload(animated: false) + case .update(_, let deletions, let insertions, let modifications): + if deletions.isEmpty && modifications.isEmpty && insertions.count == 1 && insertions.first == 0 { + // Most common use case - adding new element to the top of the list + // Example - checking new number + self.insertFirst() + } else if deletions.isEmpty && insertions.isEmpty && modifications.count == 1 && modifications.first == 0 { + self.reloadFirst() + } else { + self.reload(animated: false) + } case .error(let err): print("Realm observer error: \(err)") } @@ -85,6 +94,24 @@ class RealmSectionedDataSource: NSObject, UITableViewDataSource } } + func insertFirst() { + guard !sections.isEmpty, let item = data.first?.clone() else { + return + } + + sections[0].insert(item, at: 0) + tv.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade) + } + + func reloadFirst() { + guard !sections.isEmpty, let item = data.first?.clone() else { + return + } + + sections[0].replace(item, at: 0) + tv.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .none) + } + func setFilterPredicate(_ predicate: FilterPredicate?) { self.filterPredicate = predicate self.reload() diff --git a/AutoCatCore/Models/DateSection.swift b/AutoCatCore/Models/DateSection.swift index 6d5eb90..b548ccc 100644 --- a/AutoCatCore/Models/DateSection.swift +++ b/AutoCatCore/Models/DateSection.swift @@ -55,6 +55,22 @@ public struct DateSection: Differentiable, DifferentiableSection, Hashable wh self.elements.append(element) } + public mutating func insert(_ element: T, at index: Int) { + self.elements.insert(element, at: index) + } + + public func index(of element: T) -> Int? { + elements.firstIndex { $0.differenceIdentifier == element.differenceIdentifier } + } + + public mutating func replace(_ element: T, at index: Int) { + elements[index] = element + } + + public mutating func remove(at index: Int) { + elements.remove(at: index) + } + // MARK: - Differentiable public var differenceIdentifier: String {