Add list deletion

This commit is contained in:
Shadowfacts 2019-12-17 21:40:08 -05:00
parent 19f15d8fa9
commit 76a7c5bdf8
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 45 additions and 1 deletions

View File

@ -12,7 +12,7 @@ import Pachyderm
class ExploreViewController: EnhancedTableViewController { class ExploreViewController: EnhancedTableViewController {
var dataSource: UITableViewDiffableDataSource<Section, Item>! var dataSource: DataSource!
var resultsController: SearchResultsViewController! var resultsController: SearchResultsViewController!
var searchController: UISearchController! var searchController: UISearchController!
@ -57,6 +57,7 @@ class ExploreViewController: EnhancedTableViewController {
return cell return cell
}) })
dataSource.exploreController = self
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>() var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.bookmarks, .lists]) snapshot.appendSections([.bookmarks, .lists])
@ -137,6 +138,10 @@ class ExploreViewController: EnhancedTableViewController {
} }
} }
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
} }
extension ExploreViewController { extension ExploreViewController {
@ -173,7 +178,11 @@ extension ExploreViewController {
} }
} }
} }
class DataSource: UITableViewDiffableDataSource<Section, Item> { class DataSource: UITableViewDiffableDataSource<Section, Item> {
weak var exploreController: ExploreViewController?
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section { switch section {
case 1: case 1:
@ -182,5 +191,40 @@ extension ExploreViewController {
return nil return nil
} }
} }
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if case .list(_) = itemIdentifier(for: indexPath) {
return true
}
return false
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete,
case let .list(list) = itemIdentifier(for: indexPath) else {
return
}
let title = String(format: NSLocalizedString("Are you sure want to delete the '%@' list?", comment: "delete list alert title"), list.title)
let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "delete list alert cancel button"), style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: NSLocalizedString("Delete List", comment: "delete list alert confirm button"), style: .destructive, handler: { (_) in
let request = List.delete(list)
MastodonController.client.run(request) { (response) in
guard case .success(_, _) = response else {
fatalError()
}
var snapshot = self.snapshot()
snapshot.deleteItems([.list(list)])
DispatchQueue.main.async {
self.apply(snapshot)
}
}
}))
self.exploreController?.present(alert, animated: true)
}
} }
} }