202 lines
8.2 KiB
Swift
202 lines
8.2 KiB
Swift
//
|
|
// NotificationsTableViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 9/2/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Pachyderm
|
|
|
|
class NotificationsTableViewController: EnhancedTableViewController {
|
|
|
|
let statusCell = "statusCell"
|
|
let actionGroupCell = "actionGroupCell"
|
|
let followCell = "followCell"
|
|
|
|
let groupTypes = [Notification.Kind.favourite, .reblog]
|
|
|
|
var groups: [NotificationGroup] = [] {
|
|
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: "ActionNotificationGroupTableViewCell", bundle: nil), forCellReuseIdentifier: actionGroupCell)
|
|
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() }
|
|
|
|
let groups = NotificationGroup.createGroups(notifications: notifications, only: self.groupTypes)
|
|
|
|
self.groups.append(contentsOf: groups)
|
|
|
|
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 1
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return groups.count
|
|
}
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let group = groups[indexPath.row]
|
|
|
|
switch group.kind {
|
|
case .mention:
|
|
guard let notification = MastodonCache.notification(for: group.notificationIDs.first!),
|
|
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: actionGroupCell, for: indexPath) as? ActionNotificationGroupTableViewCell else { fatalError() }
|
|
cell.updateUI(group: group)
|
|
cell.delegate = self
|
|
return cell
|
|
|
|
case .follow:
|
|
guard let notification = MastodonCache.notification(for: group.notificationIDs.first!) else { fatalError() }
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: followCell, for: indexPath) as? FollowNotificationTableViewCell else { fatalError() }
|
|
cell.updateUI(for: notification)
|
|
cell.delegate = self
|
|
return cell
|
|
// guard let cell = tableView.dequeueReusableCell(withIdentifier: "followGroupCell", for: indexPath) as? FollowNotificationGroupTableViewCell else { fatalError() }
|
|
// cell.updateUI(notificationGroup: group)
|
|
// cell.delegate = self
|
|
// return cell
|
|
}
|
|
}
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
if indexPath.row == groups.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() }
|
|
|
|
let groups = NotificationGroup.createGroups(notifications: newNotifications, only: self.groupTypes)
|
|
|
|
self.groups.append(contentsOf: groups)
|
|
|
|
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() }
|
|
|
|
let groups = NotificationGroup.createGroups(notifications: newNotifications, only: self.groupTypes)
|
|
|
|
self.groups.insert(contentsOf: groups, at: 0)
|
|
|
|
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 {
|
|
for notificationID in groups[indexPath.row].notificationIDs {
|
|
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 {
|
|
for notificationID in groups[indexPath.row].notificationIDs {
|
|
guard let notification = MastodonCache.notification(for: notificationID) else { continue }
|
|
ImageCache.avatars.cancel(notification.account.avatar)
|
|
}
|
|
}
|
|
}
|
|
}
|