[ios] Adding swipe to delete support.

This commit is contained in:
VladiMihaylenko 2018-03-21 13:54:18 +03:00 committed by Roman Kuznetsov
parent 66dce735e6
commit 7091b13ea9
4 changed files with 45 additions and 4 deletions

View file

@ -146,6 +146,14 @@ extension BMCViewController: BMCView {
tableView.update { tableView.reloadSections(indexes, with: .automatic) }
}
}
func insert(at indexPath: IndexPath) {
tableView.insertRows(at: [indexPath], with: .automatic)
}
func delete(at indexPath: IndexPath) {
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
extension BMCViewController: UITableViewDataSource {
@ -188,6 +196,26 @@ extension BMCViewController: UITableViewDataSource {
}
extension BMCViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if viewModel.sectionType(section: indexPath.section) != .categories {
return false
}
return viewModel.numberOfRows(section: .categories) > 1
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
guard let item = viewModel.item(indexPath: indexPath) as? BMCCategory,
editingStyle == .delete,
viewModel.sectionType(section: indexPath.section) == .categories
else {
assertionFailure()
return
}
viewModel.deleteCategory(category: item)
}
func tableView(_: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch viewModel.sectionType(section: section) {
case .permissions: fallthrough

View file

@ -25,7 +25,7 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" canCancelContentTouches="NO" style="grouped" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="1" estimatedSectionFooterHeight="1" translatesAutoresizingMaskIntoConstraints="NO" id="2ia-hi-UhQ">
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" canCancelContentTouches="NO" style="grouped" separatorStyle="default" allowsSelectionDuringEditing="YES" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="1" estimatedSectionFooterHeight="1" translatesAutoresizingMaskIntoConstraints="NO" id="2ia-hi-UhQ">
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<userDefinedRuntimeAttributes>

View file

@ -121,8 +121,13 @@ extension BMCDefaultViewModel: BMCViewModel {
}
func addCategory(name: String) {
guard let section = sections.index(of: .categories) else {
assertionFailure()
return
}
categories.append(BMCCategory(identifier: BM.createCategory(withName: name), title: name))
view.update(sections: [.categories])
view.insert(at: IndexPath(row: categories.count - 1, section: section))
}
func renameCategory(category: BMCCategory, name: String) {
@ -131,9 +136,15 @@ extension BMCDefaultViewModel: BMCViewModel {
}
func deleteCategory(category: BMCCategory) {
categories.remove(at: categories.index(of: category)!)
guard let row = categories.index(of: category), let section = sections.index(of: .categories)
else {
assertionFailure()
return
}
categories.remove(at: row)
BM.deleteCategory(category.identifier)
view.update(sections: [.categories])
view.delete(at: IndexPath(row: row, section: section))
}
func beginShareCategory(category: BMCCategory) -> BMCShareCategoryStatus {

View file

@ -1,5 +1,7 @@
protocol BMCView: AnyObject {
func update(sections: [BMCSection])
func delete(at indexPath: IndexPath)
func insert(at indexPath: IndexPath)
}
enum BMCShareCategoryStatus {