diff --git a/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.swift b/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.swift index 93918985b5..c7381b9319 100644 --- a/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.swift +++ b/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.swift @@ -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 diff --git a/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.xib b/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.xib index 76a1d24a65..a912753879 100644 --- a/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.xib +++ b/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.xib @@ -25,7 +25,7 @@ - + diff --git a/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCDefaultViewModel.swift b/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCDefaultViewModel.swift index 1cf040872a..dd50f76339 100644 --- a/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCDefaultViewModel.swift +++ b/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCDefaultViewModel.swift @@ -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 { diff --git a/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCViewModel.swift b/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCViewModel.swift index de2ae5c8b7..ae09b23d7e 100644 --- a/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCViewModel.swift +++ b/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCViewModel.swift @@ -1,5 +1,7 @@ protocol BMCView: AnyObject { func update(sections: [BMCSection]) + func delete(at indexPath: IndexPath) + func insert(at indexPath: IndexPath) } enum BMCShareCategoryStatus {