// // NotificationsTableViewController.swift // Tusker // // Created by Shadowfacts on 9/2/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class NotificationsTableViewController: UITableViewController { let router: AppRouter var notifications: [Pachyderm.Notification] = [] { didSet { DispatchQueue.main.async { self.tableView.reloadData() } } } var newer: RequestRange? var older: RequestRange? init(router: AppRouter) { self.router = router super.init(style: .plain) self.title = "Notifications" 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") let request = MastodonController.client.getNotifications() MastodonController.client.run(request) { result in guard case let .success(notifications, pagination) = result else { fatalError() } self.notifications = notifications MastodonCache.addAll(statuses: notifications.compactMap { $0.status }) MastodonCache.addAll(accounts: notifications.map { $0.account }) self.newer = pagination?.newer self.older = pagination?.older } registerForPreviewing(with: self, sourceView: view) userActivity = UserActivityManager.checkNotificationsActivity() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) for cell in tableView.visibleCells { if let cell = cell as? PreferencesAdaptive { cell.updateUIForPreferences() } } } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return notifications.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let notification = notifications[indexPath.row] switch notification.kind { case .mention: guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() } let status = notification.status! cell.updateUI(for: 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.row == notifications.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.older = pagination?.older self.notifications.append(contentsOf: newNotifications) } } } override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider } 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.newer = pagination?.newer MastodonCache.addAll(statuses: newNotifications.compactMap { $0.status }) self.notifications.insert(contentsOf: newNotifications, at: 0) 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 {}