// // NotificationsTableViewController.swift // Tusker // // Created by Shadowfacts on 9/2/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class NotificationsTableViewController: EnhancedTableViewController { var timelineSegments: [TimelineSegment] = [] { didSet { DispatchQueue.main.async { self.tableView.reloadData() } } } var newer: RequestRange? var older: RequestRange? init() { super.init(style: .plain) title = "Notifications" tabBarItem.image = UIImage(systemName: "bell.fill") self.refreshControl = UIRefreshControl() refreshControl!.addTarget(self, action: #selector(refreshNotifications(_:)), for: .valueChanged) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 140 tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell") tableView.register(UINib(nibName: "ActionNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "actionCell") tableView.register(UINib(nibName: "FollowNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "followCell") tableView.prefetchDataSource = self let request = MastodonController.client.getNotifications() MastodonController.client.run(request) { result in guard case let .success(notifications, pagination) = result else { fatalError() } self.timelineSegments.append(TimelineSegment(objects: notifications)) MastodonCache.addAll(notifications: notifications) MastodonCache.addAll(statuses: notifications.compactMap { $0.status }) MastodonCache.addAll(accounts: notifications.map { $0.account }) self.newer = pagination?.newer self.older = pagination?.older } userActivity = UserActivityManager.checkNotificationsActivity() } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return timelineSegments.count } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return timelineSegments[section].count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let notificationID = timelineSegments[indexPath.section][indexPath.row] guard let notification = MastodonCache.notification(for: notificationID) else { fatalError() } switch notification.kind { case .mention: guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() } cell.updateUI(statusID: notification.status!.id) cell.delegate = self return cell case .favourite, .reblog: guard let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as? ActionNotificationTableViewCell else { fatalError() } cell.updateUI(for: notification) cell.delegate = self return cell case .follow: guard let cell = tableView.dequeueReusableCell(withIdentifier: "followCell", for: indexPath) as? FollowNotificationTableViewCell else { fatalError() } cell.updateUI(for: notification) cell.delegate = self return cell } } override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if indexPath.section == timelineSegments.count - 1, indexPath.row == timelineSegments[indexPath.section].count - 1 { guard let older = older else { return } let request = MastodonController.client.getNotifications(range: older) MastodonController.client.run(request) { result in guard case let .success(newNotifications, pagination) = result else { fatalError() } self.timelineSegments[self.timelineSegments.count - 1].append(objects: newNotifications) MastodonCache.addAll(notifications: newNotifications) MastodonCache.addAll(statuses: newNotifications.compactMap { $0.status }) MastodonCache.addAll(accounts: newNotifications.map { $0.account }) self.older = pagination?.older } } } override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration() } override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration() } @objc func refreshNotifications(_ sender: Any) { guard let newer = newer else { return } let request = MastodonController.client.getNotifications(range: newer) MastodonController.client.run(request) { result in guard case let .success(newNotifications, pagination) = result else { fatalError() } self.timelineSegments[0].insertAtBeginning(objects: newNotifications) MastodonCache.addAll(notifications: newNotifications) MastodonCache.addAll(statuses: newNotifications.compactMap { $0.status }) MastodonCache.addAll(accounts: newNotifications.map { $0.account }) self.newer = pagination?.newer 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) } } } } extension NotificationsTableViewController: StatusTableViewCellDelegate {} extension NotificationsTableViewController: UITableViewDataSourcePrefetching { func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) { for indexPath in indexPaths { let notificationID = timelineSegments[indexPath.section][indexPath.row] guard let notification = MastodonCache.notification(for: notificationID) else { continue } ImageCache.avatars.get(notification.account.avatar, completion: nil) } } func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) { for indexPath in indexPaths { let notificationID = timelineSegments[indexPath.section][indexPath.row] guard let notification = MastodonCache.notification(for: notificationID) else { continue } ImageCache.avatars.cancel(notification.account.url) } } }