Context menu for history list items

This commit is contained in:
Selim Mustafaev 2020-10-09 00:49:20 +03:00
parent b724b55850
commit 65d78c12d8
2 changed files with 59 additions and 26 deletions

View File

@ -160,6 +160,44 @@ 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
self.update(vehicle: vehicle)
completion(true)
}
updateAction.image = UIImage(systemName: "arrow.2.circlepath")
updateAction.backgroundColor = .systemBlue
let removeAction = UIContextualAction(style: .destructive, title: "Remove") { action, view, completion in
self.remove(vehicle: vehicle)
}
removeAction.image = UIImage(systemName: "trash")
let configuration = UISwipeActionsConfiguration(actions: [updateAction, removeAction])
configuration.performsFirstActionWithFullSwipe = false
return configuration
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vehicle = self.historyDataSource.item(at: indexPath)
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() IHProgressHUD.show()
var eventAction: EventAction = .doNotSend var eventAction: EventAction = .doNotSend
@ -174,13 +212,9 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat
IHProgressHUD.show(error: error) IHProgressHUD.show(error: error)
} }
.disposed(by: self.bag) .disposed(by: self.bag)
completion(true)
} }
updateAction.image = UIImage(systemName: "arrow.2.circlepath")
updateAction.backgroundColor = .systemBlue
let removeAction = UIContextualAction(style: .destructive, title: "Remove") { action, view, completion in func remove(vehicle: Vehicle) {
if let realm = vehicle.realm { if let realm = vehicle.realm {
do { do {
try realm.write { try realm.write {
@ -191,17 +225,6 @@ class CheckController: UIViewController, UITableViewDelegate, UITextFieldDelegat
} }
} }
} }
removeAction.image = UIImage(systemName: "trash")
let configuration = UISwipeActionsConfiguration(actions: [updateAction, removeAction])
configuration.performsFirstActionWithFullSwipe = false
return configuration
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vehicle = self.historyDataSource.item(at: indexPath)
self.updateDetailController(with: vehicle)
}
// MARK: - Checking number // MARK: - Checking number

View File

@ -23,6 +23,8 @@ class RealmSectionedDataSource<Item,Cell>: NSObject, UITableViewDataSource where
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)")