Context menu for history list items
This commit is contained in:
parent
b724b55850
commit
65d78c12d8
@ -160,36 +160,14 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat
|
|||||||
let vehicle = self.historyDataSource.item(at: indexPath)
|
let vehicle = self.historyDataSource.item(at: indexPath)
|
||||||
|
|
||||||
let updateAction = UIContextualAction(style: .normal, title: "Update") { action, view, completion in
|
let updateAction = UIContextualAction(style: .normal, title: "Update") { action, view, completion in
|
||||||
IHProgressHUD.show()
|
self.update(vehicle: vehicle)
|
||||||
|
|
||||||
var eventAction: EventAction = .doNotSend
|
|
||||||
if vehicle.unrecognized, let savedEvent = vehicle.events.first {
|
|
||||||
eventAction = .sendSpecific(savedEvent)
|
|
||||||
}
|
|
||||||
|
|
||||||
self.check(number: vehicle.getNumber(), action: eventAction, force: true).subscribe { vehicle in
|
|
||||||
self.updateDetailController(with: vehicle)
|
|
||||||
IHProgressHUD.dismiss()
|
|
||||||
} onError: { error in
|
|
||||||
IHProgressHUD.show(error: error)
|
|
||||||
}
|
|
||||||
.disposed(by: self.bag)
|
|
||||||
|
|
||||||
completion(true)
|
completion(true)
|
||||||
}
|
}
|
||||||
updateAction.image = UIImage(systemName: "arrow.2.circlepath")
|
updateAction.image = UIImage(systemName: "arrow.2.circlepath")
|
||||||
updateAction.backgroundColor = .systemBlue
|
updateAction.backgroundColor = .systemBlue
|
||||||
|
|
||||||
let removeAction = UIContextualAction(style: .destructive, title: "Remove") { action, view, completion in
|
let removeAction = UIContextualAction(style: .destructive, title: "Remove") { action, view, completion in
|
||||||
if let realm = vehicle.realm {
|
self.remove(vehicle: vehicle)
|
||||||
do {
|
|
||||||
try realm.write {
|
|
||||||
realm.delete(vehicle)
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
print(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
removeAction.image = UIImage(systemName: "trash")
|
removeAction.image = UIImage(systemName: "trash")
|
||||||
|
|
||||||
@ -203,6 +181,51 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat
|
|||||||
self.updateDetailController(with: vehicle)
|
self.updateDetailController(with: vehicle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
||||||
|
let vehicle = self.historyDataSource.item(at: indexPath)
|
||||||
|
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
|
||||||
|
let update = UIAction(title: "Update", image: UIImage(systemName: "arrow.2.circlepath")) { action in
|
||||||
|
self.update(vehicle: vehicle)
|
||||||
|
}
|
||||||
|
let remove = UIAction(title: "Remove", image: UIImage(systemName: "trash"), attributes: [.destructive]) { action in
|
||||||
|
self.remove(vehicle: vehicle)
|
||||||
|
}
|
||||||
|
|
||||||
|
return UIMenu(title: "Actions", children: [update, remove])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Contextual actions
|
||||||
|
|
||||||
|
func update(vehicle: Vehicle) {
|
||||||
|
IHProgressHUD.show()
|
||||||
|
|
||||||
|
var eventAction: EventAction = .doNotSend
|
||||||
|
if vehicle.unrecognized, let savedEvent = vehicle.events.first {
|
||||||
|
eventAction = .sendSpecific(savedEvent)
|
||||||
|
}
|
||||||
|
|
||||||
|
self.check(number: vehicle.getNumber(), action: eventAction, force: true).subscribe { vehicle in
|
||||||
|
self.updateDetailController(with: vehicle)
|
||||||
|
IHProgressHUD.dismiss()
|
||||||
|
} onError: { error in
|
||||||
|
IHProgressHUD.show(error: error)
|
||||||
|
}
|
||||||
|
.disposed(by: self.bag)
|
||||||
|
}
|
||||||
|
|
||||||
|
func remove(vehicle: Vehicle) {
|
||||||
|
if let realm = vehicle.realm {
|
||||||
|
do {
|
||||||
|
try realm.write {
|
||||||
|
realm.delete(vehicle)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Checking number
|
// MARK: - Checking number
|
||||||
|
|
||||||
func save(vehicle: Vehicle) throws {
|
func save(vehicle: Vehicle) throws {
|
||||||
|
|||||||
@ -22,7 +22,9 @@ class RealmSectionedDataSource<Item,Cell>: NSObject, UITableViewDataSource where
|
|||||||
case .initial:
|
case .initial:
|
||||||
self.sections = self.data.groupedByDate()
|
self.sections = self.data.groupedByDate()
|
||||||
self.tv.reloadData()
|
self.tv.reloadData()
|
||||||
case .update(_, let deletions, let insertions, let modifications):
|
case .update(_, let deletions, let insertions, let modifications):
|
||||||
|
print("====== Deletes: \(deletions.count), Inserts: \(insertions.count), Mods: \(modifications.count)")
|
||||||
|
let oldSectionsCount = self.sections.count
|
||||||
self.tv.beginUpdates()
|
self.tv.beginUpdates()
|
||||||
let delPaths = deletions.map(self.indexPath)
|
let delPaths = deletions.map(self.indexPath)
|
||||||
self.tv.deleteRows(at: deletions.map(self.indexPath), with: .automatic)
|
self.tv.deleteRows(at: deletions.map(self.indexPath), with: .automatic)
|
||||||
@ -35,7 +37,15 @@ class RealmSectionedDataSource<Item,Cell>: NSObject, UITableViewDataSource where
|
|||||||
self.tv.insertSections(IndexSet(integer: insPath.section), with: .automatic)
|
self.tv.insertSections(IndexSet(integer: insPath.section), with: .automatic)
|
||||||
}
|
}
|
||||||
self.tv.insertRows(at: insPaths, with: .automatic)
|
self.tv.insertRows(at: insPaths, with: .automatic)
|
||||||
self.tv.reloadRows(at: modifications.map(self.indexPath), with: .automatic)
|
let modPaths = modifications.map(self.indexPath)
|
||||||
|
if let modPath = modPaths.first {
|
||||||
|
if oldSectionsCount > self.sections.count {
|
||||||
|
self.tv.deleteSections(IndexSet(integer: modPath.section), with: .automatic)
|
||||||
|
} else if oldSectionsCount < self.sections.count {
|
||||||
|
self.tv.insertSections(IndexSet(integer: modPath.section), with: .automatic)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.tv.reloadRows(at: modPaths, with: .automatic)
|
||||||
self.tv.endUpdates()
|
self.tv.endUpdates()
|
||||||
case .error(let err):
|
case .error(let err):
|
||||||
print("Realm observer error: \(err)")
|
print("Realm observer error: \(err)")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user