2018-09-03 20:54:03 +00:00
|
|
|
//
|
|
|
|
// NotificationsTableViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/2/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2018-09-11 14:52:21 +00:00
|
|
|
import Pachyderm
|
2018-09-03 20:54:03 +00:00
|
|
|
|
2018-11-10 16:48:09 +00:00
|
|
|
class NotificationsTableViewController: EnhancedTableViewController {
|
2018-09-03 20:54:03 +00:00
|
|
|
|
2019-09-05 23:35:19 +00:00
|
|
|
private let statusCell = "statusCell"
|
|
|
|
private let actionGroupCell = "actionGroupCell"
|
|
|
|
private let followGroupCell = "followGroupCell"
|
2020-01-05 04:13:23 +00:00
|
|
|
private let followRequestCell = "followRequestCell"
|
2019-09-05 21:38:04 +00:00
|
|
|
|
2019-09-14 16:04:06 +00:00
|
|
|
let excludedTypes: [Pachyderm.Notification.Kind]
|
2019-09-05 23:35:19 +00:00
|
|
|
let groupTypes = [Notification.Kind.favourite, .reblog, .follow]
|
2019-09-14 16:04:06 +00:00
|
|
|
|
2019-09-05 21:38:04 +00:00
|
|
|
var groups: [NotificationGroup] = [] {
|
2018-09-03 20:54:03 +00:00
|
|
|
didSet {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.tableView.reloadData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var newer: RequestRange?
|
|
|
|
var older: RequestRange?
|
|
|
|
|
2019-09-14 16:04:06 +00:00
|
|
|
init(allowedTypes: [Pachyderm.Notification.Kind]) {
|
|
|
|
self.excludedTypes = Array(Set(Pachyderm.Notification.Kind.allCases).subtracting(allowedTypes))
|
2019-06-11 17:21:22 +00:00
|
|
|
|
2019-09-14 16:04:06 +00:00
|
|
|
super.init(style: .plain)
|
|
|
|
|
2018-10-20 16:03:18 +00:00
|
|
|
self.refreshControl = UIRefreshControl()
|
|
|
|
refreshControl!.addTarget(self, action: #selector(refreshNotifications(_:)), for: .valueChanged)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2018-09-03 20:54:03 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.estimatedRowHeight = 140
|
|
|
|
|
2020-01-05 04:13:23 +00:00
|
|
|
tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: statusCell)
|
|
|
|
tableView.register(UINib(nibName: "ActionNotificationGroupTableViewCell", bundle: .main), forCellReuseIdentifier: actionGroupCell)
|
|
|
|
tableView.register(UINib(nibName: "FollowNotificationGroupTableViewCell", bundle: .main), forCellReuseIdentifier: followGroupCell)
|
|
|
|
tableView.register(UINib(nibName: "FollowRequestNotificationTableViewCell", bundle: .main), forCellReuseIdentifier: followRequestCell)
|
2018-09-03 20:54:03 +00:00
|
|
|
|
2019-02-08 18:37:38 +00:00
|
|
|
tableView.prefetchDataSource = self
|
|
|
|
|
2019-09-14 16:04:06 +00:00
|
|
|
let request = MastodonController.client.getNotifications(excludeTypes: excludedTypes)
|
2018-10-02 23:31:00 +00:00
|
|
|
MastodonController.client.run(request) { result in
|
2018-09-03 20:54:03 +00:00
|
|
|
guard case let .success(notifications, pagination) = result else { fatalError() }
|
2019-08-01 02:09:38 +00:00
|
|
|
|
2019-09-05 21:38:04 +00:00
|
|
|
let groups = NotificationGroup.createGroups(notifications: notifications, only: self.groupTypes)
|
|
|
|
|
|
|
|
self.groups.append(contentsOf: groups)
|
|
|
|
|
2019-08-01 02:01:00 +00:00
|
|
|
MastodonCache.addAll(notifications: notifications)
|
2018-09-18 16:59:07 +00:00
|
|
|
MastodonCache.addAll(statuses: notifications.compactMap { $0.status })
|
2018-10-05 23:25:48 +00:00
|
|
|
MastodonCache.addAll(accounts: notifications.map { $0.account })
|
2019-08-01 02:09:38 +00:00
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
self.newer = pagination?.newer
|
|
|
|
self.older = pagination?.older
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Table view data source
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-09-05 21:38:04 +00:00
|
|
|
return 1
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-09-05 21:38:04 +00:00
|
|
|
return groups.count
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2019-09-05 21:38:04 +00:00
|
|
|
let group = groups[indexPath.row]
|
2018-09-03 20:54:03 +00:00
|
|
|
|
2019-09-05 21:38:04 +00:00
|
|
|
switch group.kind {
|
2018-09-03 20:54:03 +00:00
|
|
|
case .mention:
|
2019-09-05 21:38:04 +00:00
|
|
|
guard let notification = MastodonCache.notification(for: group.notificationIDs.first!),
|
2019-11-19 17:08:11 +00:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: statusCell, for: indexPath) as? TimelineStatusTableViewCell else {
|
2019-09-05 21:38:04 +00:00
|
|
|
fatalError()
|
|
|
|
}
|
2019-11-28 23:36:58 +00:00
|
|
|
cell.updateUI(statusID: notification.status!.id, state: group.statusState!)
|
2018-09-03 20:54:03 +00:00
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
2019-09-05 21:38:04 +00:00
|
|
|
|
2018-09-03 20:54:03 +00:00
|
|
|
case .favourite, .reblog:
|
2019-09-05 21:38:04 +00:00
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: actionGroupCell, for: indexPath) as? ActionNotificationGroupTableViewCell else { fatalError() }
|
|
|
|
cell.updateUI(group: group)
|
2018-09-03 20:54:03 +00:00
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
2019-09-05 21:38:04 +00:00
|
|
|
|
2018-09-03 20:54:03 +00:00
|
|
|
case .follow:
|
2020-01-05 04:13:23 +00:00
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: followGroupCell, for: indexPath) as? FollowNotificationGroupTableViewCell else { fatalError() }
|
2019-09-05 23:35:19 +00:00
|
|
|
cell.updateUI(group: group)
|
2018-09-03 20:54:03 +00:00
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
2020-01-05 04:13:23 +00:00
|
|
|
|
|
|
|
case .followRequest:
|
|
|
|
guard let notification = MastodonCache.notification(for: group.notificationIDs.first!),
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: followRequestCell, for: indexPath) as? FollowRequestNotificationTableViewCell else { fatalError() }
|
|
|
|
cell.updateUI(notification: notification)
|
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 16:59:31 +00:00
|
|
|
// MARK: - Table view delegate
|
2018-09-03 20:54:03 +00:00
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
2019-09-05 21:38:04 +00:00
|
|
|
if indexPath.row == groups.count - 1 {
|
2018-09-03 20:54:03 +00:00
|
|
|
guard let older = older else { return }
|
|
|
|
|
2019-09-14 16:04:06 +00:00
|
|
|
let request = MastodonController.client.getNotifications(excludeTypes: excludedTypes, range: older)
|
2018-10-02 23:31:00 +00:00
|
|
|
MastodonController.client.run(request) { result in
|
2018-09-03 20:54:03 +00:00
|
|
|
guard case let .success(newNotifications, pagination) = result else { fatalError() }
|
2019-08-01 02:09:38 +00:00
|
|
|
|
2019-09-05 21:38:04 +00:00
|
|
|
let groups = NotificationGroup.createGroups(notifications: newNotifications, only: self.groupTypes)
|
|
|
|
|
|
|
|
self.groups.append(contentsOf: groups)
|
|
|
|
|
2019-08-01 02:01:00 +00:00
|
|
|
MastodonCache.addAll(notifications: newNotifications)
|
|
|
|
MastodonCache.addAll(statuses: newNotifications.compactMap { $0.status })
|
|
|
|
MastodonCache.addAll(accounts: newNotifications.map { $0.account })
|
2019-08-01 02:09:38 +00:00
|
|
|
|
|
|
|
self.older = pagination?.older
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:01:13 +00:00
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
2019-06-14 02:31:36 +00:00
|
|
|
return true
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2019-06-14 02:31:36 +00:00
|
|
|
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2019-09-16 20:08:25 +00:00
|
|
|
let dismissAction = UIContextualAction(style: .destructive, title: NSLocalizedString("Dismiss", comment: "dismiss notification swipe action title")) { (action, view, completion) in
|
2019-11-17 23:49:48 +00:00
|
|
|
self.dismissNotificationsInGroup(at: indexPath) {
|
2019-09-16 20:08:25 +00:00
|
|
|
completion(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dismissAction.image = UIImage(systemName: "clear.fill")
|
|
|
|
let cellConfiguration = (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
|
|
|
|
let config: UISwipeActionsConfiguration
|
|
|
|
if let cellConfiguration = cellConfiguration {
|
2019-09-20 02:09:17 +00:00
|
|
|
config = UISwipeActionsConfiguration(actions: cellConfiguration.actions + [dismissAction])
|
|
|
|
config.performsFirstActionWithFullSwipe = cellConfiguration.performsFirstActionWithFullSwipe
|
2019-09-16 20:08:25 +00:00
|
|
|
} else {
|
|
|
|
config = UISwipeActionsConfiguration(actions: [dismissAction])
|
2019-09-20 02:09:17 +00:00
|
|
|
config.performsFirstActionWithFullSwipe = false
|
2019-09-16 20:08:25 +00:00
|
|
|
}
|
|
|
|
return config
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 23:49:48 +00:00
|
|
|
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?()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 16:03:18 +00:00
|
|
|
@objc func refreshNotifications(_ sender: Any) {
|
2018-09-03 20:54:03 +00:00
|
|
|
guard let newer = newer else { return }
|
|
|
|
|
2019-09-14 16:04:06 +00:00
|
|
|
let request = MastodonController.client.getNotifications(excludeTypes: excludedTypes, range: newer)
|
2018-10-02 23:31:00 +00:00
|
|
|
MastodonController.client.run(request) { result in
|
2018-09-03 20:54:03 +00:00
|
|
|
guard case let .success(newNotifications, pagination) = result else { fatalError() }
|
2019-08-01 02:09:38 +00:00
|
|
|
|
2019-09-05 21:38:04 +00:00
|
|
|
let groups = NotificationGroup.createGroups(notifications: newNotifications, only: self.groupTypes)
|
|
|
|
|
|
|
|
self.groups.insert(contentsOf: groups, at: 0)
|
|
|
|
|
2019-08-01 02:01:00 +00:00
|
|
|
MastodonCache.addAll(notifications: newNotifications)
|
2018-09-18 16:59:07 +00:00
|
|
|
MastodonCache.addAll(statuses: newNotifications.compactMap { $0.status })
|
2019-08-01 02:01:00 +00:00
|
|
|
MastodonCache.addAll(accounts: newNotifications.map { $0.account })
|
2019-08-01 02:09:38 +00:00
|
|
|
|
|
|
|
self.newer = pagination?.newer
|
|
|
|
|
2018-09-03 20:54:03 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.refreshControl?.endRefreshing()
|
|
|
|
|
|
|
|
// maintain the current position in the list (don't scroll to top)
|
|
|
|
self.tableView.scrollToRow(at: IndexPath(row: newNotifications.count, section: 0), at: .top, animated: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-03 22:46:20 +00:00
|
|
|
|
2019-09-09 22:40:23 +00:00
|
|
|
extension NotificationsTableViewController: StatusTableViewCellDelegate {
|
2019-11-28 23:36:58 +00:00
|
|
|
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell) {
|
2019-09-09 22:40:23 +00:00
|
|
|
// causes the table view to recalculate the cell heights
|
|
|
|
tableView.beginUpdates()
|
|
|
|
tableView.endUpdates()
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 18:37:38 +00:00
|
|
|
|
|
|
|
extension NotificationsTableViewController: UITableViewDataSourcePrefetching {
|
|
|
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
|
|
|
for indexPath in indexPaths {
|
2019-09-05 21:38:04 +00:00
|
|
|
for notificationID in groups[indexPath.row].notificationIDs {
|
|
|
|
guard let notification = MastodonCache.notification(for: notificationID) else { continue }
|
|
|
|
ImageCache.avatars.get(notification.account.avatar, completion: nil)
|
|
|
|
}
|
2019-02-08 18:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
|
|
|
for indexPath in indexPaths {
|
2019-09-05 21:38:04 +00:00
|
|
|
for notificationID in groups[indexPath.row].notificationIDs {
|
|
|
|
guard let notification = MastodonCache.notification(for: notificationID) else { continue }
|
|
|
|
ImageCache.avatars.cancel(notification.account.avatar)
|
|
|
|
}
|
2019-02-08 18:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|