Use timeline segments for notifications

This commit is contained in:
Shadowfacts 2019-07-31 20:09:38 -06:00
parent 82c56d2bd1
commit 9dfaa9e023
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 23 additions and 12 deletions

View File

@ -11,7 +11,7 @@ import Pachyderm
class NotificationsTableViewController: EnhancedTableViewController {
var notificationIDs: [String] = [] {
var timelineSegments: [TimelineSegment<Pachyderm.Notification>] = [] {
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
@ -51,10 +51,12 @@ class NotificationsTableViewController: EnhancedTableViewController {
let request = MastodonController.client.getNotifications()
MastodonController.client.run(request) { result in
guard case let .success(notifications, pagination) = result else { fatalError() }
self.notificationIDs = notifications.map { $0.id }
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
}
@ -75,16 +77,17 @@ class NotificationsTableViewController: EnhancedTableViewController {
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
return timelineSegments.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notificationIDs.count
return timelineSegments[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let notification = MastodonCache.notification(for: notificationIDs[indexPath.row]) else { fatalError() }
let notificationID = timelineSegments[indexPath.section][indexPath.row]
guard let notification = MastodonCache.notification(for: notificationID) else { fatalError() }
switch notification.kind {
case .mention:
@ -108,17 +111,20 @@ class NotificationsTableViewController: EnhancedTableViewController {
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == notificationIDs.count - 1 {
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.older = pagination?.older
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.notificationIDs.append(contentsOf: newNotifications.map { $0.id })
self.older = pagination?.older
}
}
}
@ -141,11 +147,14 @@ class NotificationsTableViewController: EnhancedTableViewController {
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
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.notificationIDs.insert(contentsOf: newNotifications.map { $0.id }, at: 0)
self.newer = pagination?.newer
DispatchQueue.main.async {
self.refreshControl?.endRefreshing()
@ -162,14 +171,16 @@ extension NotificationsTableViewController: StatusTableViewCellDelegate {}
extension NotificationsTableViewController: UITableViewDataSourcePrefetching {
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
for indexPath in indexPaths {
guard let notification = MastodonCache.notification(for: notificationIDs[indexPath.row]) else { continue }
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 {
guard let notification = MastodonCache.notification(for: notificationIDs[indexPath.row]) else { continue }
let notificationID = timelineSegments[indexPath.section][indexPath.row]
guard let notification = MastodonCache.notification(for: notificationID) else { continue }
ImageCache.avatars.cancel(notification.account.url)
}
}