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.
This commit is contained in:
Shadowfacts 2019-11-17 18:49:48 -05:00
parent a92d9ddc6f
commit da6ff67a51
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 35 additions and 22 deletions

View File

@ -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 }

View File

@ -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

View File

@ -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)

View File

@ -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 []
})
}

View File

@ -142,7 +142,7 @@ extension FollowNotificationGroupTableViewCell: MenuPreviewProvider {
return AccountListTableViewController(accountIDs: accountIDs)
}
}, actions: {
return self.actionsForNotificationGroup(self.group)
return []
})
}