From da6ff67a514dcd4b9c0928ed52ec2cb97d34e8d9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 17 Nov 2019 18:49:48 -0500 Subject: [PATCH] Add notification dismissal context menu actions Closes #49 This is a workaround for UIKit's built-in suggested context menu actions not working as expected, and should be replaced with the system thing if it becomes possible. --- .../NotificationsTableViewController.swift | 38 +++++++++++++------ .../EnhancedTableViewController.swift | 8 +++- Tusker/Screens/Utilities/Previewing.swift | 7 ---- ...ActionNotificationGroupTableViewCell.swift | 2 +- ...FollowNotificationGroupTableViewCell.swift | 2 +- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index f57b187a98..7e7170a157 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -141,18 +141,7 @@ class NotificationsTableViewController: EnhancedTableViewController { override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 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) + self.dismissNotificationsInGroup(at: indexPath) { completion(true) } } @@ -169,6 +158,31 @@ class NotificationsTableViewController: EnhancedTableViewController { return config } + override func getSuggestedContextMenuActions(tableView: UITableView, indexPath: IndexPath, point: CGPoint) -> [UIAction] { + return [ + UIAction(title: "Dismiss Notification", image: UIImage(systemName: "clear.fill"), identifier: .init("dismissnotification"), discoverabilityTitle: nil, attributes: [], state: .off, handler: { (_) in + self.dismissNotificationsInGroup(at: indexPath) + }) + ] + } + + func dismissNotificationsInGroup(at indexPath: IndexPath, completion: (() -> Void)? = nil) { + let group = DispatchGroup() + 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?() + } + } + @objc func refreshNotifications(_ sender: Any) { guard let newer = newer else { return } diff --git a/Tusker/Screens/Utilities/EnhancedTableViewController.swift b/Tusker/Screens/Utilities/EnhancedTableViewController.swift index eb8c3416ec..a8f1681ab7 100644 --- a/Tusker/Screens/Utilities/EnhancedTableViewController.swift +++ b/Tusker/Screens/Utilities/EnhancedTableViewController.swift @@ -47,7 +47,8 @@ extension EnhancedTableViewController { return nil } let actionProvider: UIContextMenuActionProvider = { (_) in - return UIMenu(title: "", image: nil, identifier: nil, options: [], children: actionsProvider()) + let suggested = self.getSuggestedContextMenuActions(tableView: tableView, indexPath: indexPath, point: point) + return UIMenu(title: "", image: nil, identifier: nil, options: [], children: suggested + actionsProvider()) } return UIContextMenuConfiguration(identifier: nil, previewProvider: previewProvider, actionProvider: actionProvider) } else { @@ -55,6 +56,11 @@ extension EnhancedTableViewController { } } + // todo: replace this with the UIKit suggested actions, if possible + @objc open func getSuggestedContextMenuActions(tableView: UITableView, indexPath: IndexPath, point: CGPoint) -> [UIAction] { + return [] + } + override func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { if let viewController = animator.previewViewController { animator.preferredCommitStyle = .pop diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index ff15df8c82..0cef0294c1 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -68,13 +68,6 @@ extension MenuPreviewProvider { }) ] } - - func actionsForNotificationGroup(_ group: NotificationGroup) -> [UIAction] { -// let notifications = group.notificationIDs.compactMap(MastodonCache.notification(for:)) - return [ - // todo: clear notifications option - ] - } private func createAction(identifier: String, title: String, systemImageName: String, handler: @escaping UIActionHandler) -> UIAction { return UIAction(title: title, image: UIImage(systemName: systemImageName), identifier: UIAction.Identifier(identifier), discoverabilityTitle: nil, attributes: [], state: .off, handler: handler) diff --git a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift index 5bcd94f93c..d05e2f6741 100644 --- a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift +++ b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift @@ -189,7 +189,7 @@ extension ActionNotificationGroupTableViewCell: MenuPreviewProvider { } return self.delegate?.statusActionAccountList(action: action, statusID: self.statusID, accountIDs: accountIDs) }, actions: { - return self.actionsForNotificationGroup(self.group) + return [] }) } diff --git a/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift b/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift index 039e5def92..db8f4f97ff 100644 --- a/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift +++ b/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift @@ -142,7 +142,7 @@ extension FollowNotificationGroupTableViewCell: MenuPreviewProvider { return AccountListTableViewController(accountIDs: accountIDs) } }, actions: { - return self.actionsForNotificationGroup(self.group) + return [] }) }