Fixes for realm datasource
This commit is contained in:
parent
57930a46ce
commit
d613203d13
@ -52,7 +52,9 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat
|
|||||||
self.check.isEnabled = false
|
self.check.isEnabled = false
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
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
|
self.history.delegate = self
|
||||||
|
|||||||
@ -18,7 +18,7 @@ class RealmSectionedDataSource<Item,Cell>: NSObject, UITableViewDataSource
|
|||||||
private var cellIdentifier: String
|
private var cellIdentifier: String
|
||||||
private var filterPredicate: FilterPredicate<Item>?
|
private var filterPredicate: FilterPredicate<Item>?
|
||||||
|
|
||||||
init(table: UITableView, data: Results<Item>, cellIdentifier: String = String(describing: Cell.self)) {
|
init(table: UITableView, data: Results<Item>, cellIdentifier: String = String(describing: Cell.self), onSizeChanged: ((Int) -> Void)? = nil) {
|
||||||
self.tv = table
|
self.tv = table
|
||||||
self.data = data
|
self.data = data
|
||||||
self.cellIdentifier = cellIdentifier
|
self.cellIdentifier = cellIdentifier
|
||||||
@ -27,11 +27,20 @@ class RealmSectionedDataSource<Item,Cell>: NSObject, UITableViewDataSource
|
|||||||
self.tv.reloadData()
|
self.tv.reloadData()
|
||||||
|
|
||||||
self.notificationToken = self.data.observe { changes in
|
self.notificationToken = self.data.observe { changes in
|
||||||
|
onSizeChanged?(self.data.count)
|
||||||
switch changes {
|
switch changes {
|
||||||
case .initial:
|
case .initial:
|
||||||
self.reload(animated: false)
|
self.reload(animated: false)
|
||||||
case .update(_, _, _, _):
|
case .update(_, let deletions, let insertions, let modifications):
|
||||||
self.reload(animated: false)
|
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):
|
case .error(let err):
|
||||||
print("Realm observer error: \(err)")
|
print("Realm observer error: \(err)")
|
||||||
}
|
}
|
||||||
@ -85,6 +94,24 @@ class RealmSectionedDataSource<Item,Cell>: 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<Item>?) {
|
func setFilterPredicate(_ predicate: FilterPredicate<Item>?) {
|
||||||
self.filterPredicate = predicate
|
self.filterPredicate = predicate
|
||||||
self.reload()
|
self.reload()
|
||||||
|
|||||||
@ -55,6 +55,22 @@ public struct DateSection<T>: Differentiable, DifferentiableSection, Hashable wh
|
|||||||
self.elements.append(element)
|
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
|
// MARK: - Differentiable
|
||||||
|
|
||||||
public var differenceIdentifier: String {
|
public var differenceIdentifier: String {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user