Fixes for realm datasource

This commit is contained in:
Selim Mustafaev 2022-04-11 00:41:01 +03:00
parent 57930a46ce
commit d613203d13
3 changed files with 49 additions and 4 deletions

View File

@ -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

View File

@ -18,7 +18,7 @@ class RealmSectionedDataSource<Item,Cell>: NSObject, UITableViewDataSource
private var cellIdentifier: String
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.data = data
self.cellIdentifier = cellIdentifier
@ -27,11 +27,20 @@ class RealmSectionedDataSource<Item,Cell>: 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(_, _, _, _):
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<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>?) {
self.filterPredicate = predicate
self.reload()

View File

@ -55,6 +55,22 @@ public struct DateSection<T>: 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 {