From 76a7c5bdf823f2e9c2ca55247b71e410b0f84a28 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 17 Dec 2019 21:40:08 -0500 Subject: [PATCH] Add list deletion --- .../Explore/ExploreViewController.swift | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/Tusker/Screens/Explore/ExploreViewController.swift b/Tusker/Screens/Explore/ExploreViewController.swift index 6843042105..6a26755100 100644 --- a/Tusker/Screens/Explore/ExploreViewController.swift +++ b/Tusker/Screens/Explore/ExploreViewController.swift @@ -12,7 +12,7 @@ import Pachyderm class ExploreViewController: EnhancedTableViewController { - var dataSource: UITableViewDiffableDataSource! + var dataSource: DataSource! var resultsController: SearchResultsViewController! var searchController: UISearchController! @@ -57,6 +57,7 @@ class ExploreViewController: EnhancedTableViewController { return cell }) + dataSource.exploreController = self var snapshot = NSDiffableDataSourceSnapshot() 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 { @@ -173,7 +178,11 @@ extension ExploreViewController { } } } + class DataSource: UITableViewDiffableDataSource { + + weak var exploreController: ExploreViewController? + override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { switch section { case 1: @@ -182,5 +191,40 @@ extension ExploreViewController { 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) + } + } }