From d0c49144c4c81e9cb850d48591300e93bdbf07e5 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 16 Sep 2019 16:08:25 -0400 Subject: [PATCH] Add notification dismissal swipe action #49 --- Pachyderm/Model/Notification.swift | 4 +-- .../NotificationsTableViewController.swift | 27 ++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Pachyderm/Model/Notification.swift b/Pachyderm/Model/Notification.swift index 89da70d5..b3ac3070 100644 --- a/Pachyderm/Model/Notification.swift +++ b/Pachyderm/Model/Notification.swift @@ -15,9 +15,9 @@ public class Notification: Decodable { public let account: Account public let status: Status? - public static func dismiss(_ notification: Notification) -> Request { + public static func dismiss(id notificationID: String) -> Request { return Request(method: .post, path: "/api/v1/notifications/dismiss", body: .parameters([ - "id" => notification.id + "id" => notificationID ])) } diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index fa3fd13a..42a9970e 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -140,7 +140,32 @@ class NotificationsTableViewController: EnhancedTableViewController { } override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { - return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration() + let dismissAction = UIContextualAction(style: .destructive, title: NSLocalizedString("Dismiss", comment: "dismiss notification swipe action title")) { (action, view, completion) in + let group = DispatchGroup() + self.groups[indexPath.row].notificationIDs + .map(Pachyderm.Notification.dismiss(id:)) + .forEach { (request) in + group.enter() + MastodonController.client.run(request) { (response) in + group.leave() + } + } + group.notify(queue: .main) { + self.groups.remove(at: indexPath.row) + self.tableView.deleteRows(at: [indexPath], with: .automatic) + completion(true) + } + } + dismissAction.image = UIImage(systemName: "clear.fill") + let cellConfiguration = (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration() + let config: UISwipeActionsConfiguration + if let cellConfiguration = cellConfiguration { + config = UISwipeActionsConfiguration(actions: [dismissAction] + cellConfiguration.actions) + } else { + config = UISwipeActionsConfiguration(actions: [dismissAction]) + } + config.performsFirstActionWithFullSwipe = true + return config } @objc func refreshNotifications(_ sender: Any) {