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
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
var notifications: [Pachyderm.Notification] = [] {
|
2018-09-03 20:54:03 +00:00
|
|
|
didSet {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.tableView.reloadData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var newer: RequestRange?
|
|
|
|
var older: RequestRange?
|
|
|
|
|
2019-01-19 19:31:31 +00:00
|
|
|
init() {
|
2018-10-20 16:03:18 +00:00
|
|
|
super.init(style: .plain)
|
2018-10-21 02:07:04 +00:00
|
|
|
|
2019-06-11 17:21:22 +00:00
|
|
|
title = "Notifications"
|
|
|
|
tabBarItem.image = UIImage(systemName: "bell.fill")
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2019-02-08 18:37:38 +00:00
|
|
|
tableView.prefetchDataSource = self
|
|
|
|
|
2018-10-02 23:31:00 +00:00
|
|
|
let request = MastodonController.client.getNotifications()
|
|
|
|
MastodonController.client.run(request) { result in
|
2018-09-03 20:54:03 +00:00
|
|
|
guard case let .success(notifications, pagination) = result else { fatalError() }
|
|
|
|
self.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 })
|
2018-09-11 14:52:21 +00:00
|
|
|
self.newer = pagination?.newer
|
|
|
|
self.older = pagination?.older
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
2018-10-12 01:20:58 +00:00
|
|
|
|
2018-10-20 14:54:59 +00:00
|
|
|
userActivity = UserActivityManager.checkNotificationsActivity()
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
switch notification.kind {
|
2018-09-03 20:54:03 +00:00
|
|
|
case .mention:
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
|
|
|
|
let status = notification.status!
|
2018-09-18 01:57:46 +00:00
|
|
|
cell.updateUI(for: status.id)
|
2018-09-03 20:54:03 +00:00
|
|
|
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 }
|
|
|
|
|
2018-10-02 23:31:00 +00:00
|
|
|
let request = MastodonController.client.getNotifications(range: older)
|
|
|
|
MastodonController.client.run(request) { result in
|
2018-09-03 20:54:03 +00:00
|
|
|
guard case let .success(newNotifications, pagination) = result else { fatalError() }
|
2018-09-11 14:52:21 +00:00
|
|
|
self.older = pagination?.older
|
2018-09-03 20:54:03 +00:00
|
|
|
self.notifications.append(contentsOf: newNotifications)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:01:13 +00:00
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
2019-06-04 17:31:05 +00:00
|
|
|
// return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
|
|
|
|
return false
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2019-06-04 17:31:05 +00:00
|
|
|
// return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
|
|
|
|
return nil
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2019-06-04 17:31:05 +00:00
|
|
|
// return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
|
|
|
|
return nil
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
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 }
|
|
|
|
|
2018-10-02 23:31:00 +00:00
|
|
|
let request = MastodonController.client.getNotifications(range: newer)
|
|
|
|
MastodonController.client.run(request) { result in
|
2018-09-03 20:54:03 +00:00
|
|
|
guard case let .success(newNotifications, pagination) = result else { fatalError() }
|
2018-09-11 14:52:21 +00:00
|
|
|
self.newer = pagination?.newer
|
2018-09-18 16:59:07 +00:00
|
|
|
MastodonCache.addAll(statuses: newNotifications.compactMap { $0.status })
|
2018-09-03 20:54:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-03 22:46:20 +00:00
|
|
|
|
|
|
|
extension NotificationsTableViewController: StatusTableViewCellDelegate {}
|
2019-02-08 18:37:38 +00:00
|
|
|
|
|
|
|
extension NotificationsTableViewController: UITableViewDataSourcePrefetching {
|
|
|
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
|
|
|
for indexPath in indexPaths {
|
|
|
|
let notification = notifications[indexPath.row]
|
|
|
|
ImageCache.avatars.get(notification.account.avatar, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
|
|
|
for indexPath in indexPaths {
|
|
|
|
let notification = notifications[indexPath.row]
|
|
|
|
ImageCache.avatars.cancel(notification.account.url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|